C# Tutorial – Create a Zombie Survival Shooting Game in Visual Studio

Welcome to this exciting tutorial. Reminder – this is an advanced tutorial, if you are not comfortable with Visual Studio or C# please refer to some of the easier tutorials to warm up and then crack on with this. Making fun little projects are the best way to learn programming and it makes it fun.

In this tutorial we will show you how to create an awesome top down zombie shooter game in visual studio and using c# programming language. This tutorial is for the advanced users because we will be using classes and objects to manipulate them. Game itself is simple you are the shooter who has limited ammo and zombies will spawn in random places and will come after the player. There is a ammo drop, ammos will drop in various different spots in the game and player needs to collect them.

Lesson Objective –

  1. Create a full top down shooting game in visual studio
  2. Using sprites and methods to change the state of player and zombies depending on the direction
  3. Introduction to Object Orientated Programming (OOP)
  4. Create new class and methods, variables and custom data from that class
  5. Using for Loops to loop through all of the components in the game
  6. Using the pre built classes in C# to add them and remove them from screen
  7. Dynamically create/remove zombies and spawn them across the screen
  8. Dynamically create and remove bullets that shoot from up/down/left/right directions
  9. Dynamically create and remove ammo boxes across the screen
  10. Create a health bar for the player and change colour depending on how much health the player has left
  11. Debug the program

Video Tutorial

Download the images for zombie shooter game here
Download Top Down Zombie Shooter Project on GitHub

We are working on Visual Studio 2010 but this project can be remade in any other version of visual studio.

Start a new project in Visual Studio, make this a C# Windows Form Application and name it zombieShooter.

This is the empty form we will be working with in this game. Look under the properties window and find the following and change them.

Change the back colour to 64, 64, 64 it’s a dark grey colour.

Change the size to 939, 705

Text change to Zombie Shooter

Under the Solution Explore, under the properties tab double click on the Resources.resx

In the resources panel click on the add resources option, from the drop down click on Add Existing File

Highlight all of the images downloaded from MOOICT.COM tutorial page and click open.

Here is has been added to the resources panel.

Now make sure to press CTRL + S or Save All button. (Do not do a save as or anything it will only save part of the project in a different folder. Always do Save ALL)

from the tool box drag and drop 2 labels to the form.

Above 3 labels has been added. Change the following in the properties window

First change the fore colour to white. then click on the font option and click on the three dotted button (…)

In this dialog box change the settings to BOLD and SIZE 16.

This is the changed labels in the form. Now change the text to the following –

Label 1 change to Ammo: 0, label 2 change to Kills: 0 and change Label 3 to Health:

Find a progress bar from the tool box and drag it to the form.

Adjust the progress bar next to the health label. This will be used to show the health of the player.

Find the picture box from the tool box and drag and drop 4 picture boxes to the form.

4 picture boxes has been added to the form. Make sure they are well separated from each other.

Select the first picture box on the left and make the changes to the properties of the picture box.

Click on the Image option and then click on the 3 dotted button next to it.

From the resources select the zdown picture and click OK.

Change the size mode to Auto Size, change the tag to zombie (lower case)

Do the same for the other zombie pictures.

Select the middle picture box, look under the properties window.

change the name to player (lower case)

Change the size mode to Auto Size.

change the image of the player to UP. Click OK.

This is the view of the form so far. Looking good.

Click on the form and go to the events manager by clicking on the lightning bolt icon next to the properties icon. Find the key down and type in keyisdown press enter, in the key up event and type keyisup and press enter.

For the last component, find the timer component in the tool box and drag it down to the form.

Under the properties window for the timer change the enabled to true, interval to 20. This means when the form will run the timer will start and it will tick 20 milliseconds.

While the timer is selected, Click on that lightning bolt icon and type gameEngine and press enter.

We need to add the bullet, bullets will be added dynamically in the game when its fired but we need to program it in with the game. We will need to create a bullet CLASS. This is where we are touching on the subject of OOP or Object Oriented Programming. This model is very popular in game programming. First we need to create the class and then we can call in to do its thing in the game.

