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).

        private void enemyBulletMaker(double x, double y)
        {
            // this function creates the enemy bullets firing towards the player object in the game
            // see this function is passing through 2 variables x and y these will be location where we place the bullets
            // first create a new rectangle
            // this rectangle will have a tag called enemy bullet, height 40 pixels, width 15 pixels, background yellow border black and border size 5
            Rectangle newEnemyBullet = new Rectangle
            {
                Tag = "enemyBullet",
                Height = 40,
                Width = 15,
                Fill = Brushes.Yellow,
                Stroke = Brushes.Black,
                StrokeThickness = 5

            };

            // now we place the bullets top location to the Y variable
            Canvas.SetTop(newEnemyBullet, y);
            // set the left location to the X location
            Canvas.SetLeft(newEnemyBullet, x);
            // add the bullet to the screen
            myCanvas.Children.Add(newEnemyBullet);
        }

 

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.

private void makeEnemies(int limit)
        {
            // make a local integer called left and set to 0
            int left = 0;
            // save the enemy limit as the total enemy
            totalEnemeis = limit;

            // this is the for loop that will make all of the enemies for this game
            // if the limit is set to 10 this loop will run 10 times if set 20 to then 20 times and so on
            for (int i = 0; i < limit; i++)
            {
                // with each loop 
                // will create a new enemy skin image brush to be used with the enemy rectangle
                ImageBrush enemySkin = new ImageBrush();

                // make a new rectangle called new enemy
                // inside this rectangle we set the properties to tag called enemy 45 height and width and link the enemy skin as the fill
                Rectangle newEnemy = new Rectangle
                {
                    Tag = "enemy",
                    Height = 45,
                    Width = 45,
                    Fill = enemySkin,

                };

                // set the starting location of the space inavder
                Canvas.SetTop(newEnemy, 10); // this is the top location
                Canvas.SetLeft(newEnemy, left); // this is the left location
                // add one to the scene 
                myCanvas.Children.Add(newEnemy);
                // change the to -60
                left -= 60;

                // add 1 to the enemy images integer
                enemyImages++;
                // if enemy images integer goes above 8 
                // then we set the integer back to 1
                if (enemyImages > 8)
                {
                    enemyImages = 1;
                }

                // the switch statement below is checking the enemy images integer
                // with each number it will assign a new skin to the enemy
                // this switch statement will run throughout the loop and it will help us make use of those space invader images we imported earlier
                // it will look for what number is in the enemy images integer and then assign that image to the enemy skin class and then break the loop. 
                switch (enemyImages)
                {
                    case 1:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader1.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 1 GIF file
                        break;
                    case 2:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader2.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 2 GIF file
                        break;
                    case 3:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader3.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 3 GIF file
                        break;
                    case 4:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader4.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 4 GIF file
                        break;
                    case 5:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader5.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 5 GIF file
                        break;
                    case 6:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader6.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 6 GIF file
                        break;
                    case 7:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader7.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 7 GIF file
                        break;
                    case 8:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader8.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 8 GIF file
                        break;
                }
            }
        }

 

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.

// with each loop 
                // will create a new enemy skin image brush to be used with the enemy rectangle
                ImageBrush enemySkin = new ImageBrush();

                // make a new rectangle called new enemy
                // inside this rectangle we set the properties to tag called enemy 45 height and width and link the enemy skin as the fill
                Rectangle newEnemy = new Rectangle
                {
                    Tag = "enemy",
                    Height = 45,
                    Width = 45,
                    Fill = enemySkin,

                };

                // set the starting location of the space invader
                Canvas.SetTop(newEnemy, 10); // this is the top location
                Canvas.SetLeft(newEnemy, left); // this is the left location
                // add one to the scene 
                myCanvas.Children.Add(newEnemy);
                // change the to -60
                left -= 60;

 

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.

                // add 1 to the enemy images integer
                enemyImages++;
                // if enemy images integer goes above 8 
                // then we set the integer back to 1
                if (enemyImages > 8)
                {
                    enemyImages = 1;
                }

                // the switch statement below is checking the enemy images integer
                // with each number it will assign a new skin to the enemy
                // this switch statement will run throughout the loop and it will help us make use of those space invader images we imported earlier
                // it will look for what number is in the enemy images integer and then assign that image to the enemy skin class and then break the loop. 
                switch (enemyImages)
                {
                    case 1:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader1.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 1 GIF file
                        break;
                    case 2:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader2.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 2 GIF file
                        break;
                    case 3:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader3.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 3 GIF file
                        break;
                    case 4:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader4.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 4 GIF file
                        break;
                    case 5:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader5.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 5 GIF file
                        break;
                    case 6:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader6.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 6 GIF file
                        break;
                    case 7:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader7.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 7 GIF file
                        break;
                    case 8:
                        enemySkin.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/invader8.gif"));
                        // if the enemy images number comes up as 1 we can change the image source to the invader 8 GIF file
                        break;
                }

 

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




Comments are closed.