DEV Community

Cover image for Let's Make Flappy Bird In C# 🐤
christine
christine

Posted on • Updated on

Let's Make Flappy Bird In C# 🐤

Whenever I have to learn/practice a programming language I always try and do it by making little games. I don't know why, but the tedious act of making a Student Management System each time I have to practice my programming skills feels redundant, and sometimes it even gets a little boring (even though it is crucial to make such systems to truly learn). I would rather entertain myself whilst practicing the fundamentals via quick bursts of mini games! And so, my C# Flappy Bird was born. 🐥

Today, I'm going to show you how to make Flappy Bird in C#. All you need is Visual Studio Installed, and twenty minutes to spare (yes, it doesn't take long to make this)! You'll be done with this in no time.😎

I want to thank Moo ICT for the base assets for this game. You can download the assets (the bird, obstacles, and ground) here.

Step One: Creating The Game Interface

Open up your Visual Studio and create a new project. For this to work, you need to select a Windows Form App project using C#. Name this anything you want, and click next x2.
Flappy Bird

Next, we need to add our componentson our Form. Open up your Toolbox (on the left) and add the following objects:

  • 2x Labels.
  • 4x Picture Boxes.

I have added an image below showing you what I named my objects. You can also go ahead and fill in the background for your form.
Flappy Bird

Your final form layout should look similar to this:
Flappy Bird

The final step to creating our form interface is to add a timer, so drag that in from the Toolbox. I named mine gameTimer, and set the interval to 10, and enabled it. Also head over to the timer's properties and assign it a tick behavior (I called it gameTimer_Tick, but thinking of it, a better name would be gameStart).
Flappy Bird

Flappy Bird

Finally, we need to set our event listeners. We will do this via our KeyUpand KeyDownproperties, which we will need to be able to move the bird up and down (duh!).
Flappy Bird

And with that, our interface is done for now! Fell free to change this around to what makes you feel comfortable. Heck, even swap out the bird with a fish! 🐟

Step Two: Setting Up Our Variables

Double click your form to open up your Form.cs file (mine is named Flappy.cs becaused I renamed it). Let's start by defining some public variables that we will use throughout.

namespace Flappy_Bird
{
    public partial class Flappy : Form
    {
        //public variables
        int obstacleSpeed = 5; //movement of obstacles (pipes)
        int gravity = 5; //movement of bird (player)
        int score = 0; //player survival rate!

        public Flappy()
        {
            InitializeComponent();
            gameTimer.Start();
        }

        //function defining what will happen during game start 
        private void gameTimer_Tick(object sender, EventArgs e)
        {

        }

        //function for when the player pressed the up arrow key
        private void onKeyUp(object sender, KeyEventArgs e)
        {

        }