Right click on the zombieShooter in the solution Explorer. Hover over ADD then click on Class.

Type in bullet and click OK. This will create a blank class for us with nothing in it. We will add things to it afterwards. Keep following the tasks.

Let’s look into the Form1.cs the code view of the main form. You can see the key is up, key is down and game engine events are already on there. Now we need to add our own variables and functions to this.

In the current codes we will add three of our own functions in it.

1 Drop Ammo function – this function will be used when the user needs more ammo in the game.

2 Shoot function – this function will be used when the player is shooing in the game.

3 Make Zombies function – this function will be used when we need to make more zombies in the game.

Highlighted lines are the custom function we added to the code.

Below are the variables

Each of the variables are explained through the comments which are the green codes.

keyisdown Function

All the code is explained through the comments.

keyisup Function

This is the key up function. You will notice we have included the SPACE bar in this function. This is because we don’t want the player to spam the Space key, the bullet will only release when the space bar is released.

Inside the function we included if the ammo is less than 1 meaning if its 0 then the game will drop an ammo box to the screen.

The game engine function

– caution this will be long. Pay extra attention DO NOT FORGET THE OPEN AND CLOSING CURLY BRACKETS.


There are two for each loop and several if statement which are inside this event. All of the codes are explained in the comments next to them. Pay extra attention to them, if there are any red lines under the code then come back to this and check the code.

Drop Ammo function –

Shoot function –

I will explain this function in more detail because as you type it might not recognise some of properties you are inputting in there, that’s not because it’s wrong that’s because we haven’t created that bullet class just yet.

Let’s start with the beginning of the function

private void shoot(string direct)

you will notice this function has a argument inside the smaller brackets called string direct. This is a local variable only for this function. What this means is we will need to pass in a string value inside this function. When this function is called some sort of string needs to be passed to it for it to function.

for example we can use shoot(“never”) or shoot(“i am clever”) it will work but it will not produce the results we want it to. For the function to work as we want to we need to pass in the facing string we created earlier and that’s responding to the key down event too.

bullet shoot = new bullet(); // create a new instance of the bullet class

In the line above we are creating an instance of the bullet class. By doing it like this we can bring in multiple instances of this class just by calling it. Good thing about OOP is when you call a class like this you bring in all of the properties with it.

shoot.direction = direct; // assignment the direction to the bullet

Now that the name SHOOT has been linked with the bullet class we can access the properties of the class and modify them any way we want to. In this case there will be a variable called direction inside the bullet class and this will determine which way the bullet is going up, down, left or right.

shoot.bulletLeft = player.Left + (player.Width / 2); // place the bullet to left half of the player

The line above is accessing the bulletLeft variable and assigning its value to it. This variable is used to determine the start position of the bullet. For this case we are assigning to the players left position and we are dividing the players height by 2 which means it will be positioned right in the middle of the player and where the guns pointed.

shoot.bulletTop = player.Top + (player.Height / 2); // place the bullet on top half of the player

This line above is doing the same thing as before but for the top position of the bullet.

shoot.mkBullet(this); // run the function mkBullet from the bullet class.

This line is allowing us to call this function inside the class. We will be creating it, so if there is a red line under it now do not worry we are getting there. Notice that we are stating (this) on the function which means same as this function we are sending some arguments to help adding the bullet to screen.

Make Zombies function –

Now it’s time to start on the bullet class –

First under the using part of the class add the two highlighted above. First one is System.Drawing; and second is System.Windows.Forms; by adding these two we are able to use the windows components such as times, picture boxes and edit their properties. This is very important, without these two you cannot get access to the windows components make sure you add them both in this class.

Let’s start with the variables first.


Note – when we are creating variables in a class and those we want to use outside the class we need to put a public in front of the variable declaration. Remember the shoot function we creating this is what that was doing before, we are calling the variables from this class to the other function.

mkBullet Function

Below is the mkBullet function. Notice this one has a public declaration which also means we need to access this from out of this class. We used this in the shoot function.

In the mkBullet function the first 7 lines of code is self explanatory and it has been explained in the comments. The lines I will be expanding on are the last 3.

