C Sharp Tutorial - Create A Flappy Bird Game Using C PDF
C Sharp Tutorial - Create A Flappy Bird Game Using C PDF
Its important to name them correctly because we will call them in the code.
its easier to call bird.Left and pipeBottom.Left than it is to pictureBox1.Left and PictureBox2.Left.
1) flappyBird
2) pipeTop
3) pipeBottom
4) ground
Now add the timer to the form. Its under the components tab in the tool box
We need a timer to animate the objects, check for collision and to know when to end the game.
Each of these images are in PNG format and they contain transparency.
Click on the first picture box and Click on the white triangle on top > choose images
Now go through each picture and set them to their appropriate spots.
You can use stretch image setting in the size mode to fit the pictures in the picture box.
We will hide the end game credits till the game ends.
you can format these labels to suit any colour, font or size you choose.
int pipeSpeed = 5;
int gravity = 5;
int Inscore = 0;
set the end screen labels and set them to invisible for now.
endText1.Visible = false;
endText2.Visible = false;
gameDesigner.Visible = false;
1) Timer function
3) key up function
Its simple just double click on the gametime and it will automatically add the function code
click on the form and click on the event button in the properties window
Last create a function to be used when we want to the end the game.
pipeBottom.Left -= pipeSpeed;
pipeTop.Left -= pipeSpeed;
flappyBird.Top += gravity;
This will scroll the pipes to the left and drop the bird using the gravity variable.
Each tick moves the picture boxes to left according to the speed we set in the pipe speed variable.
Enter the following code in the keydown function, this will reverse the gravity and make the
character jump.
if (e.KeyCode == Keys.Space)
jumping = true;
gravity = -5;
enter the following code in the key up function, this will enable gravity again to the character
if (e.KeyCode == Keys.Space)
jumping = false;
gravity = 5;
Now if you test it you will see the animations and key board controls happening.
Problems:
if (flappyBird.Bounds.IntersectsWith(ground.Bounds))
{
endGame();
}
else if (flappyBird.Bounds.IntersectsWith(pipeBottom.Bounds))
{
endGame();
}
else if (flappyBird.Bounds.IntersectsWith(pipeTop.Bounds))
{
endGame();
}
Bounds check for height and width of each of the picture box. Intersects with will check the height
and width of another picture against the first one and check to see if they are colliding.
Once the program determines that picture boxes are colliding with each other, we will end the
game.
gameTimer.Stop();
This code will manually stop the timer from running.
The timer controls everything in the game. If we stop that we can stop the whole game.
After the pipes left the screen we increase the points by one.
Problem 3.
To show the score on screen while playing enter the following under the flappyBird.Top += gravity;
endText1.Visible = true;
endText2.Visible = true;
gameDesigner.Visible = true;
etc
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;
namespace flappyBirdTutorial
{
public partial class Form1 : Form
{
bool jumping = false;
int pipeSpeed = 5;
int gravity = 5;
int Inscore = 0;
public Form1()
{
InitializeComponent();
endText1.Text = "Game Over!";
endText2.Text = "Your final score is: " + Inscore;
gameDesigner.Text = "Game Designed By your name here";
endText1.Visible = false;
endText2.Visible = false;
gameDesigner.Visible = false;
if (flappyBird.Bounds.IntersectsWith(ground.Bounds))
{
endGame();
}
else if (flappyBird.Bounds.IntersectsWith(pipeBottom.Bounds))
{
endGame();
}
else if (flappyBird.Bounds.IntersectsWith(pipeTop.Bounds))
}
}
}
}