简体   繁体   English

C#中的Simon Game

[英]Simon Game in C#

I've been creating the game Simon in a windows form using C#. 我一直在使用C#在Windows窗体中创建游戏Simon。 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. 在过去的几周中,我只使用C#,这是大学项目的一部分。 Have attached the code and a picture of what the windows form looks like. 附带了代码和Windows窗体的外观图片。 Windows Form Windows表格

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: 我的建议:直接在开关中使用Thread.sleep(这样做时UI不会响应),而不是计时器:

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(); 编辑:一种更好的方法是使用while循环,该循环检查是否经过了一定量的ms并放入Application.DoEvents();。 in there 在那里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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