WPF C# Tutorial – Create a space battle shooter game in Visual Studio
- Subject: WPF C# Tutorials
- Learning Time: 3 hours
Add key down and up events to C# –
Inside the XAML code we have noted the key down and up event in the CANVAS tag now it’s time to add the events to the C# code.
Right click on the onKeyDown keyword in the XAML document and click on Go to Definition. This will take you to the C# script window and you see the that it has added the onKeyDown event to the code.
Do the same for the onKeyUp keyword.
Right click on the onKeyUp keyword and click on go to definition. This will once again take you to the C# code window.
As you can both of the events are now added to the C# script.
Both of the events are now added to the code. Now we need to make 2 more functions to this code before we get into the variables for this game.
Add the following functions to the code, right under the key down and up events.
First one makeEnemies() function will be used to make alien ships that will move towards the player. Function are important in programming and we are using a VOID keyword on it because its not going to return any value back to the program. We can put our instructions inside of this function and just call this function later on just by typing makeEnemies(); line and it will run all of the instructions from inside of it.
Second one the game engine is an event that will be linked to the timer. This event will be very important because it will contain all of the logic for this game, including keeping score, player movement, enemy movement, collision and more. An event is different than a function because an event is usually linked to an action either key pressed, mouse clicks, timer ticks etc.
Adding Variables for the space shooter game –
First we need to add the using System.Windows.Threading; namespace to the project. A name space is a collection of classes inside of C# which are accessible when we put the using keyword on top of the project. All of the using keywords are importing different namespaces for this project and we will need to add the threading namespace to this project so we can access the dispatcher timer class from inside of it.
Type this using System.Windows.Threading; line under the last using keyword.
Now lets add the variables for this game.
Above is the variables necessary for this game. We have different types of variables for this game, we have the dispatcher timer class, Booleans, list, random class, integers and rect classes for this game. Make sure you have entered them above the MainWindow() line. These are global variables meaning they can be accessed from all of the functions from inside of this project.
Selectable code for this project is below –
//make a game timer
DispatcherTimer gameTimer = new DispatcherTimer();
// move left and move right boolean declaration
bool moveLeft, moveRight;
// make a new items remove list
List<Rectangle> itemstoremove = new List<Rectangle>();
// make a new random class to generate random numbers from
Random rand = new Random();
int enemySpriteCounter; // int to help change enemy images
int enemyCounter = 100; // enemy spawn time
int playerSpeed = 10; // player movement speed
int limit = 50; // limit of enemy spawns
int score = 0; // default score
int damage = 0; // default damage
Rect playerHitBox; // player hit box to check for collision against enemy
Each of the variables are explained in the comments.
Just to elaborate, the first DispatcherTimer gameTimer = new DispatcherTimer(); is creating a new timer for this class. This timer has its own properties that can be assigned to make this game work for example we need the tick function and interval function from inside of this timer.
Bool moveLeft, moveRight are two Booleans we need to make the player move left and right. We are using a short code mode to declare two variables of the same timer.
List<Rectangle> itemtoremove = new List<Rectangle>(); this line is creating a new list which is a type of array. This list will be used to store the items no longer needed for this project and can be removed. For example we can remove enemies that been shot down and bullets from the player. Its important to get them off the main game because otherwise it will show down as we spawn more and more items to the game dynamically.
Random rand = new Random(); this line is creating a new random variable for us. We can use the rand keyword to generate a random number for this game which will help to randomly position new enemies.
Int enemySpriteCounter, enemyCounter, playerSpeed, limit, score, damage are all integers that can hold whole numbers. These are explained in the green line comments.
Rect playerHitBox; this line is creating a new rect class that will be linked to the player rectangle and we can identify any collision of the player with any enemy objects.
Go to the next page to start adding instructions inside the main window function.