C# Tutorial – Create a Breakout game in Visual Studio

In this tutorial we will learn how to create/develop a fully working breakout game using c# and visual studio.  You will learn how to create multiple picture boxes and use them inside of a loop using c# programming language.  This tutorial is aimed for the beginners who are learning how to program and what better way to learn than to clone an all time classic. In this tutorial we are not using any other libraries such as XNA, MonoGame, DirectX or any other. We are simply going use a windows form application and C# programming to completely create a game from scratch.

Lesson Objectives –

1) Create a simple break out game using button, picture boxes and timer

2) Create a control loop to loop through system components and identify picture boxes

3) Create picture boxes with different tags and identify them using c#

4) Keeping score within the game

5) Execute win or lose scenario for the player

6) Animate paddle and ball to give rewarding game play for the user

 

Full Video Tutorial

Download Breakout Game Project on GitHub

Full 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 Breakout_Game_MOO_ICT
{
    public partial class Form1 : Form
    {

        bool goLeft;
        bool goRight;
        bool isGameOver;
        
        int score;
        int ballx;
        int bally;
        int playerSpeed;

        Random rnd = new Random();

        PictureBox[] blockArray;

        public Form1()
        {
            InitializeComponent();

            PlaceBlocks();
        }

        private void setupGame()
        {
            isGameOver = false;
            score = 0;
            ballx = 5;
            bally = 5;
            playerSpeed = 12;
            txtScore.Text = "Score: " + score;

            ball.Left = 376;
            ball.Top = 328;

            player.Left = 347;


            gameTImer.Start();

            foreach(Control x in this.Controls)
            {
                if(x is PictureBox && (string)x.Tag == "blocks")
                {
                    x.BackColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
                }
            }
        }


        private void gameOver(string message)
        {
            isGameOver = true;
            gameTImer.Stop();

            txtScore.Text = "Score: " + score + " " + message;
        }

        private void PlaceBlocks()
        
        {
            blockArray = new PictureBox[15];

            int a = 0;

            int top = 50;
            int left = 100;

            for(int i = 0; i < blockArray.Length; i++)
            {
                blockArray[i] = new PictureBox();
                blockArray[i].Height = 32;
                blockArray[i].Width = 100;
                blockArray[i].Tag = "blocks";
                blockArray[i].BackColor = Color.White;


                if(a == 5)
                {
                    top = top + 50;
                    left = 100;
                    a = 0;
                }

                if(a < 5)
                {
                    a++;
                    blockArray[i].Left = left;
                    blockArray[i].Top = top;
                    this.Controls.Add(blockArray[i]);
                    left = left + 130;
                }

            }
            setupGame();
        }

        private void removeBlocks()
        {
            foreach(PictureBox x in blockArray)
            {
                this.Controls.Remove(x);
            }
        }



        private void mainGameTimerEvent(object sender, EventArgs e)
        {
            txtScore.Text = "Score: " + score;

            if (goLeft == true && player.Left > 0)
            {
                player.Left -= playerSpeed;
            }

            if (goRight == true && player.Left < 700)
            {
                player.Left += playerSpeed;
            }

            ball.Left += ballx;
            ball.Top += bally;

            if (ball.Left < 0 || ball.Left > 775)
            {
                ballx = -ballx;
            }
            if (ball.Top < 0)
            {
                bally = -bally;
            }

            if (ball.Bounds.IntersectsWith(player.Bounds))
            {

                ball.Top = 463;

                bally = rnd.Next(5, 12) * -1;

                if (ballx < 0)
                {
                    ballx = rnd.Next(5, 12) * -1;
                }
                else
                {
                    ballx = rnd.Next(5, 12);
                }
            }

            foreach (Control x in this.Controls)
            {
                if (x is PictureBox && (string)x.Tag == "blocks")
                {
                    if(ball.Bounds.IntersectsWith(x.Bounds))
                    {
                        score += 1;

                        bally = -bally;

                        this.Controls.Remove(x);
                    }
                }

            }


            if(score == 15)
            {
                gameOver("You Win!! Press Enter to Play Again");
            }

            if(ball.Top > 580)
            {
                gameOver("You Lose!! Press Enter to try again");
            }



        }

        private void keyisdown(object sender, KeyEventArgs e)
        {

            if(e.KeyCode == Keys.Left)
            {
                goLeft = true;
            }
            if(e.KeyCode == Keys.Right)
            {
                goRight = 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.Enter && isGameOver == true)
            {
                removeBlocks();
                PlaceBlocks();
            }
        }
    }
}



13 responses to “C# Tutorial – Create a Breakout game in Visual Studio”

  1. Danial says:

    i have followed the exact codes but it is still not working. please help

  2. Anhar Ali says:

    Where is the error?

  3. Anhar Ali says:

    What errors are showing? If you check them and post it here it would be helpful to pin it down.

  4. Jean Melkovsky says:

    A question: why did we make the player a button instead of a picture box?

  5. Anhar Ali says:

    There was no specific reason except I wanted to see if a button can be used to animate and interact with other objects.

  6. Syaadahr says:

    The ball and player didn’t move?? Pls help

  7. Nelson says:

    My error is the ‘private’ it says “The modifier ‘private’ is not valid for this item”.

  8. Anhar Ali says:

    Which line is this private error showing on?

  9. Nelson says:

    124, where there is private void gameOver()

  10. Anhar Ali says:

    Check the curly brackets. It might be because the function before this function is not closed. This function only stops the timer from within. Page 13 of the tutorial will show the code you need double check the codes from there.

  11. Nelson says:

    Alright, thank you i’ll try that.

  12. Nelson says:

    I can now open the game but the picture boxes are not getting hit by the ball, the ball goes through them :/

  13. saalam says:

    How i implement restart code hen game is over