我正在制作一个记忆游戏,我想要,当点击 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;
}
}
如果我在没有构建和运行示例应用程序的情况下正确理解案例,则序列是:
点击第一张卡片 - 一切都很好
点击第二张卡片:
它显示前面 ( 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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句