        //function for when the player releases the down arrow key
        private void onKeyDown(object sender, KeyEventArgs e)
        {

        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Step Three: Moving The Bird

Let's get our event listener functions (onKeyDown and onKeyUp) out of the way. We need to add onclick events to each to increase/decrease our gravity value, ie. we need our bird to move. You might have to play with this value later on to suit your game. We also need to enable our gravity variables to change upon game (timer start)

       //affects the birds' margin according to the value of gravity
       birdPlayer.Top += gravity;

        //function defining what will happen during game start 
        private void gameTimer_Tick(object sender, EventArgs e)
        {

        }

        //function for when the player pressed the up arrow key
        private void onKeyUp(object sender, KeyEventArgs e)
        {
            //the bird will be "pushed" up
            if (e.KeyCode == Keys.Up)
            {
                gravity = -5;
            }  
        }

        //function for when the player releases the down arrow key
        private void onKeyDown(object sender, KeyEventArgs e)
        {
            //the bird will be "pushed" down
            if (e.KeyCode == Keys.Down)
            {
                gravity = 5;
            }
        }
Enter fullscreen mode Exit fullscreen mode

Step Four: Moving The Obstacles

Now that we have the bird moving, we can see that the obstacles disappear without "respawning". That's not really fun, so let's get them moving/spawning by seeing if they are out of bounds and repositioning them.

        //function defining what will happen during game start 
        private void gameTimer_Tick(object sender, EventArgs e)
        {
            //affects the birds' margin according to the value of gravity
            birdPlayer.Top += gravity;

            //will affect the obstacles margin so that it moves out of the screen area (giving the illusion that the bird is moving forward) 
            obstacleBottom.Left -= obstacleSpeed;
            obstacleTop.Left -= obstacleSpeed;

            //this will allow us to spawn the obstacles again and again when they are out of bounds
            if(obstacleBottom.Left < -150 )
            {
                obstacleBottom.Left = 950;
            }

            if (obstacleTop.Left < -180)
            {
                obstacleTop.Left = 950;
            }
        }

        //function for when the player pressed the up arrow key
        private void onKeyUp(object sender, KeyEventArgs e)
        {
            //the bird will be "pushed" up
            if (e.KeyCode == Keys.Up)
            {
                gravity = -5;
            }  
        }

        //function for when the player releases the down arrow key
        private void onKeyDown(object sender, KeyEventArgs e)
        {
            //the bird will be "pushed" down
            if (e.KeyCode == Keys.Down)
            {
                gravity = 5;
            }
        }
Enter fullscreen mode Exit fullscreen mode

Step Four: Setting The Score

Okay, we are almost there. now all we need to do is up the score when the player successfully avoids each obstacle, and then also implement a gameOver() function that will end the game when the player collides with an obstacle. We can also go ahead and make the game "harder" by increasing the speed once the score reaches a certain value.

//function defining what will happen during game start 
        private void gameTimer_Tick(object sender, EventArgs e)
        {
            //affects the birds' margin according to the value of gravity
            birdPlayer.Top += gravity;

            //will affect the obstacles margin so that it moves out of the screen area (giving the illusion that the bird is moving forward) 
            obstacleBottom.Left -= obstacleSpeed;
            obstacleTop.Left -= obstacleSpeed;

            //we update the score throughout the game by casting our value as string
            scoreText.Text = score.ToString();

            //this will allow us to spawn the obstacles again and again when they are out of bounds
            if(obstacleBottom.Left < -150 )
            {
                obstacleBottom.Left = 950;
                //the score increases if the pipe spawns since we haven't hit it.
                score++;
            }

            if (obstacleTop.Left < -180)
            {
                obstacleTop.Left = 950;
                //the score increases if the pipe spawns since we haven't hit it.
                score++;
            }

            //this will prevent our bird from "falling" off the screen if it goes beyond the bounds of our screen it is game over
            if (birdPlayer.Bounds.IntersectsWith(obstacleBottom.Bounds) || birdPlayer.Bounds.IntersectsWith(obstacleTop.Bounds) || birdPlayer.Bounds.IntersectsWith(ground.Bounds) || birdPlayer.Top < -25)
            {
                if (score > 0)
                {
                    score--;
                }
                else
                {
                    gameOver();
                }
            }

            //this will increase the obstacle speed as the game runs to make it harder
            if (score > 10)
            {
                obstacleSpeed += 10;
            }
        }
Enter fullscreen mode Exit fullscreen mode

Putting It All Together

namespace Flappy_Bird
{
    public partial class Flappy : Form
    {
        //public variables
        int obstacleSpeed = 5; //movement of obstacles (pipes)
        int gravity = 5; //movement of bird (player)
        int score = 0; //player survival rate!

        public Flappy()
        {
            InitializeComponent();
            gameTimer.Start();
        }

        //function defining what will happen during game start 
        private void gameTimer_Tick(object sender, EventArgs e)
        {
            //affects the birds' margin according to the value of gravity
            birdPlayer.Top += gravity;

            //will affect the obstacles margin so that it moves out of the screen area (giving the illusion that the bird is moving forward) 
            obstacleBottom.Left -= obstacleSpeed;
            obstacleTop.Left -= obstacleSpeed;

            //we update the score throughout the game by casting our value as string
            scoreText.Text = score.ToString();

            //this will allow us to spawn the obstacles again and again when they are out of bounds
            if(obstacleBottom.Left < -150 )
            {
                obstacleBottom.Left = 950;
                //the score increases if the pipe spawns since we haven't hit it.
                score++;
            }

            if (obstacleTop.Left < -180)
            {
                obstacleTop.Left = 950;
                //the score increases if the pipe spawns since we haven't hit it.
                score++;
            }

            //this will prevent our bird from "falling" off the screen if it goes beyond the bounds of our screen it is game over
            if (birdPlayer.Bounds.IntersectsWith(obstacleBottom.Bounds) || birdPlayer.Bounds.IntersectsWith(obstacleTop.Bounds) || birdPlayer.Bounds.IntersectsWith(ground.Bounds) || birdPlayer.Top < -25)
            {
                if (score > 0)
                {
                    score--;
                }
                else
                {
                    gameOver();
                }
            }

            //this will increase the obstacle speed as the game runs to make it harder
            if (score > 10)
            {
                obstacleSpeed += 10;
            }
        }

        //function for when the player pressed the up arrow key
        private void onKeyUp(object sender, KeyEventArgs e)
        {
            //the bird will be "pushed" up
            if (e.KeyCode == Keys.Up)
            {
                gravity = -5;
            }  
        }

        //function for when the player releases the down arrow key
        private void onKeyDown(object sender, KeyEventArgs e)
        {
            //the bird will be "pushed" down
            if (e.KeyCode == Keys.Down)
            {
                gravity = 5;
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

I hope this helped, I'm no programming God but I seemed to be able to make a base for a good Flappy Bird game clone. Maybe challenge yourself to clean up the code, and add a menu system. 😁

Check out the full project on my GitHub. Until Next time! ☺

Top comments (2)

Collapse
 
andrewbaisden profile image
Andrew Baisden

Wow what fun brings bad good memories.

Collapse
 
kaylumah profile image
Max Hamulyák

Wow 20 minutes is kinda fast :)
Reminds me of a bomberman clone I once made in school. It was also using good old Windows Forms :)