简体   繁体   中英

Simon Game in C#

I've been creating the game Simon in a windows form using C#. I'm having a problem in the labels that blink to show the pattern. When one label is required to blink twice (because it appears in the pattern twice) it will only blink once. Also, in general the labels will sometimes not blink in the correct order they are meant to (ie the second in the pattern blinks before the first). Any assistance in how to fix this or in general how to improve on my code would be great. I have only been using C# for the last few weeks and it's part of a university project. Have attached the code and a picture of what the windows form looks like. Windows Form

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

namespace Simon2
{
    public partial class Form1 : Form
    {
        List<int> sequence = new List<int>();
        Random rnd = new Random();
        int number = 0;

        public Form1()
        {
            InitializeComponent();

            sequence.Add(rnd.Next(0, 4));
            hey();
        }

        void hey() 
        {
            foreach (int colour in sequence)
            {
                switch (colour)
                {
                    case 0: {
                        timer1.Enabled = true;
                        break;
                    }
                    case 1: {
                        timer2.Enabled = true;
                        break;
                    }
                    case 2: {
                        timer3.Enabled = true;
                        break;
                    }
                    case 3: {
                        timer4.Enabled = true;
                        break;
                    }
                }
            }
        }

        void pattern(int colour)
        {
            if (sequence[number] == colour)
            {                   
                label1.Text = ("Score: " + sequence.Count);
                sequence.Add(rnd.Next(0, 4));
                number = 0;
                hey();
            }
            else
            {
                MessageBox.Show("Fail!");
                Application.Exit();
            }                 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Red1.BackColor == Color.Transparent)
            {
                Red1.BackColor = Color.Red;
                timer1.Interval = 300;
            }
            else
            {
                Red1.BackColor = Color.Transparent;
                timer1.Interval = 300;
                timer1.Stop();                  
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (Blue1.BackColor == Color.Transparent)
            {
                Blue1.BackColor = Color.Blue;
                timer2.Interval = 300;
            }
            else
            {
                Blue1.BackColor = Color.Transparent;
                timer2.Interval = 300;
                timer2.Stop();                  
            }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            if (Yellow1.BackColor == Color.Transparent)
            {
                Yellow1.BackColor = Color.Yellow;
                timer3.Interval = 300;
            }
            else
            {
                Yellow1.BackColor = Color.Transparent;
                timer3.Interval = 300;
                timer3.Stop();                 
            }
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            if (Green1.BackColor == Color.Transparent)
            {
                Green1.BackColor = Color.Lime;
                timer4.Interval = 300;
            }
            else
            {
                Green1.BackColor = Color.Transparent;
                timer4.Interval = 300;
                timer4.Stop();                  
            }
        }

        private void Red_Click(object sender, EventArgs e)
        {
            pattern(0);             
        }

        private void Blue_Click(object sender, EventArgs e)
        {
            pattern(1);
        }

        private void Yellow_Click(object sender, EventArgs e)
        {
            pattern(2);
        }

        private void Green_Click(object sender, EventArgs e)
        {
            pattern(3);
        }
    }
}

I am not familiar with the game itself, my understanding is that one light after the other has to light up. My suggestion: Use Thread.sleep (UI will not be responsive while this does it's thing), instead of timers, directly in the switch:

switch (colour){
            case 0: {
                Red1.BackColor = Color.Red;
                Thread.Sleep(500);
                Red1.BackColor = Color.Transparent;
                break;
            }

edit: a better way would be to use a while loop which checks if a certain amount of ms elapsed and put Application.DoEvents(); in there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM