[Object Oriented] Design and Implementation of the terminator program of the mini game

Source: Internet
Author: User

I entered school in September and graduated in July. It ended my pleasant and rich college life at the Software Institute. This series is a review of the four-year study of professional courses, see: http://blog.csdn.net/xiaowei_cqu/article/details/7747205

Object-Oriented Programming

This is the first lesson in programming. I was also the first time in this course to get started with programming languages and write code. We will use the C ++ Program Design: An Introduction to programming and object-oriented design in the photographic edition as the teaching material.

Now, it is really painful to come to that "Getting started" experience. Many concepts are hard to understand, so we can only repeat the code in the book over and over again. But every time I run a little thing, I feel excited. At the end of freshman year, there was a "big" Course Design: a group of two developers wrote a small game. I and Xiaoqi wrote a simple game, and after this experience, I really felt like getting started with programming.

Game "yingmu" [Game Function Requirement Description] This game is designed based on Japanese cartoon "dunk expert". I believe everyone is quite familiar with the characters in the game. In the game, we use the upper, lower, left, and right buttons to control the movement of players. If a player encounters an enemy, the game ends. If all enemies are eliminated, the game is cleared. The game is divided into two levels. The map of each level is randomly generated, and the enemy's movement speed is gradually accelerated. Although its functions seem simple, there is endless fun. It is a game that combines entertainment and adventure.
This time we designed it in the development environment of Microsoft Visual C ++ 6.0 and ezwindow library. We didn't think of a good way to implement some program functions at the moment, so this game is not presented to everyone. I believe that in the future, we will be able to better master and update this game. [General game category diagram] [key categories in the Game] Player
Enum floor {floor1 = 0, floor2, floor3, floor4}; class player {public: // constructorplayer (simplewindow & W); Public: // inspectors simplewindow & getwindow () const; // obtain the position getposition () const in the player's window; // obtain the current player position direction getdirection () const; // obtain the current bitmap & getbmp (const Direction & D, int I); // get the const bitmap & getbmp (const Direction & D, int I) const in the corresponding direction and step; floor getfloor () const; // get the current layer of the player bool isdying (); // check whether the player is dead bool atrightedge () const; // check whether the player has reached the edge of the window bool atleftedge () const; // facilitatorsvoid create (); // create a player (that is, the player is "placed" in the game window) void kill (); // "kill" the player (erased from the window) void move (); // enables the player to move the void fire () under keyboard control; // fired the void okup (); // set whether the player can jump between layers void okdown (); void cannotupdown (); // mutatorsvoid setposition (const Position & P ); // set the location void setfloor (floor & F) for all the current bitmap of the player; // void setdirection (const Direction & D), a data member of the layer after the player jumps ); // set the player's current direction // data member vector <bullet *> bullets; /*************************************** * /// check the bullets and enemies in the gamecontroller, so in the public domain, set to visible ///********************************** * *****/PRIVATE: // facilitiesvoid draw (); void erase (); /*************************************** * *********** // the user does not directly manipulate the image, instead, call creat () and kill () /// so it is defined as private ///***************************** * ********************* // data memberssimplewindow & window; vector <vector <bitmap> BMP; Direction currentdirection; position currentposition; bool bupok; bool bdownok; floor currentfloor; // The current layer // in the game, the layer is not a layer-like layer, but a layer of the floor defined by enumeration. // because the layer inherits from the map, the key is a graph, there are no special properties. // The layer defined by enumeration has initialization function, is that each layer has its own value //, and the enemies of each layer allocated in the corresponding gamecontroller constructor and the gap/of the Basketball box also correspond to their own values in the vector, therefore, in some functions of the game controller, // (such as testgap and testenemy) only checks the basketball boxes and enemies on the current layer, // avoids unexpected Implementation of the jumping effect, it also improves the checking efficiency. Int steps; // number of steps the player has taken since the beginning of the game // It is used to switch the player bitmap in turn continuously to achieve the moving effect };
Enemy and bullet
/*************************************** * ******************* // Enemy. h /////// class enemy declaration and definition //// this is the enemy in the game, the enemy keeps walking in their respective layers ///**************************** * *****************************/# ifndef enemy_h # define enemy_hconst int enemybitmaps = 2; enum enemystatus {alive, dead, deadalready}; // deadalready ensures that enemy will not be repeatedly killed by killclass enemy {public: // constructor enemy (simplewindow & W, const Position & p1 = (0, 0), const Position & p2 = (0, 0), double H = 1); // you need to provide a window in which the enemy moves, and the size (speed) of each displacement public: // inspectors simplewindow & getwindow () const; // obtain the position getposition () const of the window where the enemy is located; // obtain the current enemy's position direction getdirection () const; // obtain the current enemy's direction double gethorizmovement () const; // obtain the horizontal shift bitmap & getbmp (const Direction & D) for each movement; // obtain the corresponding direction bitmap const bitmap & getbmp (const Direction & D) const; // obtain the current enemy state. enemystatus getstatus () const; // mutators void setwindow (simplewindow & W); // set the enemy window void setdirection (const Direction & D ); // set the direction of the enemy void setposition (const Position & P); // set the position of the enemy void setstatus (enemystatus S ); // change the enemy's status (you will see its importance later) void sethorizmovement (double H ); // set the size of each horizontal displacement to change the moving speed void setp1p2 (const Position & PP1, const Position & pp2 ); // set the range of enemy movement // facilitators void create (); // create an enemy (drawn in the game window) void kill (); // kill the enemy (erased from the current window) void move (); // enable the enemy to constantly move void draw () under the timer; // draw the current bitmap void erase (); // erase position newposition () const; // set the direction void changedirection () of the enemy in the next moment; // check if the direction is out of the moving range, change the direction to private: // data members simplewindow & window; vector <bitmap> BMP; double horizmovement; Direction currentdirection; position currentposition; position P1, P2; enemystatus currenstatus ;}; # endif /************************************* ** // derive class enemy_1 // enemies of the first and second levels are derived from the class enemy, but bitmap is different, different speeds ///*********************************** * ***/# ifndef enemy_1_h # define enemy_1_h # include "enemy. H "class enemy_1: Public Enemy {public: enemy_1 (simplewindow & W, const Position & p1 = (0, 0), const Position & p2 = (0, 0 ), double H = 0.5 );}; # endif/******************************** // derive class enemy_2 // ********************************/# ifndef enemy_2_h # define enemy_2_h # include "enemy. H "class enemy_2: Public Enemy {public: enemy_2 (simplewindow & W, const Position & p1 = (0, 0), const Position & p2 = (0, 0 ), double H = 0.8 );}; # endif /************************************* * ************* // derive class bullet // the bullets fired by Sakura wood in the game /// automatically horizontally in one direction mobile, ///*********************************** * ***************/# ifndef bullet_h # define bullet_h # include "enemy. H "class bullet: Public Enemy {public: Bullet (simplewindow & W, direction D, const Position & p1 = (0, 0), const Position & p2 = (0, 0 )); bool atrightedge () const; // after the bullet moves out of the range, it should be eliminated. // Therefore, the corresponding judgment function is added to return the Boolean value bool atleftedge () const; void B _creat (); // create a bullet}; # endif
Gamecontroller
/*************************************** * ****************** // Gamecoroler. h //// game console, which controls and checks the status of each role in the game and interaction between different /// roles, is the most important and most labor-intensive part of the game ///*************************** * ******************************/# ifndef gamecontrol_h # define gamecontrol_henum gamelevel {one, second, done}; Class gamecontroller {public: // constructorgamecontroller (const string & Title = "terminator (Sakura road edition)", const Position & winposition = P Osition (2.0, 2.0), const float winlength = 16, const float winheight = 13); // The title, location, window width, and height are used to initialize the game window. // destructor ~ Gamecontroller (); // inspectorssimplewindow * getwindow (); gamelevel currentlevel () const; // The checker obtains the window and the current level respectively. // facilitatorsvoid play (const gamelevel level ); // set the level void testgap (player * player, vector <gap *> GaP) where the game is located; // check whether the players in the game are in the basket (if they are in the basket, they can jump, out of the basket is not allowed) void testbullet (vector <enemy_1 *> E, vector <bullet *> B); void testbullet (vector <enemy_2 *> E, vector <bullet *> B); // checks whether the bullets in the game reach the enemy. If yes, the bullets and enemies are erased from the window. Bool testplayer (player * player, vector <enemy_1 *> E); bool testplayer (player * player, vector <enemy_2 *> E ); // check whether players in the game are caught by the enemy (if the enemy encounters a player, the game fails) int timertick (); // The function called by timercallback // players in the game window, enemies, this is where the bullet jumps are achieved by him. // But because this function is constantly called, we want to add a small prompt (Message) after the first pass. // The result is that the message segment is popped up, therefore, a global variable playone is used to ensure that the prompted function is executed only once. The plain white "magic number" may increase the difficulty of code reading. Private: // data memberssimplewindow * gamewindow; gamelevel level; // various roles in the game are controlled by gamecontroller. // Therefore, as a data member of the console, the player * player is included in the attributes of gamecontroller; vector <gap *> gaps; vector <Layer *> layers; vector <enemy_1 *> enemy1; vector <enemy_2 *> enemy2 ;}; # endif
Global classes: Global and welcome
/*************************************** **************************** // Resources. h /////// the end of the game is to display the interface resources //// you wanted to and welreources. H is put together, because the windows are independent from the game windows // is not used as a data member of the gamecontrol, but a global variable is used ///, but it may be because of the game. CPP and gamecontroll. CPP must contain, an error occurs ///*********************************** * *********************************/# ifndef wel_h # define wel_h // included in game. CPP is equivalent to the global variable // The Int timercallback (void); // and INT mouseclick (const Position & mouseposition ); you can easily use simplewindow end ("terminator (cherry blossom)", 15.0, 12.0, position (2.0, 2.0); bitmap welldone (end); bitmap quit (end ); bitmap fail (end); void setwindows () {fail. load ("BMP \ fail.bmp"); Assert (fail. getstatus () = bitmapokay); welldone. load ("BMP \ done.bmp"); Assert (welldone. getstatus () = bitmapokay); double width = fail. getwidth (); double height = fail. getheight (); quit. load ("BMP \ quit.bmp"); quit. setposition (position (0.6 * width, 0.8 * Height); Assert (quit. getstatus () = bitmapokay);} int mouseclickend (const Position & mouseposition) {If (quit. isinside (mouseposition) {end. close () ;}return 1 ;}# endif
Welcome. h
/*************************************** **************** // Welresources. h //// is not a separate class. It is the welcome page when a player starts to enter the game *. H files are included in game. as a global variable in CPP ///******************************** * *********************/# ifndef welcome_h # define welcome_h # include "assert. H "simplewindow welcome (" terminator (cherry blossom) ", 15.0, 12.0, position (2.0, 2.0); bitmap playbutton (welcome); bitmap instructionbutton (welcome ); bitmap instruction (welcome); void setwelcomewindows () {bitmap welcomebmp (welcome); welcomebmp. setposition (position (0, 0); welcomebmp. load ("BMP \ hello.bmp"); welcomebmp. draw (); Assert (welcomebmp. getstatus () = bitmapokay); double width = welcomebmp. getwidth (); double height = welcomebmp. getheight (); playbutton. setposition (position (0.1 * width, 0.85 * Height); playbutton. load ("BMP \ play.bmp"); Assert (playbutton. getstatus () = bitmapokay); instruction. setposition (position (0, 0); instruction. load ("BMP \ in.bmp"); Assert (instruction. getstatus () = bitmapokay); instructionbutton. setposition (position (0.1 * width, 0.35 * Height); instructionbutton. load ("BMP \ inbutton.bmp"); Assert (instructionbutton. getstatus () = bitmapokay); instructionbutton. draw () ;}# endif

[Logical block diagram of game control]


[Game Description]This game does not require installation. Open the file folder “yingmu.exe and double-click “yingmu.exe to start the game.
When the game is opened, the "enter" window is displayed. a screenshot of a slam dunk photo is displayed. Click the "Instruction" icon in the window to view the specific game operation instructions. Then, click the "play" icon to start the game.
After the game starts, the player can see the cherry blossom road in the lower left corner and three enemies that keep moving in the window. The Player uses the keyboard's left and right keys to control the cherry blossom road moving in the window, when you go to the basket, you can have up or down key control to achieve the layer jump. In the game, players press the space bar key to launch a bullet (there is no limit on the number of bullets). When a bullet encounters an enemy, it can destroy the enemy. When all enemies are eliminated, a dialog box is displayed, prompting players to enter the next level. Second, after all enemies are destroyed, the game wins. Players will see the congratulations window. Click the "quit" icon in the lower left corner to exit the game. If the game fails when the player is accidentally caught by the enemy, a page indicating a failed game appears. Click the "quit" icon to exit the game. [Game]

[Project Summary]

After studying the freshman C ++ language, our team completed the first project programming-terminator (Sakura road edition ).

After receiving the task, we quickly started ......

First, we checked the relevant information online, downloaded some related games for trial use, tried to figure out the game design architecture and function implementation, and learned the strengths of others. Next, let's design the terminator of the course design. Our initial design was to divide the interface into several layers. Players can move around on each layer and move around through the arrow keys. When they encounter a stair, they can climb up or down, the Space key launches bullets and has a variety of different foods. Players control the villain to "eat" and different foods increase or decrease the corresponding attributes (such as the life value and speed ). However, when it comes to practice, it turns out that coding is far less simple than reading code. We work in a division of labor. Two people write different classes, almost from scratch, and it seems very unfamiliar and difficult to decide and implement the functions of each class, it is especially important to sum up the necessary attributes and actions of an object when it is derived. This is just a difficult start. When we put the scattered Code together, we finally realized that the so-called "Object-Oriented" is not as simple as we think. Although we have carefully opened the bughunt program in the book, we do not understand it in many places, especially the importance of gamecontroller-connecting isolated objects, what can communicate with each other is the real "active" object, which forms a vivid game. We spent a few nights, rewrote many times, cut out a lot of content that was ambitious to achieve at first, so we came up with the prototype of Terminator.
Later, we carefully added some features in the game, such as the bullet launching, the "moving" effect of Sakura wood, and image processing ...... The game's entry and exit interfaces were also completed in this period.
In the early stage of the game's final production, we tried it in the lab and found a problem: different screens and resolutions caused the offset of the window and image coordinates, we initially set coordinates on our own computers, and numbers that are used directly due to laziness-quickly show their drawbacks, and images even overlap on lab computers. We immediately thought of making the game into two versions (standard screen and wide screen), but later we found that the resolution will also affect the coordinates, so the two versions cannot be implemented. We also consulted other people to check the information, and finally used the image size to adjust the coordinates between layers (because it seems that different screen images remain unchanged ), however, the problem has not been fundamentally solved, and the result is not as good as expected. This makes us especially think that we can comfort ourselves-the overall aesthetics of the game is greatly reduced, we hope that we can better solve this problem and upgrade and update the game in the future.
The advent of the game is not what you expect ......

Now let's look back at the first game we wrote. Even with the "awesome" mentality, we still feel that she has so many unsatisfactory aspects. After all, the final result is a bit simple, it is too far away from the original idea, and many features are not implemented-Because ezwin has limited functionality, adding game backgrounds or even moving on a "ladder, when there were obvious flashcharts, they were changed to jumps. The dead bullets and enemies were cleverly hidden in the game diagram, and there was no real Delete to release the memory. The time was limited, and there is no "Life Value" or "Speed" on the interface ...... When we begin to implement the program architecture, we lack rigorous logic thinking, and our ideas are too messy. As a result, some functions are too complicated to implement, the program is bloated and not conducive to reading.
We have gained a lot since the January S ......

In this course design, we deeply understand the importance of debugging. Many problems are found in compilation and connection. As a result, we can see that many errors occur when compiling our own program, confidence is certainly under a blow, but through repeated modifications, we still have a sense of accomplishment when looking at the decrease in the number of errors. In general, the time for debugging is far more than the time for coding.
Through the course design, we not only train our practical ability, but also cultivate our teamwork ability. When you encounter problems, we will discuss with each other, consult with others, and finally find a solution. I think this should be the best learning process and improve ourselves in practice, it is much more efficient than reading books. At the same time, we also realized that the knowledge in books is far from enough for programming. Textbooks only introduce some things, but they just guide you to get started. We need to take the initiative to learn. It is a good way to build projects together.
We know that we are too far and too far away, and we have to go a long and long way ......

Although the terminator (Sakuragi flower road edition), a game produced by our group, is somewhat rough and has very limited functions, it serves as the entire framework and system of the game, it enables us to use the object-oriented thinking to design games. In the compilation and later program debugging and troubleshooting processes, we also practiced our proficiency in the C ++ language and accumulated experience in solving common problems. What's more, he makes us deeply aware of our own gap-we have been studying for a year and have never used MFC; we cannot master HGE quickly in a short time; even if we refer to others' code, read a word or a word ...... This ordinary program may be a true portrayal of our poor knowledge. However, this is by no means the end point. We will try our best to improve ourselves in the future, strive to create excellent works.
This is the first trial of our software learning and the achievement of our freshman object-oriented programming study. I believe that each of us will never forget it ...... Reprinted please indicate the source: http://blog.csdn.net/xiaowei_cqu/article/details/7747650

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.