C# Tutorial – Make a multiple Level game in Windows Form Application

Hi in this tutorial we will cover how to add multiple levels in a windows form application game made in visual studio. In the past we have done some very exciting games but they are all single level games and we have added lots of different interactivity and challenges for the player, this time you will see how to add another level to the game where you can collect a key and open the door it will move you to level 2 and in level 2 you do the same to back to level 1. Note – there are better ways to do this, i found this way to be the most easiest to understand and experiment with.

Lesson objectives –

  1. Learn how to make extra levels for a windows form game with C# and visual studio
  2. Understand how windows form work and how to create new ones inside of a existing project
  3. How to activate the new level when a goal is met for example you collected the key and now at the door
  4. Move on to level of the game and then come back to level one in real time
  5. Use player navigation and picture box collision to create a interactive experience

Video Tutorial

Download the images for this project here

Full Source Code –

Level 1 Source Code –

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Moving_Through_Multiple_Levels_MOOICT
{
    public partial class Form1 : Form
    {
        bool goUp, goDown, goLeft, goRight;
        int playerSpeed = 8;
        bool gotKey;

        public Form1()
        {
            InitializeComponent();
        }

        private void TimerEvent(object sender, EventArgs e)
        {
            if (goLeft)
            {
                player.Left -= playerSpeed;
            }
            if (goRight)
            {
                player.Left += playerSpeed;
            }
            if (goDown)
            {
                player.Top += playerSpeed;
            }
            if (goUp)
            {
                player.Top -= playerSpeed;
            }

            if (key.Bounds.IntersectsWith(player.Bounds))
            {
                gotKey = true;
                key.Visible = false;
            }

            if (door.Bounds.IntersectsWith(player.Bounds) && gotKey)
            {

                Level2 newLevel = new Level2();
                this.Hide();
                GameTimer.Stop();
                gotKey = false;
                newLevel.Show();
            }
        }

        private void KeyIsDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                goLeft = true;
            }
            if (e.KeyCode == Keys.Right)
            {
                goRight = true;
            }
            if (e.KeyCode == Keys.Up)
            {
                goUp = true;
            }
            if (e.KeyCode == Keys.Down)
            {
                goDown = true;
            }
        }

        private void KeyIsUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                goLeft = false;
            }
            if (e.KeyCode == Keys.Right)
            {
                goRight = false;
            }
            if (e.KeyCode == Keys.Up)
            {
                goUp = false;
            }
            if (e.KeyCode == Keys.Down)
            {
                goDown = false;
            }
        }

        private void OnFormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}

Level 2 Source Code –

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Moving_Through_Multiple_Levels_MOOICT
{
    public partial class Level2 : Form
    {
        bool goUp, goDown, goLeft, goRight;
        int playerSpeed = 8;
        bool gotKey;

        public Level2()
        {
            InitializeComponent();
        }

        private void TimerEvent(object sender, EventArgs e)
        {
            if (goLeft)
            {
                player.Left -= playerSpeed;
            }
            if (goRight)
            {
                player.Left += playerSpeed;
            }
            if (goDown)
            {
                player.Top += playerSpeed;
            }
            if (goUp)
            {
                player.Top -= playerSpeed;
            }

            if (key.Bounds.IntersectsWith(player.Bounds))
            {
                gotKey = true;
                key.Visible = false;
            }

            if (door.Bounds.IntersectsWith(player.Bounds) && gotKey)
            {

                Form1 newLevel = new Form1();
                this.Hide();
                GameTimer.Stop();
                gotKey = false;
                newLevel.Show();
            }
        }

        private void KeyIsDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                goLeft = true;
            }
            if (e.KeyCode == Keys.Right)
            {
                goRight = true;
            }
            if (e.KeyCode == Keys.Up)
            {
                goUp = true;
            }
            if (e.KeyCode == Keys.Down)
            {
                goDown = true;
            }
        }

        private void KeyIsUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                goLeft = false;
            }
            if (e.KeyCode == Keys.Right)
            {
                goRight = false;
            }
            if (e.KeyCode == Keys.Up)
            {
                goUp = false;
            }
            if (e.KeyCode == Keys.Down)
            {
                goDown = false;
            }
        }

        private void OnFormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}

 




Comments are closed.