These days began to find a job in Shenzhen, thinking about the past some of the technology has been forgotten before the temperature. See the respected "Preach Wisdom podcast" There is a public class, speaking of C # to write Flappybird games, also on their own search of the game resources, also to try.
In fact, the technology used is the Preach Intelligence podcast. But the project structure and some logic slightly modified, more in line with the original game features.
———————— re-declare: Not my original plan ————————————
Importing resources
The form background is set to background picture 382*681, then add a PictureBox on the form, put the road picture, the size stretch.
Then add three timer, set 100ms,10ms,10ms
In this way, the game interface is built.
Here is the code structure, the parent class of all game elements, abstract class: Public abstract class Gameobj
Small birds: public class Bird:gameobj
Pipeline class: public class Pipe:gameobj
Gravity Class: public static Class Gravity
Gameobj class: Add two abstract methods void Move () and void Draw (Graphics g)
The following is the code for the entire class:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Drawing;namespace flappybird{///<summary>// The parent of the game object/// </summary> public abstract class Gameobj { public int X {get; set;} public int Y {get; set;} public int Height {get; set;} public int Width {get; set;} Public Gameobj (Int. x,int y,int height,int width) {this . x = x; This. y = y; This. Height = height; This. width = width; } public abstract void Move (); public abstract void Draw (Graphics g);} }
The following is the bird class Source:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Drawing;using Flappybird. Properties;namespace flappybird{public class Bird:gameobj {//<summary>//various bird morphology arrays </summary> private static image[] IMGs ={Properties.Resources.bird_blue_0, Properties.Resources.bird_blue_1, Properties.Resources.bird_blue_2 }; <summary>/////</summary> public int Birdindex {get; set;} <summary>////store current drop speed///</summary> public float tempo {get; set;} <summary>///Store drop duration///</summary> public float time {get; set;} private static Bird Bird = null; <summary>////Singleton mode to ensure only one bird object is instantiated///</summary>//<returns></returns> public static Bird Getsinglebird () {if (Bird = = null) { Bird = new Bird (100, 200, 0); } return bird; }///<summary>///Call the constructor of the parent class///</summary>//<param name= "x" ></param& Gt <param name= "y" ></param>//<param name= "Birdindex" ></param> private Bird (int x , int y, int birdindex): base (x, Y, imgs[0]. Height, Imgs[0]. Width) {this. Birdindex = Birdindex; This. Time = 10f; This. Speed = 0f; }///<summary>//Override abstract Draw method for parent class///</summary> public override void Draw (Graphic S g) {switch (this). Birdindex) {case 0:g.drawimage (imgs[0], this. X, this. Y); Break Case 1:g.drawimage (imgs[1], thIs. X, this. Y); Break Case 2:g.drawimage (imgs[2), this. X, this. Y); Break Default:break; } this. birdindex++; This. Birdindex%= 3; }///<summary>//Override the parent class's abstract Move method////</summary> public override void Move () {this. Y = this. Y <= 60? 0:this. Y-60; This. Y-= 30; }///<summary>///Get the graphics area for collision detection///</summary>//<RETURNS></RETURNS&G T public static Rectangle Getrectangle () {return new Rectangle (bird. X, Bird. Y, Resources.bird_blue_0.Width, Resources.bird_blue_0.Height); } }}
Combined with comments, I believe we can understand ...
Pipe type:
In this class, I define a pipes array to render 3 rows of water pipes in the screen (each column contains up to two water pipes)
It is then initialized with the Initpipes () function. In this function, a method of comparing flattering is used to determine whether the last element in the array is null, similar to the singleton pattern used in the bird class.
Specific code:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Drawing;using Flappybird. Properties;namespace flappybird{public class Pipe:gameobj {public override void Move () { for (int i = 0; i < 3; i++) {pipes[i, 0]. X-= 1; Pipes[i, 1]. X-= 1; }//this. X-= 1; public override void Draw (System.Drawing.Graphics g) {for (int i = 0; i < 3; i++) {//Draw the upper pipe g.drawimage (resources.bound_up, pipes[i, 0]. X, Pipes[i, 0]. Y); Draw down the pipe g.drawimage (Resources.bound_down, Pipes[i, 1]. X, Pipes[i, 1]. Y); }} private Pipe (int x, int y): base (x, Y, Resources.bound_down. Height, Resources.bound_down. Width) {} private static pipe[,] pipes = new pipe[3, 2]; private static void Initpipes () { if (pipes[2, 1] = = NULL) {Random Rand = new Random (DateTime.Now.Millisecond); for (int i = 0; i < 3; i++) {int y = rand. Next (80, 380); Pipe Pipes[i, 0] = new pipe (+ i *,-y); Bottom pipe pipes[i, 1] = new pipe (+ i *, y + resources.bound_up. Height + 170); }}} public static Pipe getpipes (int row, int col) {if (Row > 1 | | col ; 2) {return null; } initpipes (); Return pipes[col, row]; public static Rectangle getrectangle (int row,int col) {return new Rectangle (Pipes[col, row]. X, Pipes[col, row]. Y, Resources.bound_up. Width, Resources.bound_up. Height); } }}
Then the Gravity class:
Using system;using system.collections.generic;using system.linq;using system.text;namespace flappybird{public static Class Gravity {Private Const float g=9.8f; Private Const FLOAT A = 40f; <summary>///To get the whereabouts of//</summary>//<param name= "Time" ></param> <param name= "Speed" ></param>///<returns></returns> public static int Getdro Pheight (float time,float speed) {if (speed<0) {return (int) (0 .5f * A * time * time + speed * time); } return (int) (0.5f * g * time * time + speed * time); }///<summary>///Get down the speed///</summary>//<param name= "Time" ></para m>//<param name= "Speed" ></param>///<returns></returns> public static Float Getdropspeed (float time,float speed) {if (speed<0) {return speed + A * time; } return speed + G * time; } }}
defines a float A; This indicates the downward acceleration that the bird receives when it flies upward. This design, is for the birds to fly up, not a bit to jump to a certain height, let it rise continuously, but the effect of deceleration express.
I see the Preach Wisdom Podcast in the teacher Zhao is directly set up a rise in height, give the feeling is a bird to "fly" up, so the design will be simple, the disadvantage is that there is no continuous effect.
Here comes the feast, Form1 code:
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;using flappybird. Properties;namespace flappybird{public partial class Form1:form {public Form1 () {Initi Alizecomponent (); } private bool Isgameover = false; private void Form1_Load (object sender, EventArgs e) {timer1. Enabled = true; Timer2. Enabled = true; Timer3. Enabled = true; private void Form1_paint (object sender, PaintEventArgs e) {if (isgameover) { Return } bird.getsinglebird (). Draw (E.graphics); Pipe.getpipes (0, 0). Draw (E.graphics); private void Timer1_Tick (object sender, EventArgs e) {if (isgameover) { Return } this. Invalidate (); private void Form1_mouseclick (object sender, MouseEventArgs e) {if (isgameover) { Return }//bird.getsinglebird (). Move (); Bird.getsinglebird (). Speed = -80f; private void Form1_keypress (object sender, KeyPressEventArgs e) {} private void Form1_keydo WN (object sender, KeyEventArgs e) {if (isgameover) {return; } if (E.keycode = = keys.space) {//bird.getsinglebird (). Move (); Bird.getsinglebird (). Speed = -80f; }}///<summary>//Calculate gravity impact///</summary>//<param name= "Sender" > </param>//<param name= "E" ></param> private void Timer2_tick (object sender, EventArgs e) {if (isgameover) {return; } int DRopheight = Gravity.getdropheight (Bird.getsinglebird (). Time * 0.01f, Bird.getsinglebird (). Speed); int currenty = Bird.getsinglebird (). Y + dropheight; int maxy = Clientrectangle.height-picturebox1.height-bird.getsinglebird (). Height; if (currenty >= maxy) {Bird.getsinglebird (). Y = Maxy; } else if (CurrentY < 0) {Bird.getsinglebird (). Y = 0; } else {Bird.getsinglebird (). Y = CurrentY; } bird.getsinglebird (). Speed = Gravity.getdropspeed (Bird.getsinglebird (). Time * 0.01f, Bird.getsinglebird (). Speed); } Random rand = new Random (DateTime.Now.Millisecond); private void Timer3_tick (object sender, EventArgs e) {if (isgameover) {retur N } pipe.getpipes (0, 0). Move (); The pipe column closest to the left is prepared for the following collision detection. Because only the left-most tubeCollision detection is required for the road. Avoid unnecessary actions. int left = 0; for (int i = 0; i < 3; i++) {//If a column of pipes is left out of the screen range, place it outside the right end of the screen//and then move left if (pipe.getpipes (0, I). X <-90) {//If I column is removed from the screen, then it is (i + 1)% 3 column on leftmost left = (i + 1)% 3; Console.WriteLine (left); int y = rand. Next (80, 380); Above the pipe pipe.getpipes (0, I). X = 500; Pipe.getpipes (0, I). Y =-y; The following pipe pipe.getpipes (1, i). X = 500; Pipe.getpipes (1, i). y = +-y + resources.bound_up. Height + 170; }} bool Isinterup = Bird.getrectangle (). Intersectswith (Pipe.getrectangle (0, left)); BOOL Isinterdown = Bird.getrectangle (). Intersectswith (Pipe.getrectangle (1, left)); BOOL Isinterground = Bird.getrectangle (). Intersectswith (New Rectangle (PictureboX1. Location, picturebox1.size)); if (isinterup| | isinterdown| | Isinterground) {timer1. Enabled = false; Timer2. Enabled = false; Timer3. Enabled = false; Isgameover = true; } } }}
Combined with the first few classes, here is left to everyone to understand it. (By no means am I too lazy to write instructions.) ha haha)
Portal: http://download.csdn.net/detail/geeking/8255401
C # version Flappybird not fully implemented