Categories
electronics

Shooter Corona Game in C-sharp – game tutorial.

Game Tutorial: Corona shooter game in C-sharp (C #): You can play it here (free download) but also find the source code to program the game itself. Just by using other drawings, you can make another game. For example, a 2D space shooter.

Shooter Corona Game Tutorial | free source code.

Shooter Corona Game: How to make a game, Game Tutorial: Corona shooter game in C-sharp (C #): You can play it here (free download) but also find the source code to program the game itself. Just by using other drawings, you can make another game. For example, a 2D space shooter.

Corona shooter game in C # + source code.

Shooter Corona Game: Here you can download the game. Note: This is written in C # framework Dot.NET V4.x If it does not work. This is because you don’t have this framework yet. You can download it for free at Microsoft:

Donet framework-microsoft

The advantage of this framework is that it is easily transferable to other systems. So if you install the framework on an Apple PC, then the shooter will work on an Apple computer.

Corona Shooter Game in action

Shooter Corona Game: How does this game look like? I made a short YouTube video. But again you can make a completely different game. Just by using other drawings.

This is a pretty simple game. Good to learn programming in C #.

Shooter self programming process? Which can:

Shooter Corona Game: I use the Microsoft Visual Studio. This is not a tutorial on how to use Visual Studio. So this is not a beginner’s tutorial. But for the slightly advanced.

If you fully understand the operation. You can naturally tweak this code to introduce more and more complex rules. For example, with an additional timer to shoot yourselves. (Now it automatically fires)

Below is the listing of Corona shooter game code:

Shooter Corona Game – source code listing: This is one long listing (normal 426 lines of code). But I have cut it into pieces between, to insert some explanation. So everything is on the code page of your Form1.cs. All the rest of the code is generated by Visual Studio itself when you create a new project in the development software.

initialize game variables

using System;
using System.Drawing;
using System.Windows.Forms;
using WMPLib;
/*
 Copyright (c) Bruno Stroobandt - Infonosity.net
 V1.0 - 2020-03-09 YMD
     */

namespace SpaceGame
{
    public partial class Form1 : Form
    {
        WindowsMediaPlayer gameMedia;
        WindowsMediaPlayer shootMedia;
        WindowsMediaPlayer explosions;

        PictureBox[] stars;
        PictureBox[] munitions;
        PictureBox[] enemies;
        PictureBox[] enemiesMunition;

        private int backgroundspeed;
        private int playerSpeed;
        private int munitionsSpeed;
        private int enemiesSpeed;
        int enemiesMunitionSpeed;
        Random rnd;

        private int score;
        private int level;
        private int dificulty;
        private bool pause;
        private bool gameIsOver;
        

game main form loading events

This is done when the form is loaded, and when you press the replay button.

        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load; //laad het form
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            backgroundspeed = 4;
            playerSpeed = 4;
            enemiesSpeed = 4;
            munitionsSpeed = 20;
            enemiesMunitionSpeed = 4;
            stars = new PictureBox[15];
            munitions = new PictureBox[3];
            rnd = new Random();

            //load immages
            Image munition = Image.FromFile(@"asserts\munition.png");

            Image enemie1 = Image.FromFile(@"asserts\E1.png");
            Image enemie2 = Image.FromFile(@"asserts\E2.png");
            Image enemie3 = Image.FromFile(@"asserts\E3.png");
            Image boss1 = Image.FromFile(@"asserts\Boss1.png");
            Image boss2 = Image.FromFile(@"asserts\Boss2.png");


            //init enemies PBox
            enemies = new PictureBox[10];

            for (int i = 0; i < enemies.Length; i++)
            {
                enemies[i] = new PictureBox();
                enemies[i].Size = new Size(40, 40);
                enemies[i].SizeMode = PictureBoxSizeMode.Zoom;
                enemies[i].BorderStyle = BorderStyle.None;
                enemies[i].Visible = false;
                Controls.Add(enemies[i]);
                enemies[i].Location = new Point((i + 1) * 50, -50);
            }
            enemies[0].Image = boss1;
            enemies[1].Image = enemie2;
            enemies[2].Image = enemie3;
            enemies[3].Image = enemie3;
            enemies[4].Image = enemie1;
            enemies[5].Image = enemie1;
            enemies[6].Image = enemie3;
            enemies[7].Image = enemie3;
            enemies[8].Image = enemie2;
            enemies[9].Image = boss2;

            //init bullets
            for (int i = 0; i < munitions.Length; i++)
            {
                munitions[i] = new PictureBox();
                munitions[i].Size = new Size(12, 12);
                munitions[i].Image = munition;
                munitions[i].SizeMode = PictureBoxSizeMode.Zoom;
                munitions[i].BorderStyle = BorderStyle.None;
                Controls.Add(munitions[i]);
            }
            //create Win Media Player
            gameMedia = new WindowsMediaPlayer();
            shootMedia = new WindowsMediaPlayer();
            explosions = new WindowsMediaPlayer();
            //load the sounds
            gameMedia.URL = "songs\\GameSong.mp3";
            shootMedia.URL = "songs\\shoot.mp3";
            explosions.URL = "songs\\boom.mp3";
            //setup songs playstyle
            gameMedia.settings.setMode("loop", true);
            gameMedia.settings.volume = 6;
            shootMedia.settings.volume = 2;
            explosions.settings.volume = 8;

            //init enemies munition
            enemiesMunition = new PictureBox[10];
            for (int i = 0; i < enemiesMunition.Length; i++)
            {
                enemiesMunition[i] = new PictureBox();
                enemiesMunition[i].Size = new Size(2, 25);
                enemiesMunition[i].BackColor = Color.Yellow;
                enemiesMunition[i].Visible = false;
                int x = rnd.Next(0, 10);
                enemiesMunition[i].Location = new Point(enemies[x].Location.X + 20, enemies[x].Location.Y - 20);
                Controls.Add(enemiesMunition[i]);

            }
           gameMedia.controls.play();

            score = 0;
            level = 1;
            dificulty = 9; // 9= easy - 1=hard
            pause = false;
            gameIsOver = false;


            
            for (int i = 0; i < stars.Length; i++)
            {
                stars[i] = new PictureBox
                {
                    BorderStyle = BorderStyle.FixedSingle,
                    Location = new Point(rnd.Next(20, 800), rnd.Next(-10, 400))
                };
                if (i % 2 == 1)
                {
                    stars[i].Size = new Size(2, 2);
                    stars[i].BackColor = Color.DarkGreen;
                }
                else
                {
                    stars[i].Size = new Size(3, 3);
                    stars[i].BackColor = Color.WhiteSmoke;
                }

                Controls.Add(stars[i]);
            }
            MoveBgTimer.Start();
        }

Game timer events:

For each timer event, you need to put a separate timer on your form. The easiest is, that you create the timer on the form design, and then double-click it. The procedure will be created in the listing. And then you enter what must be done. (To ​​make sure no errors are created. Because from one wrong letter, you can get a lot of gray hairs 😎

 

        private void MoveBgTimer_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < stars.Length / 2; i++)
            {
                stars[i].Top = stars[i].Top + backgroundspeed;

                if (stars[i].Top >= this.Height)
                {
                    stars[i].Top = -stars[i].Height;
                }
            }

            for (int i = stars.Length / 2; i < stars.Length; i++)
            {
                stars[i].Top += backgroundspeed - 2;

                if (stars[i].Top >= this.Height)
                {
                    stars[i].Top = -stars[i].Height;
                }
            }

        }

        private void LeftMoveTimer_Tick(object sender, EventArgs e)
        {
            if (Player.Left > 10)
            {
                Player.Left -= playerSpeed;
            }
        }

        private void RightMoveTimer_Tick(object sender, EventArgs e)
        {
            if (Player.Left < 530)
            {
                Player.Left += playerSpeed;
            }
        }

        private void UpMoveTimer_Tick(object sender, EventArgs e)
        {
            if (Player.Top > 10)
            {
                Player.Top -= playerSpeed;
            }
        }

        private void DownMoveTimer_Tick(object sender, EventArgs e)
        {
            if (Player.Top < 395)
            {
                Player.Top += playerSpeed;
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (!pause) 
            {
                if (e.KeyCode == Keys.Right)
                {
                    RightMoveTimer.Start();
                }
                if (e.KeyCode == Keys.Left)
                {
                    LeftMoveTimer.Start();
                }
                if (e.KeyCode == Keys.Down)
                {
                    DownMoveTimer.Start();
                }
                if (e.KeyCode == Keys.Up)
                {
                    UpMoveTimer.Start();
                }

            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            RightMoveTimer.Stop();
            LeftMoveTimer.Stop();
            UpMoveTimer.Stop();
            DownMoveTimer.Stop();
            if (e.KeyCode == Keys.Space) 
            {
                if (!gameIsOver) 
                {
                    if (pause)
                    {
                        StartTimers();
                        label1.Visible = false;
                        gameMedia.controls.play();
                        pause = false;
                    }
                    else 
                    {
                        label1.Location = new Point(this.Width / 2 - 120, 150);
                        label1.Text = "PAUSED";
                        label1.Visible = true;
                        gameMedia.controls.pause();
                        StopTimers();
                        pause = true;
                    }
                }
            }
        }

       

        private void MoveMunitionTimer_Tick(object sender, EventArgs e)
        {
            shootMedia.controls.play();
            for (int i=0; i < munitions.Length; i++) 
            {
            if (munitions[i].Top > 0) 
                {
                    munitions[i].Visible = true;
                    munitions[i].Top -= munitionsSpeed;
                    Collition();
                }
            else 
                {
                    munitions[i].Visible = false;
                    munitions[i].Location = new Point(Player.Location.X + 20 , Player.Location.Y -i *30);

                }
            }
        }

        private void MoveEnemieTimer_Tick(object sender, EventArgs e)
        {
            MoveEnemies(enemies,enemiesSpeed);
        }
        private void MoveEnemies(PictureBox[] array,int speed) 
        {
            for(int i = 0; i < array.Length; i++) 
            {
                array[i].Visible = true;
                array[i].Top += speed;
                if(array[i].Top > this.Height) 
                {
                    array[i].Location = new Point((i + 1) * 50, -200);
                }

            }
        }

And then a few subroutines for our shooter game.

Again make your buttons on the form and double-click it in order to make the appropriate subroutine.

//andere subroutines:

        private void Collition() 
        {
        for (int i = 0; i < enemies.Length; i++)
            {
                if (munitions[0].Bounds.IntersectsWith(enemies[i].Bounds)
                    || munitions[1].Bounds.IntersectsWith(enemies[i].Bounds)
                    || munitions[2].Bounds.IntersectsWith(enemies[i].Bounds))
                {
                    explosions.controls.play();
                    score += 1;
                    scorelbl.Text = (score < 10) ? "0" + score.ToString() : score.ToString();
                    if (score % 30 == 0) 
                    {
                        level += 1;
                        levellbl.Text = (level < 10) ? "0" + level.ToString() : level.ToString();
                        if (enemiesSpeed <= 10 && enemiesMunitionSpeed <= 10 && dificulty >= 0) 
                        {
                            dificulty--;
                            enemiesSpeed++;
                            enemiesMunitionSpeed++;
                        }
                        if (level == 10) 
                        {
                            GameOver("WOW - Well Done !!!");
                        }
                    }

                    enemies[i].Location = new Point((i + 1) * 50, -100);
                }
                if (Player.Bounds.IntersectsWith(enemies[i].Bounds)) 
                {
                    explosions.settings.volume = 30;
                    explosions.controls.play();
                    Player.Visible = false;
                    GameOver("Game Over");
                }
            }
        
        }
        private void GameOver(string str)
        {
            label1.Text = str;
            label1.Location = new Point(120, 100);
            label1.Visible = true;
            ReplayBtn.Visible = true;
            ExitBtn.Visible = true;
            gameMedia.controls.stop();
            StopTimers();
        }
        //stop timers
        private void StopTimers ()
        {
            MoveBgTimer.Stop();
            MoveEnemieTimer.Stop();
            MoveMunitionTimer.Stop();
            EnemiesMunitionTimer.Stop();
        }
        private void StartTimers() 
        {
            MoveBgTimer.Start();
            MoveEnemieTimer.Start();
            MoveMunitionTimer.Start();
            EnemiesMunitionTimer.Start();

        }

        private void EnemiesMunitionTimer_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < (enemiesMunition.Length - dificulty); i++)
            {
                if (enemiesMunition[i].Top < this.Height)
                {
                    enemiesMunition[i].Visible = true;
                    enemiesMunition[i].Top += enemiesMunitionSpeed;
                    CollitionWithEnemiesMunition();
                }
                else
                {
                    enemiesMunition[i].Visible = false;
                    int x = rnd.Next(0, 10);
                    enemiesMunition[i].Location = new Point(enemies[x].Location.X + 20, enemies[x].Location.Y + 30);

                }
            }
        }
        private void CollitionWithEnemiesMunition() 
        {
            for (int i = 0; i < enemiesMunition.Length; i++) 
            {
                if (enemiesMunition[i].Bounds.IntersectsWith(Player.Bounds)) 
                {
                    enemiesMunition[i].Visible = false;
                    explosions.settings.volume = 30;
                    explosions.controls.play();
                    Player.Visible = false;
                    GameOver("Game Over");
                }
            }


        }

        private void ReplayBtn_Click(object sender, EventArgs e)
        {
            this.Controls.Clear();
            StopTimers();
            InitializeComponent();
            StartTimers();
            Load -= Form1_Load; //ontlaad het form

            Form1_Load(sender, e);
        }

        private void ExitBtn_Click(object sender, EventArgs e)
        {
            Environment.Exit(1);
        }
    }
}

The drawings of the corona shooter game:

Shooter Corona Game – sprites: Yes, the stars are just picture boxes. The viruses in this game are drawings. But you have to be something creative, if you want to claim that you made the game yourself. Then make your own drawings. Or download the compiled version (top of the page). In that zip are also the drawings and sound files. But if you replace it with spaceships, you immediately have a space shooter game 🙂


The end ‘Shooter Corona Game

Back to Computer – index

Is this to difficult? Then you can look at Just Basic Basketball Game. This really is a programming language for beginners.