我已经使用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();
}
}
}
这两个是18张牌中的前2张,但这是所有人的代码,只有属性.Resources.Image1从Image.1改为Image.2,.3,.4等。< / p>
我不知道一旦所有9张(总共18张卡)的牌匹配,我会让游戏显示一个消息框的代码。
非常感谢任何帮助。
答案 0 :(得分:0)
你必须计算比赛数。每次玩家配对两张图像时,都应增加一个匹配计数器。
我会把这段代码放在这里:
if (pendingImage1.Tag == pendingImage2.Tag)
{
/* your existing code */
matches++;
if (matches == 9)
{
MessageBox.Show("Congratulations. You've successfully paired all the images.");
}
}
一部分。
也许这不是一个复制粘贴代码,但我希望你明白这个想法。 :)
答案 1 :(得分:0)
试试这个
using System.Windows.Forms;
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);
int TotalPoints = Convert.ToInt32(scoreSheet.Text);
if(TotalPoints >= 100){
MessageBox.Show("Your message");
}
}
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);
int TotalPoints = Convert.ToInt32(scoreSheet.Text);
if(TotalPoints >= 100){
MessageBox.Show("Your message");
}
}
else
{
flipDuration.Start();
}
}
}
答案 2 :(得分:0)
不确定这是否是最好的&#34;处理此问题的方法,但您可以跟踪已匹配的卡的总数以及获胜所需的匹配数。创建一个名为&#34; winCount&#34;的全局变量另一个名为&#34; currentMatches&#34;的变量。 winCount可以在代码中手动设置为9 int winCount = 9
,游戏显示&#34; You Win&#34;在currentMatches == winCount
时弹出。
例如:
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();
}
}
}