× Main Menu

WPF C# Tutorial – Create a Space Invaders game in Visual Studio

Enemy Bullet Maker Function

This function is responsible for making the bullets that fires back at the player. This function makes the game more interesting and challenges the player because without it the player can simply stay in one place and fire bullets at the invaders. You have to be a little evil to be a game developer sometimes heheh (in a good way).

 

This function takes in two arguments which mean when this function runs, we need to give it two numbers to set the x and y location of the enemy bullets. For example in order to run this function we will call it like this enemyBulletMake(200, 300); this means the enemy bullet will be made and placed at 200 pixels from the left and 300 pixels from the top. We can also use the Get Left and Get Top location function to gather information on an object and directly include it inside of this function.

Inside this function we have a new rectangle being made, this rectangle has a tag of enemy bullet, height of 40 pixels and width of 15 pixels. It will have yellow background and black 5 pixels thick border.

Last 3 lines are setting the x and y location of the enemy bullet and adding it to the screen. This is a very simply function but when we have implemented it inside the timer it will make the game that much useful and challenging.

Make Enemies Function

This function will make as many enemy space invaders we want for this game, by grouping all of he relevant instructions inside one function we are making the game more efficient and hopefully reusable in the future.

 

The purpose of this function isn’t just to make enemies for us too shoot at its also to give them appropriate images, tag them, set the x and y and lastly place them on the screen. As you can see above this function takes in the limit integer as an argument. By sending in an integer number we can tell this function how many we want in the beginning of the game. So if we tell the program makeEnemies(10); it will make 10 enemies for us. But we want these enemies to have random images attached to them as the skin and we want them to be placed perfectly next to each other so it looks like the classic game.

First when the function runs, we create a new local integer called left and its equals to 0. This integer will help us lace the characters next to each other. Secondly, we are telling the total enemy’s integer how many enemies we ordered for this game. Total enemy’s integer keep track of how many invaders are on the screen and how many were shot down.

Everything else in this function will go through the main for loop. A for loop is an efficient way to run repetitive code without repeating it.

Beginning of the code runs like this for loops take a local integer called i, this i is equals 0 at the beginning, so we want to run this loop as many times as the number of invaders ordered from the developer i.e. us. So, if we said give me 10 invaders this loop will run 10 times and if 20 then 20 times and so on. As long as i is less than the limit we will increase i by 1 until we reach the same number as the limit.

This condition helps us to run everything inside the code 20 or 30 or 50 times. Let’s see what is inside the for loop.

 

This part above is responsible for making and placing the enemy to the screen. There are few things to look closely here. First, we are creating an image brush called enemy skin, it’s the same method to which we used to make the player skin. Then we make the enemy rectangle give its properties and link the enemy skin to it fill property. Then we are setting y or top axis of the enemy all of the enemies will have the same top location which is 10 pixels from the top of the screen. Next, we are setting the left or x axis of the invaders. Here you can see we are setting it to the LEFT integer we created earlier in the program. Now notice that once we have created and placed the invader on the screen, we are deducting 60 from the left integer. Why? This is because when the loop runs once the left integer will be 0, it runs again it will -60 it runs again it will be -120 and so on therefore we would have perfectly placed the enemy invaders on the screen left of each other.

 

This part of the code will make sure that each space invader has its own images and no two-invader side to side will have the same picture as its background. First, we add 1 to the enemy images integer this integer will be used to check which image to apply to which invader. After that we have an if statement that checks if the enemy images integer has gone over 8 if so, it will be reset back to 1. We have 8 different images for the invaders, remember importing them in the beginning of the project. This is why 8 is an important number here. After that we are using a switch statement that checks the number inside of the enemy images integer and then it will find the case inside of it relevant to that number and apply the images to the invader. This way we can make sure that there are lots of colourful images on the screen and it makes the game look very pretty.

So, the make enemies’ function is kind of long but nothing compared to what’s coming next. We have the games engine next on the list and it’s a big one. If you have made it so far then well done, now make sure you have done all of the above right first before starting on the next one.

Go to Page 5 for the game engine function




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

%d bloggers like this: