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

Make enemies function for the space shooter game –

In this function we will make brand new enemies to add to the screen. I will make the enemies the same way and it will randomly select an image to assign to the enemies so it looks like its generating different enemies that will spawn in the game.

The full source code for the make enemies function is below, each line has been commented in the code however we will take a closer look at the this function below.

        private void makeEnemies()
        {
            // this function will make the enemies for us including assignning them images

            ImageBrush enemySprite = new ImageBrush(); // make a new image brush called enemy sprite

            enemySpriteCounter = rand.Next(1, 5); // generate a random number inside the enemy sprite counter integer

            // below switch statement will check what number is generated inside the enemy sprite counter
            // and then assign a new image to the enemy sprite image brush depending on the number

            switch (enemySpriteCounter)
            {
                case 1:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/1.png"));
                    break;
                case 2:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/2.png"));
                    break;
                case 3:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/3.png"));
                    break;
                case 4:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/4.png"));
                    break;
                case 5:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/5.png"));
                    break;
                default:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/1.png"));
                    break;
            }

            // make a new rectangle called new enemy
            // this rectangle has a enemy tag, height 50 and width 56 pixels
            // background fill is assigned to the randomly generated enemy sprite from the switch statement above

            Rectangle newEnemy = new Rectangle
            {
                Tag = "enemy",
                Height = 50,
                Width = 56,
                Fill = enemySprite
            };


            Canvas.SetTop(newEnemy, -100); // set the top position of the enemy to -100
            // randomly generate the left position of the enemy
            Canvas.SetLeft(newEnemy, rand.Next(30, 430));
            // add the enemy object to the screen
            MyCanvas.Children.Add(newEnemy);

            // garbage collection
            GC.Collect(); // collect any unused resources for this game
        }

Lets take a look at the local variables first

ImageBrush enemySprite = new ImageBrush(); This line here is making a new image brush called enemy sprite. This image brush will be the local image brush that can only be used from inside of this function. we cannot access this from outside. We will load the alien images that we have imported into this project on this sprite and assign them to the enemy rectangles.

enemySpriteCounter = rand.Next(1, 5); This line is generating a random number between 1 and 5 inside the enemy sprite counter. So each time this function runs first it will make a new enemysprite image brush and then it will make a new random number in the enemy sprite counter integer. With this enemy sprite counter, we can assign different images to the enemy rectangles for this space shooter game.

The switch statement –

            switch (enemySpriteCounter)
            {
                case 1:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/1.png"));
                    break;
                case 2:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/2.png"));
                    break;
                case 3:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/3.png"));
                    break;
                case 4:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/4.png"));
                    break;
                case 5:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/5.png"));
                    break;
                default:
                    enemySprite.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/1.png"));
                    break;
            }

This switch statement has 6 conditions. One for each image we have imported and one default condition is nothing else matched or error during run time. If we have generated number 3 inside the enemy sprite counter then it will load the 3.PNG file inside the enemy sprite image brush or if it generated 2 it will do the same with 2.PNG file and so on switch statements are kind of like if statements but they have the cases and use breaks so the whole loop doesn’t run after the condition is met, it will jump out of the loop and move the instructions outside of it as in making the new rectangle for the enemy object.

Making the new rectangle

Below is the code that’s making the new rectangle and assigning the chosen image to it. It is also setting up the LEFT and TOP position of the new enemy rectangle and adding it to the canvas.

            Rectangle newEnemy = new Rectangle
            {
                Tag = "enemy",
                Height = 50,
                Width = 56,
                Fill = enemySprite
            };


            Canvas.SetTop(newEnemy, -100); // set the top position of the enemy to -100
            // randomly generate the left position of the enemy
            Canvas.SetLeft(newEnemy, rand.Next(30, 430));
            // add the enemy object to the screen
            MyCanvas.Children.Add(newEnemy);

 

We are setting the height to 50, width to 56 pixels, it has a tag called enemy and the fill will be linked to the enemy sprite we made earlier in this function. all that is being done inside of the rectangle class so right after that we will need to set up the LEFT (X) position and TOP (Y) position of the new enemy rectangle.

Canvas SET TOP and SET LEFT functions do this specifically. So, the top we say the new object can spawn -100 from the top so it will start far off from the top position so it can move down towards the player. LEFT position is showing new enemy will have a random X position between 30 and 430 pixels so it can be any position within the limit. If we spawn the characters in the same location over and over it will be come predictable.

MyCanvas.Children.Add(newEnemy); this line will place the newly created object to the screen with all of the required properties assigned to it.

If you’ve successfully followed the tutorials so far then very well done, we will get started on the game’s engine code on the next page.




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