2. Introduction
• It is a simple game in which one bird is moving between
different obstacles.
• If the bird touches any obstacle then the game will be end
and the total score will show on the screen.
• Every time the bird crosses any obstacle the score
increases by 1.
3. Creating a Flappy Bird game in Visual
Studio Using C#
• Create Windows Form Application
• 4 Picture boxes,
• 1 Timer Object
• 4 Labels
6. Coding
the core variables for the game
bool jumping = false;
Int pipeSpeed = 5;
int gravity = 5;
Int Inscore = 0;
7. Set the label properties
• 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;
8. Functions
• We need 3 functions that will be triggered by various
events
• 1) Timer function
• 2) keydown function
• 3) key up function
• 4) game end function
10. gameTimer_Tick function
pipeBottom.Left -= pipeSpeed;
pipeTop.Left -= pipeSpeed;
flappyBird.Top += gravity;
scoreText.Text = " " + Inscore; //for scores
This will scroll the pipes to the left and drop the bird using
the gravity variable.
11. The bounds property
• Ending game
• 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.
12. Enter the following code in the timer
function
if (flappyBird.Bounds.IntersectsWith(ground.Bounds))
{
endGame();
}
elseif (flappyBird.Bounds.IntersectsWith(pipeBottom.Bounds))
{
endGame();
}
elseif (flappyBird.Bounds.IntersectsWith(pipeTop.Bounds))
{
endGame();
}
gameTimer.Stop();
13. Regenerating pipes
Enter the following code in the game timer function
if (pipeBottom.Left< -80)
{
pipeBottom.Left = 1000;
Inscore += 1; //score add
}
elseif (pipeTop.Left< -95)
{
pipeTop.Left = 1100;
Inscore += 1; //score add
}
The code above checks whether the pipes have left the screen
and gone beyond -80px to the left.
15. Code of key down function & keyUp
• this will reverse the gravity and make the character jump.
if (e.KeyCode == Keys.Space)
{
jumping = true;
gravity = -7;
}
• 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;
}
16. endGame()
• create a function to be used when we want to the end the
game.
private void endGame()
{
gameTimer.Stop();
endText1.Visible = true;
endText2.Visible = true;
gameDesigner.Visible = true;
}
17. Full 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;
namespace Flappy_Bird
{
public partial class Game : Form
{
bool jumping = false;
int pipeSpeed = 5;
int gravity = 5;
int Inscore = 3;
public Game()
{
InitializeComponent();
endText1.Text = "Game Over!";
endText2.Text = "Your final score is: " + Inscore;
gameDesigner.Text = "Game Designed Hamza Gulistan";
endText1.Visible = false;
endText2.Visible = false;
gameDesigner.Visible = false;
}
}