C# - Create Glass Bridge Game In C#

How To Create Glass Bridge Game From Squid Game Using C#

Glass Bridge Game In C#


in this c# form tutorial we will see how to make The Glass Stepping Stones Game or the Glass Bridge Game From Squid Game using panels and pictureboxes in csharp programming language.

game rules:
- the player have to hop across a bridge of glasses with some glasses that breack easily.

tools:
- c# programming language.
- microsoft visual studio express 2013.
- pixabay.com (images).

Watch This Full Demo



- The Project Source Code


using System;
using System.Collections;
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 csharp_glass_bridge
{
    public partial class Game_Form : Form
    {
        public Game_Form()
        {
            InitializeComponent();
        }

        // images
        Bitmap step = Properties.Resources.step;
        Bitmap nostep = Properties.Resources.nostep;

        // random to get random images
        Random random = new Random();
        Bitmap randomImage;

        // arraylist of pictureboxes
        PictureBox[,] picBoxes;
        // populate this array with random images
        // 6 - number of rows
        // 2 - number of columns
        Bitmap[,] imagesOrder = new Bitmap[6, 2];
        ArrayList list = new ArrayList();
        int counter = 0;
        bool won = true;

        private void Game_Form_Load(object sender, EventArgs e)
        {
            pictureBoxStart.Image = step;

            // populate the picBoxes with pictureboxes
            picBoxes = new[,] {{ pictureBox_1_1, pictureBox_1_2 },{ pictureBox_2_1, pictureBox_2_2 },
                               { pictureBox_3_1, pictureBox_3_2 },{ pictureBox_4_1, pictureBox_4_2 },
                               { pictureBox_5_1, pictureBox_5_2 },{ pictureBox_6_1, pictureBox_6_2 }
            };

            // add images to the list
            list.Add(step);
            list.Add(nostep);

            createRandomImages();

            AddEventToAllPictureboxes(null, null);

            enablePicBox(counter);

        }

        // button to restart the game
        private void buttonRestart_Click(object sender, EventArgs e)
        {
            createRandomImages();
            won = true;
            counter = 0;
            label_message.Text = "";
            enablePicBox(counter);
            pictureBoxFinish.Image = null;
            foreach(PictureBox pbox in picBoxes)
            {
                pbox.Image = null;
            }
        }


        // create a function to get random images
        public void createRandomImages()
        {
            for(int i = 0; i < 6; i++)
            {
                randomImage = (Bitmap)list[random.Next(list.Count)];
                imagesOrder[i, 0] = randomImage;

                if(randomImage.Equals(step))
                {
                    imagesOrder[i, 1] = nostep;
                }
                else
                {
                    imagesOrder[i, 1] = step;
                }
            }
        }


        // create an event click to all the pictureboxes in panel top
        public void paneltop_picBoxes_click(object sender, EventArgs e)
        {
            PictureBox pbox = (PictureBox)sender;
            if(pbox.Enabled)
            {
                pbox.Image = imagesOrder[counter, 1];

                // disable picturebox
                pbox.Enabled = false;
                picBoxes[counter, 0].Enabled = false;

                if (imagesOrder[counter, 1].Equals(nostep))
                {
                    won = false;
                }
                if (counter == 5 && won == true)
                {
                    pictureBoxFinish.Image = step;
                    label_message.Text = "You've Won";
                }
                else if (won == false)
                {
                   // MessageBox.Show("You've Lost");
                    label_message.Text = "You've Lost";
                }

                counter++;
                enablePicBox(counter);
            }
            
        }


        // create an event click to all the pictureboxes in panel bottom
        public void panelbottom_picBoxes_click(object sender, EventArgs e)
        {
            PictureBox pbox = (PictureBox)sender;
            if (pbox.Enabled)
            {
                pbox.Image = imagesOrder[counter, 0];

                // disable picturebox
                pbox.Enabled = false;
                picBoxes[counter, 1].Enabled = false;

                if (imagesOrder[counter, 0].Equals(nostep))
                {
                    won = false;
                }
                if (counter == 5 && won == true)
                {
                    pictureBoxFinish.Image = step;
                    label_message.Text = "You've Won";
                }
                else if (won == false)
                {
                    //MessageBox.Show("You've Lost");
                    label_message.Text = "You've Lost";
                }

                counter++;
                enablePicBox(counter);
            }
            
        }

        // add event to all the pictureboxes
        public void AddEventToAllPictureboxes(object sender, EventArgs e)
        {
            foreach(Control c in panel_top.Controls)
            {
                if(c is PictureBox)
                {
                    c.Click += new System.EventHandler(paneltop_picBoxes_click);
                }
            }

            foreach (Control c in panel_bottom.Controls)
            {
                if (c is PictureBox)
                {
                    c.Click += new System.EventHandler(panelbottom_picBoxes_click);
                }
            }
        }


        // create a function to enable picturebox
        public void enablePicBox(int index)
        {
            if(index <= 5)
            {
                picBoxes[index, 0].Enabled = true;
                picBoxes[index, 1].Enabled = true;
            }
            
        }
    }
}


OUTPUT:
Create Glass Bridge Game In C#

Glass Bridge Game In C#

Glass Stepping Stones Game In C#

Glass Stepping Stones Game Source Code In C#







Share this

Related Posts

Previous
Next Post »

1 comments:

comments