gpt4 book ai didi

c# 内存游戏,需要在游戏结束时添加消息

转载 作者:太空宇宙 更新时间:2023-11-03 15:12:47 24 4
gpt4 key购买 nike

我使用 Windows 窗体 (c#) 创建了一个内存游戏,游戏已经完成,但我很难添加最后一部分,当所有卡片都匹配时,需要向用户显示一个消息框,例如“干得好!所有卡片都已配对”。

这是一段代码,我认为将插入 MessageBox.Show 的代码:

      private void card1_Click(object sender, EventArgs e)
//if the first slot of pendingImages is available put this card there for comparison
{
//turn card over
card1.Image = Properties.Resources.Image1;
//if this is the first card to be turned over, save its image
if (pendingImage1 == null)
{
pendingImage1 = card1;
}
//else check if pendingImage 2 is available then store the card here for comparison
else if(pendingImage1 != null && pendingImage2 == null)
{
pendingImage2 = card1;
}
//if both pendingImage slots are filled then compare the cards
if (pendingImage1 != null && pendingImage2 != null)
{
if (pendingImage1.Tag == pendingImage2.Tag)
{
//clear the variables to be used again
pendingImage1 = null;
pendingImage2 = null;
//once the cards are matched and turned permanentaly, disable the card to make it unclickable
card1.Enabled = false;
dupCard1.Enabled = false;
//add 10 points to the score evry time cards match
scoreSheet.Text = Convert.ToString(Convert.ToInt32(scoreSheet.Text) + 10);
}
else
{
flipDuration.Start();
}
}
}

private void dupCard1_Click(object sender, EventArgs e)
{
dupCard1.Image = Properties.Resources.Image1;
if (pendingImage1 == null)
{
pendingImage1 = dupCard1;
}
else if (pendingImage1 != null && pendingImage2 == null)
{
pendingImage2 = dupCard1;
}
if (pendingImage1 != null && pendingImage2 != null)
{
if (pendingImage1.Tag == pendingImage2.Tag)
{
pendingImage1 = null;
pendingImage2 = null;
card1.Enabled = false;
dupCard1.Enabled = false;
scoreSheet.Text = Convert.ToString(Convert.ToInt32(scoreSheet.Text) + 10);
}
else
{
flipDuration.Start();
}
}
}

这 2 张是总共 18 张卡片中的第 2 张,但这是仅适用于所有卡片的代码 Properties.Resources.Image1 从 Image.1 更改为 Image.2、.3、.4 等。

我不确定什么代码可以让我在所有 9 张(总共 18 张牌)对牌都匹配后让游戏显示一个消息框。

任何帮助将不胜感激。

最佳答案

不确定这是否是处理此问题的“最佳”方式,但您可以跟踪已匹配的卡牌总数以及获胜所需的匹配次数。创建一个名为“winCount”的全局变量和另一个名为“currentMatches”的变量。 winCount 可以在代码中手动设置为 9 int winCount = 9,当 currentMatches == winCount 时,游戏会弹出“You Win”。

例如:

int winCount = 9;
int currentMatches = 0;

private void card1_Click(object sender, EventArgs e)
//if the first slot of pendingImages is available put this card there for comparison
{
//turn card over
card1.Image = Properties.Resources.Image1;
//if this is the first card to be turned over, save its image
if (pendingImage1 == null)
{
pendingImage1 = card1;
}
//else check if pendingImage 2 is available then store the card here for comparison
else if(pendingImage1 != null && pendingImage2 == null)
{
pendingImage2 = card1;
}
//if both pendingImage slots are filled then compare the cards
if (pendingImage1 != null && pendingImage2 != null)
{
if (pendingImage1.Tag == pendingImage2.Tag)
{
//clear the variables to be used again
pendingImage1 = null;
pendingImage2 = null;
//once the cards are matched and turned permanentaly, disable the card to make it unclickable
card1.Enabled = false;
dupCard1.Enabled = false;
//add 10 points to the score evry time cards match
scoreSheet.Text = Convert.ToString(Convert.ToInt32(scoreSheet.Text) + 10);

//NEW CODE
currentMatches ++;
if(currentMatches == winCount)
{
MessageBox.Show("Congratulations! You Win!)
return;
}
}
else
{
flipDuration.Start();
}
}
}

private void dupCard1_Click(object sender, EventArgs e)
{
dupCard1.Image = Properties.Resources.Image1;
if (pendingImage1 == null)
{
pendingImage1 = dupCard1;
}
else if (pendingImage1 != null && pendingImage2 == null)
{
pendingImage2 = dupCard1;
}
if (pendingImage1 != null && pendingImage2 != null)
{
if (pendingImage1.Tag == pendingImage2.Tag)
{
pendingImage1 = null;
pendingImage2 = null;
card1.Enabled = false;
dupCard1.Enabled = false;
scoreSheet.Text = Convert.ToString(Convert.ToInt32(scoreSheet.Text) + 10);
}
else
{
flipDuration.Start();
}
}
}

关于c# 内存游戏,需要在游戏结束时添加消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40383022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com