WPF C# Tutorial – Create a Flappy Bird Game in Visual Studio
- Subject: WPF C# Tutorials
- Learning Time: 2 hours
Adding Variables and writing code for flappy bird
To begin lets import the threading name space to this game. Threading has the timer class we want so we will need import the name space first and then have access to the dispatcher timer class.
Type in using System.Windows.Threading in the top part of the C# script. This line will tell the compiler that we need this name space included with this project and then we can access the classes inside of it.
After that add the following variables on top of the public MainWindow() line. These variables are called global variables, they are positioned on top of the class and not inside of any functions. This means any function form the game can access and change these variables. Since this is a small flappy bird we don’t need to be too concerned about these being global. The selectable code is shown below.
// create a new instance of the timer class called game timer
DispatcherTimer gameTimer = new DispatcherTimer();
// new gravity integer hold the vlaue 8
int gravity = 8;
// score keeper
// new rect class to help us detect collisions
// new boolean for checking if the game is over or not
bool gameover = false;
Each line is commented already but we will go over it again to make sure we are all on the same page.
Dispatcher Timer is the class we imported using the name space using System.Windows.Threading line earlier. We are creating a new instance of the dispatcher timer called game timer and then initializing a new dispatcher on it.
Int gravity = 8; this integer will be used to pull the character down on the screen to simulate gravity for this game.
Double score; this DOUBLE variable will be used to keep score of the game. The reason we are using a double is because integers are not suitable for decimal point numbers. We have two pipes one on top and one on bottom both them will have .5 score attached so when we successfully go through both it be added to a 1. With double variables we can calculate decimal point numbers.
Rect FlappyRec; This is a Rect class that’s going to be linked to the bird image on the screen. This RECT has the intersect property which is useful to us in this game. In order to check for collisions between the flappy bird and the pipes we need this Rect Class.
Bool gameover = false; this is a Boolean variable called game over and its set to false. This Boolean will be used to check if the game is running or stopped if it has stopped then we can give it instructions to run the game again.
That’s all of the global variables we need for this game. Let’s start with making some functions and events we need.
See the screen shot below, add the start game and game engine code in the C# script. Add them at the end of key is up event that’s the last event at the moment. Both of these functions are important and so make sure you don’t add them outside of the class as that will not work.
These both functions need to be added before the last two curly brackets. Make sure you don’t have any red lines or errors showing in visual studio. If everything is in order then we can move on to adding instructions for the functions.
Go to Page 5 for adding instruction to functions
- c# game development
- flappy bird development
- flappy bird wpf c#
- game development wpf
- make games in visual studio
- wpf project files
- wpf tutorial