Create a Simon Says Style Game in Windows Forms and C# with Visual Studio
- Subject: C# Tutorials
- Learning Time: 2 hours
Hi, in this tutorial we will make a Simon Says style using windows form and C# programming. This game was a fun problem to solve as it required some out of the box thinking. In the beginning the idea was simple the game picks 3 blocks from the screen on 4×4 grid. We save the sequence and we need to blink those blocks to change colour in the same sequence so the user will know which box the game picked. First problem to solve was how to save the sequence information for the player and the game also we needed to figure out how to do the blink animation for the picture boxes. It was challenging but the solution turned out how to be more simple than the problem. We wanted to strictly use 1 timer in the game so this way we don’t need to keep starting and stopping multiple timers in the form so in this game we will use one timer but keep track of the blinking animation using a simple switch statement. Also we managed to make the game so when you score 3 it will level up by one so the game becomes more challenging the more you play it. We hope you find this tutorial useful and also you can make a lot more projects using the similar methods as this tutorial. Keep learning and keep making things.
Lesson Objectives:
- Create a Simon Says Style in Windows Form and C#
- Generate a 4×4 Grid of picture boxes through the code
- Add Click events to those picture boxes
- Assign a random colour each time the start button is clicked on those picture boxes
- Allow the game to randomly pick 3 picture boxes
- Animate those picture boxes by changing their colour to white in sequential animation
- Allow the player to follow that sequence and click on the picture boxes
- if the sequences match then show a message box of success
- If player score 3 then move up one level and add one more box to the sequence
Video Tutorial
Simon Says Game Source Code –
using System.Diagnostics; namespace Simon_Says_Game_MOO_ICT { public partial class Form1 : Form { int blocksX = 160; int blocksY = 80; int score = 0; int level = 3; List<PictureBox> pictureBoxes = new List<PictureBox>(); List<PictureBox> chosenBoxes = new List<PictureBox>(); Random rand = new Random(); Color temp; int index = 0; int tries = 0; int timeLimit = 0; bool selectingColours = false; string correctOrder = string.Empty; string playerOrder = string.Empty; public Form1() { InitializeComponent(); SetUpBlocks(); } private void GameTimerEvent(object sender, EventArgs e) { if (selectingColours) { timeLimit++; switch (timeLimit) { case 10: temp = chosenBoxes[index].BackColor; chosenBoxes[index].BackColor = Color.White; break; case 20: chosenBoxes[index].BackColor = temp; break; case 30: chosenBoxes[index].BackColor = Color.White; break; case 40: chosenBoxes[index].BackColor = temp; break; case 50: if (index < chosenBoxes.Count - 1) { index++; timeLimit = 0; } else { selectingColours = false; } break; } } if (tries >= level) { if (correctOrder == playerOrder) { tries = 0; GameTimer.Stop(); MessageBox.Show("Well done, you got them correctly.", "MOO Says: "); score++; } else { tries = 0; GameTimer.Stop(); MessageBox.Show("Your guesses did not match, try again.", "MOO Says: "); } } lblInfo.Text = "Click on " + level + " blocks in the same sequence."; } private void ButtonClickEvent(object sender, EventArgs e) { if (score == 3 && level < 7) { level++; score = 0; } correctOrder = string.Empty; playerOrder = string.Empty; chosenBoxes.Clear(); chosenBoxes = pictureBoxes.OrderBy(x => rand.Next()).Take(level).ToList(); for (int i = 0; i < chosenBoxes.Count; i++) { correctOrder += chosenBoxes[i].Name + " "; } foreach (PictureBox x in pictureBoxes) { x.BackColor = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256)); } Debug.WriteLine(correctOrder); index = 0; timeLimit = 0; selectingColours = true; GameTimer.Start(); } private void SetUpBlocks() { for (int i = 1; i < 17; i++) { PictureBox newPic = new PictureBox(); newPic.Name = "pic_" + i; newPic.Height = 60; newPic.Width = 60; newPic.BackColor = Color.Black; newPic.Left = blocksX; newPic.Top = blocksY; newPic.Click += ClickOnPictureBox; if (i == 4 || i == 8 || i == 12) { blocksY += 65; blocksX = 160; } else { blocksX += 65; } this.Controls.Add(newPic); pictureBoxes.Add(newPic); } } private void ClickOnPictureBox(object? sender, EventArgs e) { if (!selectingColours && chosenBoxes.Count > 1) { PictureBox temp = sender as PictureBox; temp.BackColor = Color.Black; playerOrder += temp.Name + " "; Debug.WriteLine(playerOrder); tries++; } else { return; } } } }