flappy bird game in c#

19
FLAPPY BIRD GAME By: Hamza Gulistan Reg no: FA14-Bcs-027

Upload: comstas

Post on 14-Apr-2017

183 views

Category:

Education


4 download

TRANSCRIPT

Page 1: Flappy bird game in c#

FLAPPY BIRD GAMEBy: Hamza GulistanReg no: FA14-Bcs-027

Page 2: Flappy bird game in c#

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.

Page 3: Flappy bird game in c#

Creating a Flappy Bird game in Visual Studio Using C#• Create Windows Form Application• 4 Picture boxes,• 1 Timer Object • 4 Labels

Page 4: Flappy bird game in c#

NamingName Text Property

PictureBox1 flappyBird

PictureBox2 pipeTop

PictureBox3 pipeBottom

PictureBox4 ground

label1 label2label3 label4

scoreTextendText1endText2gameDesigner

timer1 gameTimer

Page 5: Flappy bird game in c#
Page 6: Flappy bird game in c#

Codingthe core variables for the gamebool jumping = false;Int pipeSpeed = 5;int gravity = 5;Int Inscore = 0;

Page 7: Flappy bird game in c#

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;

Page 8: Flappy bird game in c#

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

Page 9: Flappy bird game in c#

Adding timer properties:name = gameTimer Enabled = True Interval = 15

Page 10: Flappy bird game in c#

gameTimer_Tick function

pipeBottom.Left -= pipeSpeed; pipeTop.Left -= pipeSpeed; flappyBird.Top += gravity; scoreText.Text = " " + Inscore; //for scoresThis will scroll the pipes to the left and drop the bird using the gravity variable.

Page 11: Flappy bird game in c#

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.

Page 12: Flappy bird game in c#

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();

Page 13: Flappy bird game in c#

Regenerating pipesEnter the following code in the game timer functionif (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.

Page 14: Flappy bird game in c#

score on screenscoreText.Text = “ " + Inscore;

Page 15: Flappy bird game in c#

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 characterif (e.KeyCode == Keys.Space){jumping = false;gravity = 5;}

Page 16: Flappy bird game in c#

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;}

Page 17: Flappy bird game in c#

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;}}

Page 18: Flappy bird game in c#

private void gameTimer_Tick(object sender, EventArgs e) { pipeBottom.Left -= pipeSpeed; pipeTop.Left -= pipeSpeed;

flappyBird.Top += gravity; scoreText.Text = " " + Inscore; if (pipeBottom.Left < -50) //ending game { pipeBottom.Left = 800; Inscore = Inscore + 1; } else if (pipeTop.Left < -75) { pipeTop.Left = 910; Inscore += 1; }

if (flappyBird.Bounds.IntersectsWith(ground.Bounds)) //regenreting pipes { endGame(); } else if (flappyBird.Bounds.IntersectsWith(pipeBottom.Bounds)) { endGame(); } else if (flappyBird.Bounds.IntersectsWith(pipeTop.Bounds)) { endGame(); }

}

Page 19: Flappy bird game in c#

private void endGame() { gameTimer.Stop(); endText1.Visible = true; endText2.Visible = true; gameDesigner.Visible = true;

}

private void endText2_Click(object sender, EventArgs e) { endText2.Text = "Your final score is: " +Inscore; }

private void GameKeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Space) { jumping = true; gravity = -7; } }

private void GameKeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Space) { jumping = false; gravity = 5; } }

}}