First pure OO feel

xiaoxiao2021-03-14  186

First of all, I have to explain first: I here's so-called OO feel is very beautiful, I have not done a big project, and I have not experienced software maintenance. In fact, I am looking forward to I can have a big project experience, maybe it will be more beneficial for the establishment of object-oriented ways!

At the end of February, I started to be a game I think more complete. It is a game similar to "Thunderbia". I will join as many elements as possible. But later when the objects in the game are more and more, the entire project begins to change. I immediately realized that it is increasingly difficult to design, the code architecture begins to look forward to a better design method!

In the past, there would be some point in my game program. I have a target-oriented shadow. The class in the game program seems to have no reuse value ----- Unless you have a similar object in another game! ---- Oh, the same subject will be very bored! ----- I only designed a class, a single class, no inheritance, no polymorphism, and no overload. Now look at it, the coupling between the class is also high (even in some early things, the image processing section and the game logic part are put together)! --- It is a pile of garbage code!

When I realized this, I started to learn C in depth. At the same time, I also started another study subject --- mfc. While learning MFC, you know that there is a big pile of virtual functions, polymorphism, and then I have summarized my "Summary for polymorphism --- virtual functions .

After reading more and more C and oo ------ I have never exposed to UML ----- I gradually had some experience. "Write a program in the world, knowing the world is programming." How wonderful it will be!

Then I roughly saw an elevator case in "C Programming Classic" - "That is an example of a few chapters, I am depressed. In preliminary understanding, then start my simulation program: analog airplane shooting game!

I try to change my thinking. I think, I got some effect!

Look at the following documents:

//

2006.3.14

Recently, the contact face is more frequent, with some experience, decided to put this kind of experience or

Is a kind of ideology.

This is a plane shooting game program. I will use the object as much as possible to depict this game.

(If you analyze , I want me to build this program will be more responsible.)

1. Write a problem description:

This is an simulation program for aircraft shooting game. There is a player plane in the game, and the bullets generated by many players.

There are also many enemy aircraft, and bullets generated by many enemy aircraft. Player aircraft receives players enter, thus making players aircraft

Generate a corresponding action, such as moving and launching a bullet. The enemy plane is randomly generated. Player plane and enemy plane

It will produce a lot of bullets. The bullets of the player plane attacked the enemy, and the corresponding enemy disappeared; while the enemy plane bullet attack

When the player plane, or the enemy plane hits the player plane, the player is dead. (To simplify the problem, suppose that players in the game have only one life - that is, the player is an enemy

The human bullet attacks ends - this will also be the end of this program).

Since it is analog program, there is a simulator in the program, which is used to simulate players - "of course the result is random -

2. Identify the class in the system:

A player aircraft, enemy aircraft, player bullets, enemy bullets, simulated classes, and all consisting of all games.

3. Identify the object:

A player aircraft object, many enemy aircraft objects, many players bullets, many enemy bullets,

A simulator object, a game object.

4. Identify class properties:

Player plane object: coordinate, current motion status (which direction moves, whether to launch bullets), current life status, X speed, Y speed enemy aircraft: coordinate, current motion status (in which direction moves, whether to launch bullets), current life State, X speed, y speed

(Different parts in that the motion status of the player plane is determined by the player, and the enemy aircraft movement is

Its own decision)

Player bullets: coordinates, life status (whether there is disappearance), Y speed

Enemy bullet: coordinate, life status (whether there is no disappearance), Y speed

Analog class: Randomly generated key value ----- only used to simulate players

Game class: State (start, end), have various objects

5. Identify class behavior:

Player plane: Mobile, receive players enter, painting yourself, launch bullets,

Enemy plane: move, painting yourself, launch bullets

Player bullets: move, painting yourself, testing if you hit enemies

The enemy bullet: move, painting yourself, testing if hitting players

Analog class: generate key values

Game class: The operation of each object of the game,

6. Category level:

A base class COBJECT, Secondary School Class CPLANE, CBULLET, Level 3 Party Class CPLAYERPLANE, CENEMYPLANE

, CPlayerbullet, CENEMYBULLET

Simulation class

Game class, composed of CPlayerPlane, CPlayerbullet, Cenemyplane, Cenemybullet, CModel class.

2006.3.16

Feel this simulation process, it is difficult to design before writing code, once it is determined,

It is not difficult to encode.

2006.3.16

21:29

It has been tested to complete the enemy aircraft class and player classes, not intended to make the bullets! The main purpose is to use this

The simulation project works for a few days.

Basically, I feel that the difficulty part is to serve the document, especially when describing the interaction between classes and classes, is more complicated. When the encoding is, there is not much difficult problem as long as the document is compared.

PS: 2006.3.19

Based on itself, I picked it back in the item at the end of February. I have to do it, I have to do it, my belief is strong!

I follow the OOD steps I just learned, write the document from all kinds of clamps, to model each class, one by one. When I describe the behavior of each class, I am naturally described: Player Aircraft Class: Move, accept the player input, can notify the game class object to create a player's bullet object, can tell the game object how to paint yourself (and then the game class object tells the renderer object how to draw it .....

Isn't this OO thinking? These entity objects, they are doing this in the real world --- placed in the program, I still use the code to depict their characteristics!

This is really great! In the next mating description document, I am getting more and more oo, I really feel that I am referring to writing the program, but ----- talking a story, the story How do each role interact with other characters!

Up to now, this program has been completed! All parts have been manufactured, the rest is to connect these parts with the game class object (the object defined in the program). ------ really very As the motherboard in the computer, I installed those parts above, then this "motherboard" will also let all "accessories" communicate with each other! Every "accessories" will work together by this "motherboard" to constitute a performance Strong PC! I let all the contact between "accessories" and "accessories" disappear! This "accessories" should communicate with another "accessories", good, please, please inform "motherboard", "motherboard" will Help you transcribe your request!

This is really wonderful!

Now I saw the code of my last game project, I feel rough, with garbage. Haha, I am really progress!

转载请注明原文地址:https://www.9cbs.com/read-129272.html

New Post(0)