C # Basic Video tutorial 7.3 How to write a simple game

Source: Internet
Author: User
Tags control characters

In the front we basically realized the bird's lap track (in fact, if you do not do this, you can use two rectangular block collision detection instead of the same), as with all previous tutorials, the draft will be designed to encapsulate a class. At least so far we have known the bird's properties and methods, first of all, do not control how the method is implemented, and regardless of the method input and output, we have to design about a few methods.

How to initialize a bird-import and export some polygon points based on the previous section (initializing here only needs to import a bunch of points on the line)

Bird destruction methods-like a bird hitting a pipe or underground, the whole game starts again, and the bird must start over again.

Operating methods-for example, you can define whether a bird's movement is affected by gravity, or it can be defined as stationary or moving forward at a constant speed.

Flight method-different from the previous operating method, the flight method is controlled by the player, you click the button he flew (there is time interval, will not always be triggered)

You can also define a lot of extra methods (most of the cool-running games have bullets to control characters, bigger and smaller, invincible, violent, etc, which can be defined in this class)

?

Think of how the initialization method is implemented

What the incoming type needs to be: I pass the whole form past, at least the width and height of the entire form, so that if the game's interface changes, I initialize the position of the spawn bird to always remain in the middle of the screen, such as the position

What this method needs to do: 1 to import a bunch of track points and generate into the point array so that the main window is ready to draw 2 set the initial zoom and pan position

This method also needs to do: regardless of how to control the movement of birds (transform_x and transform_y), to refresh the true locus point position (together with the change)

?

In particular, it is important to note that the points that are imported are fixed dead spots, which should change with the movement of the birds. So either the Fly method or the Run method, or to test the convenient manual modification of the location Movex and Movey methods, you should refresh the polygonlistaftermoving point array. In order to verify that each refresh point is really what we need, it is best to paint the trajectory in a different color in the main program (this is also an important way to improve the efficiency of programming, think of some ways to visualize what you want to verify)

?

Because Flapperbird's game is like a cool run, a more scientific approach is that the scene is constantly producing tubes (width and height are random), and the pipe continues to run from one end of the screen to the other, the bird seems to have been flying forward, in fact, the bird is not moving back and forth. If you do not control the bird, the bird will always fall down, if you let the bird fly, you can go up a little. Then this down method is very simple to achieve, note that we are alone in a thread to execute, each time to his transform_y accumulated a certain distance (the front with a try catch is a relatively lazy method, the first run, then the thread has not been instantiated can not be terminated, If it is already running, then the thread can be terminated, whether it is first run or not, and the simplest violence.

?

The flight method is a little more complicated, to be divided into several small pieces to analyze.

First of all to understand how to prevent the recurrence of the flight method, we define a variable ifflying, as long as the flight is being performed this variable is true, the execution is set to false, if triggered more than once, the first look is true, is true does not go down. Of course, in the course of the flight also to determine whether the game is over, if the game can not fly again (such as hit something, how to define the game end bird This class should not tube, he should be the standard method, only need to pass a few variables tell him what to do, the bird is not the referee, not responsible for the specific game rules)

Second, to figure out how to fly, assuming that each flight to 1 seconds or 2 seconds, this time period we every 20ms (also can set their own time) to modify the value of transform_y, this can not be stacked as before a fixed value, looks too fake, as you jump or run, There's always an acceleration-deceleration process. So you can use some parabolic, trigonometric curve to simulate (these values are tested, for the game's dynamic, the reader can search the very real-life function to make the birds fly more like the real)

?

After the bird analysis, and then to see how to do the pipe (we have not considered the pipe into a polygonal trajectory point, only need to generate a rectangle), compared to the bird, the rectangle class properties and methods are different, can be copied after the change (we redefine the width of the rectangle, height, The minimum maximum value for x and Y is used for collision detection, because if a point is judged to be within a rectangle, it is only necessary to determine whether the x-coordinate and y-coordinate are separate between xmin-xmax/ymin-ymax.

?

When the pipe is initialized, the width and height are random values, but must be generated on the side of the screen (for testing to make a few buttons to repeat the generation, to see if the effect is satisfactory, and then adjust the parameters of the random values)

?

Other methods are identical, each time the rectangle runs to refresh its own boundary points (as the polygon refreshes all point arrays), in addition to the pipe only a translation method, there is no fly method, this is more simple than the bird

?

With the bird, the tube, the next thing you need to do is define a referee. I made an extra class called Gamesense, and he actually did the work of drawing (which should be done with an extra class to paint), and the referee's most important thing was to decide whether to hit. Note that I am here to define the input and output of the method, because the game is relatively simple, collision needs to determine whether a polygon instance with a rectangular instance has a collision, the internal implementation method is very simple, traversing the point array of the polygon instance to see if he is within the rectangle of four corner points.

?

Test no matter which element to move, as long as two areas have contact, the collision State does respond

?

Let's take a look at the drawing process, the Form1_paint event, each time the drawing is done, one thing needs to be reset the brush (the so-called reset brush refers to the way you once used the Translatetransform,scaletransform,rotatetransform method , you can think of you holding a pen in the upper left corner of the screen x=0,y=0 position, each translation of the brush will be moved to a new position, each rotation of your brush will follow the rotation, the concept here we in the Application tutorial OpenGL simulation will also talk about, need a certain space imagination ability, It is more necessary for the reader to verify the entire process of the drawing, such as drawing a circle first, and then drawing a circle tangent to him.

?

?

For more instructional videos and downloads, please pay attention to the following information:

My Youku Space:

Http://i.youku.com/acetaohai123

?

My online forum:

http://csrobot.gz01.bdysite.com/

?

Problem Exchange:

qq:910358960

Email:[email protected]

?

?

?

?

?

?

C # Basic Video tutorial 7.3 How to write a simple game

Related Article

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.