Я сделал эту игру на память, следуя инструкции для Microsoft: https://docs.microsoft.com/en-us/visualstudio/ide/step-9-try-other-features?view=vs-2019 и после добавления некоторых дополнительных функций, похоже, я где-то допустил ошибку. Я думаю, что проблема в метках переднего или заднего цветов.
Я добавил некоторый код, чтобы при щелчке по метке он изменял цвет с темно-зеленого на салатовый, а после нажатия обеих меток и истечения времени таймера он снова превращался в темный зеленый.
После добавления этого кода, после победы в игре сообщение не отображается, а таймер все еще работает.
Вот код:
namespace Matching_Game
public partial class Form1 : Form
{
Random random = new Random();
Label firstClicked = null;
Label secondClicked = null;
List<string> icons = new List<string>()
{
"!", "!", "N", "N", "$", "$", "k", "k",
"b", "b", "v", "v", "+", "+", "j", "j",
"r", "r", "o", "o", "%", "%", "d", "d",
"l", "l", "L", "L", "×", "×", "Y", "Y",
"¸", "¸", "~", "~"
};
int timePassed;
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyPress += (ss, ee) =>
{
if (ee.KeyChar == 27) //= escape
{
if (MessageBox.Show
(
"Zapri aplikacijo",
"Igra spomin",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button2 // hit Enter == No !
)
== DialogResult.Yes
)
{
Application.Exit();
}
}
};
AssignIconsToSquares();
}
private void AssignIconsToSquares()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLabel = control as Label;
if (iconLabel != null)
{
int randomNumber = random.Next(icons.Count);
iconLabel.Text = icons[randomNumber];
iconLabel.ForeColor = iconLabel.BackColor;
icons.RemoveAt(randomNumber);
}
}
}
private void label_Click(object sender, EventArgs e)
{
System.IO.Stream str1 = Properties.Resources.Card_flip_sound_effect;
System.Media.SoundPlayer snd1 = new System.Media.SoundPlayer(str1);
snd1.Play();
timer2.Start();
if (timer1.Enabled == true)
return;
Label clickedLabel = sender as Label;
clickedLabel.BackColor = Color.Lime;
if (clickedLabel != null)
{
if (clickedLabel.ForeColor == Color.Black)
{
return;
}
if (firstClicked == null)
{
firstClicked = clickedLabel;
firstClicked.ForeColor = Color.Black;
return;
}
secondClicked = clickedLabel;
secondClicked.ForeColor = Color.Black;
if (firstClicked.Text == secondClicked.Text)
{
firstClicked = null;
secondClicked = null;
System.IO.Stream str2 = Properties.Resources.Coin_collect_sound_effect;
System.Media.SoundPlayer snd2 = new System.Media.SoundPlayer(str2);
snd2.Play();
return;
}
CheckForWinner();
timer1.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
System.IO.Stream str1 = Properties.Resources.Error_tone;
System.Media.SoundPlayer snd1 = new System.Media.SoundPlayer(str1);
snd1.Play();
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked.BackColor = Color.DarkGreen;
secondClicked.BackColor = Color.DarkGreen;
firstClicked.ForeColor = Color.DarkGreen;
secondClicked.ForeColor = Color.DarkGreen;
firstClicked = null;
secondClicked = null;
}
private void CheckForWinner()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLabel = control as Label;
if (iconLabel.BackColor == Color.Lime)
{
return;
}
}
timer2.Stop();
System.IO.Stream str = Properties.Resources.Ta_da_orchestra_fanfare;
System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
snd.Play();
MessageBox.Show("Vsi pari so povezani!", "Čestitke!");
Close();
}
private void timer2_Tick(object sender, EventArgs e)
{
timePassed += 1;
timeLabel.Text = timePassed / 60 + ":" + ((timePassed % 60) >= 10 ? (timePassed % 60).ToString() : "0" + timePassed % 60);
}
}
Спасибо за любая помощь или идеи!