c#如何使匹配卡保持打开状态? (记忆游戏)

时间:2018-10-17 12:59:55

标签: c# wpf

我正在为学校项目制作的配对/记忆游戏有问题。 我希望在单击两个相同的卡后将它们保留下来,当单击两个不同的卡时,它们将返回到该卡的背面图像。

我是一个初学者,所以如果有人能够以一种非常简单的方式向我解释这一点,那将对我有很大帮助。目前,我对此感到困惑。

到目前为止,这是我的代码,用于从列表中加载图像并将它们放置在网格中:

private void AddImage()
    {

        List<ImageSource> images = GetImagesList();

        Random random = new Random();


        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < cols; col++)
            {
                Image ImageOnBacksideOfCard = new Image();
                ImageOnBacksideOfCard.Source = new BitmapImage(new Uri("project/achterkant.png", UriKind.Relative));

                //Ruimte tussen kaartjes
                Thickness margin = ImageOnBacksideOfCard.Margin;
                margin.Top = 10;
                ImageOnBacksideOfCard.Margin = margin;

                ImageOnBacksideOfCard.MouseDown += new MouseButtonEventHandler(CardClick);

                //Randomize

                positie1 = random.Next(images.Count);
                ImageOnBacksideOfCard.Tag = images[positie1];
                images.RemoveAt(positie1);

                Grid.SetColumn(ImageOnBacksideOfCard, col);
                Grid.SetRow(ImageOnBacksideOfCard, row);
                grid.Children.Add(ImageOnBacksideOfCard);


            }
        }


//Load pictures
private List<ImageSource> GetImagesList()
    {
        List<ImageSource> images = new List<ImageSource>();

        for (int i = 0; i < 16; i++)
        {
            int imageNr = i % 8 + 1;
            ImageSource source = new BitmapImage(new Uri("project/" + imageNr + ".jpg", UriKind.Relative));
            images.Add(source);
        }
        return images;
    }

 //Cards turn on click
    private void CardClick(object sender, MouseButtonEventArgs e)
    {
        Image card = (Image)sender;
        ImageSource front = (ImageSource)card.Tag;
        card.Source = front;
    }



    //Reset
    public void Reset()
    {
        grid.Children.Clear();
        AddImage();
    }

0 个答案:

没有答案