tm.Interval = speed; // set the timer interval to speed

TM is our timer we created earlier in the CLASS. The interval is set to the speed, in this case its 20 which means the bullet will move 20 milliseconds towards any direction we set.

tm.Tick += new EventHandler(tm_Tick); // assignment the timer with an event

TM needs a tick event linked to the time in order for us to move the bullet across the screen. It’s a good idea to assign a time to each of the bullets created through this and each button will be independent from the last. += new EventHandler() is the event invoker and we have inserted tm_Tick is our event. We also need to declare the tm tick event next.

tm.Start(); // start the timer

Lastly we are starting the timer in this function.

tm_Tick event – Unlike the game engine event this will be a smaller event function.

With all this done now it’s time to debug and test the game out. or Press F5

Shooting bullets work and zombies die from the screen and re-spawn when one dies in the game.

Ammo drop is working too and so is the health bar it changed colour when health dropped below 20.

Full Source code is available on the next page.

13 responses to “C# Tutorial – Create a Zombie Survival Shooting Game in Visual Studio”

  1. Michael says:

    Hey there i really like this code and ive typed it exactly how you have but for some reason the code ” foreach (Control x in this.Controls)”
    doesnt seem to wkr very well its fine until after the code “”((PictureBox)x).Dispose();// dispose the picture box completely from the program
    ammo += 5;// add 5 ammo to the integer”

    then after that every single x has a red line under it and it says “The name ‘x’ does not exist in the current context” please help

  2. Anhar Ali says:

    Hi Michael, I’m glad you found this tutorial helpful. The x variable is stored in the for each loop line “foreach (Control x in this.Controls)
    {“. This line is starting a for each loop and we are using x as the control method to find the picture boxes. Check if you wrote the for each loop correctly in this context.

  3. Albin K says:

    Visual studio gives me a warning when I type ” x.Tag == “ammo” ” and for some reason whenever I press the “up” key the game pulls me to the top and I get stuck. I can move to the left or right but I can’t go down. The version of Visual Studios that I’m using is 2015.

  4. Syed Jawad Asghar says:

    my zombie sprites are not moving and game is not getting over i have done exactly what is done in tutorial but it not working please help with this. i am using visual studio 2015

  5. IceAndFire says:

    tnx so much, i wanted to make this type of game so long time, i never knew how, thanks so much sir

  6. Mạnh Đỗ says:

    Hi, I really love your tutorials, but may I want to ask you something: Can I make a scoreboard or save high scores ?

  7. tal says:

    what is lt ? its not in the variables…… on the bullet class if (Bullet.Left & lt; 16 || Bullet.Left & gt; 860 || Bullet.Top & lt; 10 || Bullet.Top & gt; 616)

  8. Tim says:

    thx for the tutorial. it was very usefull in getting the basics down. Ima making a game during my post college boredom 🙂

    Sept 2019

  9. Muwux says:

    Hi, how would you add health? I got added but “health” forever spawn.

  10. milk says:

    Hi, i got an error like this ” possible unintended reference comparison to get a value comparison cast the left hand side to ‘string’ ” in foreach (Control x in this.Controls) and foreach (Control j in this.Controls). Please help me

  11. Vishal Kumar Tigga says:

    I have really liked the content over here, it literally hooked me into this gaming industry, copied and typed all the code but a bug is keep on coming.
    all the directions of the game are working except when i face the downside, my bullets doesn’t come out. it will stay in the ground and when i move from that particular place I can see that the bullet is there lying on the ground. can you help please

  12. Anhar Ali says:

    Hi, I’m glad you liked the tutorial. It sounds like the bullet class is probably missing the directive to move it downwards when its created on the form. Check the spelling is the direction is the same one that’s being passed through the class. So when the key is being pressed down see if the facing string is the same as the one you coded in the bullets class.

  13. Fariha Afrin Khan. says:

    I’ve typed the codes, and fixed errors and have emphasize this project idea adding some of my own concept of Design Tools and with new coding blocks for those Tools in main Form 1.

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