代码必须先做一件事,然后等待几秒钟,然后继续

时间:2018-10-20 09:50:47

标签: c# wpf

我正在做一个记忆游戏,我希望当单击2张卡片时,它们再次转过身并向后仰。正如您在我的代码中看到的那样,我计算的是点击次数,当“ NumberOfCLicks”达到2时,将调用“ resetCards”方法。它会完成应该做的事情,将两张卡都翻转,但在再次翻转之前不会先显示第二张卡的正面。

我的代码:

public class MemoryGrid
{
    private Grid grid;
    private int rows, cols;

    public MemoryGrid(Grid grid, int rows, int cols)
    {
        this.grid = grid;
        this.rows = rows;
        this.cols = cols;

        InitializeGrid();
        AddImages();
    }


    private void InitializeGrid()
    {
        for (int i = 0; i < rows; i++)
        {
            grid.RowDefinitions.Add(new RowDefinition());
        }
        for (int i = 0; i < cols; i++)
        {
            grid.ColumnDefinitions.Add(new ColumnDefinition());
        }
    }

    private void AddImages()
    {
        List<ImageSource> images = GetImagesList();
        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < cols; col++)
            {
                Image back = new Image();
                back.Source = new BitmapImage(new Uri("/images/back.png", UriKind.Relative));

                back.MouseDown += new System.Windows.Input.MouseButtonEventHandler(CardClick);

                back.Tag = images.First();
                images.RemoveAt(0);
                Grid.SetColumn(back, col);
                Grid.SetRow(back, row);
                grid.Children.Add(back);
            }
        }
    }



    static int numberOfClicks = 0;
    private Image card;
    static int score;
    private Image Image1;
    private Image Image2;



    private void CardClick(object sender, MouseButtonEventArgs e)
    {
        Image card = (Image)sender;
        ImageSource front = (ImageSource)card.Tag;
        card.Source = front;
        numberOfClicks++;

        checkCards(card);
    }

    private void checkCards(Image card)
    {

        this.card = card;
        if (numberOfClicks < 2 || numberOfClicks == 2)
        {

            if (this.Image1 == null)
            {
                Image1 = card;
            }
            else if (this.Image2 == null)
            {
                Image2 = card;
            }
        }

        if (numberOfClicks == 2)
        {
            checkPair();

            numberOfClicks = 0;
            Image1 = null;
            Image2 = null;
        }
    }

    public void checkPair()
    {
        resetCards(Image1, Image2);
       // more code here to check if the 2 cards are a pair.
       // First only a reset after 2 cards.
    }

    private void resetCards(Image card1, Image card2)
    {
        this.Image1 = card1;
        this.Image2 = card2;


        card1.Source = new BitmapImage(new Uri("/images/back.png", UriKind.Relative));
        card2.Source = new BitmapImage(new Uri("/images/back.png", UriKind.Relative));
    }

    public List<ImageSource> GetImagesList()
    {
        List<ImageSource> images = new List<ImageSource>();
        List<string> random = new List<string>();

        for (int i = 0; i < 16; i++)
        {

            int imageNR = 0;

            Random rnd = new Random();
            imageNR = rnd.Next(1, 17);
            if (random.Contains(Convert.ToString(imageNR)))
            {
                i--;
            }
            else
            {
                random.Add(Convert.ToString(imageNR));
                ImageSource source = new BitmapImage(new Uri("images/" + imageNR + ".png", UriKind.Relative));
                images.Add(source);
            }
        }
        return images;
    }
}

3 个答案:

答案 0 :(得分:1)

如果我在没有构建和运行示例应用程序的情况下正确理解了这种情况,则顺序为:

点击第一张卡-一切正常

点击第二张卡片:

  • 它显示在前面(card.Source = front;

  • 执行checkCards

  • 执行checkPair是因为numberOfClicks == 2

  • 执行resetCards,卡片会非常快速地显示背面图片。没注意到正面图片

在显示图像之前引入一些延迟:

private bool hasDelay;
private async void resetCards(Image card1, Image card2)
{
    this.Image1 = card1;
    this.Image2 = card2;

    hasDelay = true;
    await Task.Delay(2000);

    card1.Source = new BitmapImage(new Uri("/images/back.png", UriKind.Relative));
    card2.Source = new BitmapImage(new Uri("/images/back.png", UriKind.Relative));
    hasDelay = false;
}

请勿延迟处理点击!

private void CardClick(object sender, MouseButtonEventArgs e)
{
    if (hasDelay) return;

    Image card = (Image)sender;
    ImageSource front = (ImageSource)card.Tag;
    card.Source = front;
    numberOfClicks++;

    checkCards(card);
}

答案 1 :(得分:0)

最简单的方法是使用Thread.Sleep方法:

Thread.Sleep(2000);

但是我建议您使用Tasks。这为您提供了一个异步过程,并且该接口不会冻结。然后您可以执行以下操作:

    public static async Task TaskMethod()
    {
        Debug.WriteLine("Start Waiting");
        Task t = Task.Run(() => DoSomething() );
    }

    private static void DoSomething()
    {
        Thread.Sleep(3000);
        Console.WriteLine("Wake up !");
    }

答案 2 :(得分:-1)

如果显示第二张卡片时代码仅需等待而不执行任何操作,则只需使用System.Threading.Thread.Sleep(1000);命令。您看到的“ 1000”表示它将等待1秒钟。 2000将是2秒,依此类推。该命令只是在给定的时间内阻止下一步发生。我希望这会有所帮助!

祝你好运!

编辑:我刚刚被告知这行不通。抱歉!