使用计时器更改颜色块的有效方法

时间:2014-08-25 15:18:28

标签: c# timer colors

我是编程新手,过去3个月我一直在用C#编程,现在我正在尝试在WP8.1上创建一个应用程序,其中包括创建一个包含4个块的屏幕,分配它的计时器,并改变4个块的颜色作为计时器“滴答”。

目标是块的颜色没有重复(例如,如果第一个块是红色,那么它不能是蓝色或绿色......)

我已达到目标,但我认为我的代码有点草率,这就是我在这里的原因..

public void ColorTimerChange(object sender, object args)
{
    count++;
    //Sets the text of a TextBlock so that I can check the time
    TimerTB.Text = count.ToString();
    //Creates a new Random instance
    Random rand = new Random();


    if (count % count == 0)
    {
        //Generates random numbers between 1 and 4 for every color.. If there are numbers that are equal, generate again
        RandBlue = rand.Next(1, 5);
        RandGreen = rand.Next(1, 5);
        RandRed = rand.Next(1, 5);
        while (RandBlue == RandGreen || RandBlue == RandRed || RandGreen == RandRed)
        {
            RandBlue = rand.Next(1, 5);
            RandGreen = rand.Next(1, 5);
            RandRed = rand.Next(1, 5);
        }
        //Textblocks so that I can check the random numbers 
        RedTB.Text = RandRed.ToString();
        RandomTB.Text = RandBlue.ToString();
        GreenTb.Text = RandGreen.ToString();


            switch (RandRed)
            {
               //if the random number for red is equal to 1, change the color of the 1st block to red. If the others random numbers are different to 2, change the color of the 2nd block to white, and so on.
                case(1):
                    {
                        Block1.Fill = color.Red;
                        if (RandGreen != 2 && RandBlue != 2)
                            Block2.Fill = color.White;
                        if (RandGreen != 3 && RandBlue != 3)
                            Block3.Fill = color.White;
                        if (RandGreen != 4 && RandBlue != 4)
                            Block4.Fill = color.White;
                        break;
                    }
                case (2):
                    {
                        Block2.Fill = color.Red;
                        if (RandGreen != 1 && RandBlue != 1)
                            Block1.Fill = color.White;
                        if (RandGreen != 3 && RandBlue != 3)
                            Block3.Fill = color.White;
                        if (RandGreen != 4 && RandBlue != 4)
                            Block4.Fill = color.White;
                        break;
                    }
                case (3):
                    {
                        Block3.Fill = color.Red;
                        if (RandGreen != 2 && RandBlue != 2)
                            Block2.Fill = color.White;
                        if (RandGreen != 1 && RandBlue != 1)
                            Block1.Fill = color.White;
                        if (RandGreen != 4 && RandBlue != 4)
                            Block4.Fill = color.White;
                        break;
                    }
                case (4):
                    {
                        Block4.Fill = color.Red;
                        if (RandGreen != 2 && RandBlue != 2)
                            Block2.Fill = color.White;
                        if (RandGreen != 3 && RandBlue != 3)
                            Block3.Fill = color.White;
                        if (RandGreen != 1 && RandBlue != 1)
                            Block1.Fill = color.White;
                        break;
                    }
                default:
                    break;
            }

            ...
}

这是红色的示例..我为绿色和蓝色做了相同的开关。 颜色在“Helperz.cs”中定义,我为我在代码中找到的每种颜色创建了新的SolidBrushColor(Colors.color)。

我怎么能做同样的事情,但不必“复制”粘贴,并为每个颜色/块一遍又一遍地做同样的事情。

提前谢谢大家!

1 个答案:

答案 0 :(得分:0)

Block保存在数组中。然后你可以简单地说:

Blocks[RandRed] = Color.Red;

和其他颜色相同。