C# 中的西蒙游戏

Simon Game in C#

我一直在使用 C# 以 windows 形式创建游戏 Simon。我在闪烁以显示图案的标签中遇到问题。当要求一个标签闪烁两次时(因为它在图案中出现两次)它只会闪烁一次。此外,一般来说,标签有时不会以正确的顺序闪烁(即模式中的第二个在第一个之前闪烁)。任何关于如何解决这个问题或一般如何改进我的代码的帮助都会很棒。我最近几周才使用 C#,它是大学项目的一部分。 附上 windows 形式的代码和图片。 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);
        }
    }
}

我对游戏本身不熟悉,我的理解是一盏接着一盏灯要亮。 我的建议:直接在开关中使用 Thread.sleep(UI 不会有响应),而不是计时器,直接在开关中使用:

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

编辑: 更好的方法是使用 while 循环来检查是否经过了一定数量的 ms 并放置 Application.DoEvents();在那里