WPF C# Tutorial – Create a space battle shooter game in Visual Studio

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.

wpf c# space shooter tutorial add key down events

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.

wpf c# space shooter tutorial add key up events

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.

wpf c# space shooter tutorial key down and key up events added to the 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.

wpf c# space shooter tutorial adding two more functions

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.

wpf c# space shooter tutorial adding the threading namespace

Type this using System.Windows.Threading; line under the last using keyword.

Now lets add the variables for this game.

wpf c# space shooter tutorial variables for the 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.

7 responses to “WPF C# Tutorial – Create a space battle shooter game in Visual Studio”

  1. Daniel Rymes says:

    Having difficulties with URI definition. The original version failed during the build. Code compiled fine after replacing with relative version: `new Uri(@”../images/player.png”, UriKind.Relative)`. However, app is breaking, because it can’t find the image in …\bin\Debug\images\purple.png.

    Wondering if the issue could be related to URI syntax of if something’s wrong with the build process (e.g. missing/wrong item csproj). Any fix suggestions?

  2. Anhar Ali says:

    Hi Daniel, you made the images folder inside the debug folder. This folder needs to be in the main application directory. In page 1 of the tutorial it shows how to make the folder inside the solutions explorer, and then you can import the images into it.

  3. David Macák says:

    Thank you for this tutorial. I am new to C# and WPF and just found your fantastic web and your tutorials. I like this style of tutorials where you actually make something useful and fun. I made this and learned a lot by writing the code myself rather than ctrl+v.

    Also after limit = 20; it gets much harder 😀

  4. Hi Mr. Moo says:

    Well done thank you for all the education to the internet!!!!!!!!!!!! <3

  5. Matthew Marse says:

    a fun tutorial to do because we are in space shooting alien space *suits* I mean if that’s not fun

  6. Davor Kreß says:

    Your sites are very helpful and cool. Thank you very much.

  7. Anhar Ali says:

    Thank You. 🙂

Comment on this tutorial and let us know how you got on -