The last introduction with GDI + wrote a verification Code image generator, this time to introduce the use of GDI + write before the popular time of the small game: Flappy Bird. Get acquainted with some of the simple uses of GDI + by writing this game.
This is a rough game screen, we do not mind ah, after all, this is the art of doing things:
Let's analyze how to write this game first. The game process is: 1, the bird keeps falling down, and the more the faster, 2, the Barrier column keeps appearing and moving to the left; 3, the game at the beginning of the bottom of the forward bar is constantly rotating.
Game rules: 1, the bird's body can not touch obstacles, 2, the bird's body can not touch the bottom and the upper; 3. When a bird crosses an obstacle, it counts the number of obstacles and adds 1.
Knowing the game process and the rules of the game, let's write the game:
Because of the height of the obstacles in the game, the width is uniform. We can write an obstacle class for creating an obstacle object with a bird object.
Class Diamonds {int _width = 40; <summary>///graphics width, default//</summary> public int Width {get {ret Urn _width; } set {_width = value;} } int _heigth; <summary>//Graphics high///</summary> public int Heigth {get {return _ _ Heigth; } set {_heigth = value;} } float _x; <summary>///x-axis coordinates///</summary> public float X {get {return _x; set//x axis coordinates cannot be less than 0 {if (value < 0) {value = 0; } _x = value; }} float _y; <summary>///y-axis coordinates cannot be greater than 300 or less than 0///</summary> public float Y {get { return _y; } set {if (Value > 400) {value = 400; } if (value < 0) {value = 0; } _y = value; }} float _speed = 0; <summary>///the speed at which coordinates are increased///The initial speed of the bird object//positive for rising speed//negative for descent//</summary> Public float Speed {get {return _speed;} set {_speed = value;} }///<summary>//change x axis coordinates///</summary> public void Changex () { if (this. x = = 0)//each x-axis is 0 o'clock width minus 1 {this. Width-= 1; } else {this. X-= 1; }}///<summary>//Change the y-axis///control the rise and fall speed of the Bird object///</summary> public void C Hangey () {//change the speed of the y-axis by first slowing down and then accelerating the increase of this. Y-= this. Speed; This. Speed-= 0.2f; } /// ≪summary>///constructor function///</summary>//<param name= "x" ></param>//< param name= "y" ></param>//<param name= "heigth" ></param> public Diamonds (float x, Floa T y, int heigth) {this. x = x; This. y = y; This. Heigth = Heigth; }
The class for creating obstacles and birds has been written, and there are ways to move around and move up and down.
Next start the process of writing the game:
First, draw the game diagram
Use GDI + to draw the game diagram and use the time controls to redraw the game graph continuously to produce dynamic effects.
First declare the global variables and objects:
Set the gap height between the barriers static int width = 100; Determine which forward bar to draw bool B = true; Counts the current number of obstacles through int count = 0; int _maxcount = 0; <summary>///record the maximum number of passes///</summary> public int MaxCount {get {R Eturn _maxcount; } set {_maxcount = value;} }//Create canvas, size 300*400 static Bitmap bmp = new Bitmap (300, 400); static Bitmap bmp1 = new Bitmap (300, 20); Create a pen, draw a bird static pen penone = new Pen (brushes.red, 5);//Create a pen, draw the forward bar static pen pentwo = new Pen (color.black , 2); Create bird static Diamonds bird = new Diamonds (100, 190, 15); Create random number generator static random r = new Random (); Create GDI + objects from canvas bmp static Graphics GP = Graphics.fromimage (BMP); static Graphics GP1 = Graphics.fromimage (BMP1); Create an object of two obstructions static int up1height = R.next (80, 241); Diamonds up1 = new Diamonds (0, up1height);Diamonds down1 = new Diamonds (width + up1height, 400-width-up1height); static int up2height = R.next (80, 241); Diamonds up2 = new Diamonds (0, up1height); Diamonds down2 = new Diamonds (, Width + up1height, 400-width-up1height);
To draw a game chart:
Birds Fly Bird. Changey (); Obstacle movement Up1. Changex (); Down1. Changex (); Up2. Changex (); Down2. Changex (); Determine if the game is over Isend (); Statistics by number passcount (); Clear the picture GP. Clear (Color.Blue); If an obstacle disappears, a new obstacle if (Up1) appears. Width = = 0) {up1height = R.next (80, 241); Up1 = new Diamonds (0, up1height); Down1 = new Diamonds (+, Width + up1height, 400-width-up1height); }//If an obstacle disappears, a new obstacle if (UP2) appears. Width = = 0) {up2height = R.next (80, 241); up2 = new Diamonds (0, up2height); DOWN2 = new Diamonds (+, Width + up2height, 400-width-up2height); }//Draw out individual objects//bird GP. DrawRectangle (Penone, Bird. X, Bird. Y, Bird. Width-10, Bird. Heigth); Handicap GP. FillrecTangle (Brushes.green, up1. X, Up1. Y, Up1. Width, Up1. Heigth); Gp. FillRectangle (Brushes.green, Down1. X, Down1. Y, Down1. Width, Down1. Heigth); Gp. FillRectangle (Brushes.green, up2. X, Up2. Y, Up2. Width, Up2. Heigth); Gp. FillRectangle (Brushes.green, Down2. X, Down2. Y, Down2. Width, Down2. Heigth); Give the picture to the picture frame pbximage.image = bmp;} <summary>///Screen forward bar operation///</summary>//<param name= "Sender" ></param> <param name= "E" ></param> private void Timer2_tick (object sender, EventArgs e) { Gp1. Clear (Color.Blue); Draw forward Bar Gp1. DrawLine (pentwo, 0, 0, 300, 0); Gp1. DrawLine (pentwo, 0, 20, 300, 20); if (b) {for (int i = 0; i <; i + =) {Gp1. FillRectangle (Brushes.green, I, 0, 10, 20); } B = false; } else {for (int i = ten; I <; i + =) {Gp1. FillRectangle (Brushes.green, I, 0, 10, 20); } B = true; } pbxlines.image = BMP1; }///<summary>//////</summary> private void Isend () {//due to bird body Body line thickness problem, the width of the corresponding increase of 5 pixels or so, the high increase of 2 pixels if ((bird. X + >= up1. X) && (bird. X <= up1. X + () && (bird. Y <= up1. Heigth + 5) | | (Bird. Y >= up1. Heigth + width-20))//When the bird head enters the obstacle 1 then ends {Gameover (); } else if ((bird. X + >= up2. X) && (bird. X <= up2. X + () && (bird. Y <= up2. Heigth + 5) | | (Bird. Y >= up2. Heigth + width-20))//When the bird head enters the obstacle 2 then ends {Gameover (); } else if (bird. Y >= 375 | | Bird. Y <= 0)//When the birds meet or touch the ground {gameover (); } } <summary>///end of Game judgment///</summary> private void Gameover () {/ /Display Control timer1. Enabled = false; Lblend.visible = true; Btnstart.visible = true; Lblmaxcount.visible = true; Label1. Visible = true; Get the highest score if (this. MaxCount < count) {this. MaxCount = count; } Lblmaxcount.text = this. Maxcount.tostring (); Return }///<summary>///Calculate the number of passes///</summary> private void Passcount () { When the end of the obstacle x-axis passes over the bird, it passes through an if (up1. X = = 65 | | Up2. X = =) {count++; } Lblcount.text = count. ToString (); }///<summary>//<param name= "Sender" ></param& Gt <param name= "E" ></param> private voidbtnStart_Click (object sender, EventArgs e) {//Start, end control not visible btnstart.visible = false; Lblend.visible = false; Lblmaxcount.visible = false; Label1. Visible = false; Btnstart.text = "Start Again"; The current quantity is zeroed by Count = 0; Reinitialize Picture bird = new Diamonds (100, 190, 15); Up1height = R.next (80, 241); Up1 = new Diamonds (0, up1height); Down1 = new Diamonds (+, Width + up1height, 400-width-up1height); Up2height = R.next (80, 241); up2 = new Diamonds (0, up1height); DOWN2 = new Diamonds (, Width + up1height, 400-width-up1height); Activates the timer1. Enabled = true; }///<summary>///Click Bird Fly///</summary>//<param name= "Sender" ></param >//<param name= "E" ></param> private void Pbximage_click (object sender, EventarGS e) {//After debugging, this speed can play higher bird. Speed = 4.5f; }///<summary>///double-click Accelerated Ascent//</summary>/<param name= "Sender" ></param> <param name= "E" ></param> private void Pbximage_doubleclick (object sender, EventArgs e) {Bird. Speed = 7; }