C# (Sharp) Tutorial - Create A Flappy Bird Game Using C#
C# (Sharp) Tutorial - Create A Flappy Bird Game Using C#
We need a timer to animate the objects, check for collision and to know when to end the game.
now to add the pictures in the right picture boxes
here are the pictures for flappy bird
Now go through each picture and set them to their appropriate spots.
set the end screen labels and set them to invisible for now.
endText1.Visible = false;
endText2.Visible = false;
gameDesigner.Visible = false;
Find the key down option and type in GameKeyDown, press enter
Do the same for 3) key up function
Find the key up option and type in GameKeyUp press enter
Last create a function to be used when we want to the end the game.
private void endGame()
{
}
Check the screen shot below
Character movements will be done through the key down and key up.
Starting with the game timer:
Inside the gameTimer_Tick function enter the following code
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:
More tutorials on www.mooict.com
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.
Add the following inside the end game function
gameTimer.Stop();
The code above checks whether the pipes have left the screen and gone beyond -80px to the left. If
it has then we change the pipes left position to the far right on the screen which creates an illusion
More tutorials on www.mooict.com
of animation and keeps the game going. Instead of creating and recreating the objects we recycle
the same one over and over.
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;
scoreText.Text = "" + Inscore;
Full Code:
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
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;
}
private void gameTimer_Tick(object sender, EventArgs e)
{
pipeBottom.Left -= pipeSpeed;
pipeTop.Left -= pipeSpeed;
flappyBird.Top += gravity;
scoreText.Text = "" + Inscore;
if (flappyBird.Bounds.IntersectsWith(ground.Bounds))
{
endGame();
}
else if (flappyBird.Bounds.IntersectsWith(pipeBottom.Bounds))
{
endGame();
}
else if (flappyBird.Bounds.IntersectsWith(pipeTop.Bounds))
{
endGame();
}
}
private void GameKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
jumping = true;
gravity = -5;
}
}
private void GameKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
jumping = false;
gravity = 5;
}
}
private void endGame()
{
gameTimer.Stop();
endText1.Visible = true;
endText2.Visible = true;
gameDesigner.Visible = true;
}
}
}