我正在制作一个石头剪刀布游戏和一个cpu。我按下石头,布或剪刀的按钮,并在计时器结束后检查cpu是否有相同的图片。
我的问题是什么都没发生。我只看到这两张图片。下面是代码:
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = seconds--.ToString();
if (label1.Text == "-1")
{
timer1.Stop();
Random r = new Random();
pictureBox2.Image = picture[r.Next(picture.Length)];
if (pictureBox1.Image == pictureBox2.Image)
{
MessageBox.Show("Draw!!", "", MessageBoxButtons.OK);
label1.Text = "a";
}
if (pictureBox1.Image == WindowsFormsApp1.Properties.Resources.rock && pictureBox2.Image == WindowsFormsApp1.Properties.Resources.scissors)
{
MessageBox.Show("You win!!", "", MessageBoxButtons.OK);
label1.Text = "a";
}
if (pictureBox1.Image == WindowsFormsApp1.Properties.Resources.rock && pictureBox2.Image == WindowsFormsApp1.Properties.Resources.paper)
{
MessageBox.Show("Cpu Wins!! ", "", MessageBoxButtons.OK);
label1.Text = "a";
}
if (pictureBox1.Image == WindowsFormsApp1.Properties.Resources.paper && pictureBox2.Image == WindowsFormsApp1.Properties.Resources.scissors)
{
MessageBox.Show("Cpu Wins!!", "", MessageBoxButtons.OK);
label1.Text = "a";
}
if (pictureBox1.Image == WindowsFormsApp1.Properties.Resources.paper && pictureBox2.Image == WindowsFormsApp1.Properties.Resources.rock)
{
MessageBox.Show("You win!!", "", MessageBoxButtons.OK);
label1.Text = "a";
}
if (pictureBox1.Image == WindowsFormsApp1.Properties.Resources.scissors && pictureBox2.Image == WindowsFormsApp1.Properties.Resources.paper)
{
MessageBox.Show("You win!!", "", MessageBoxButtons.OK);
label1.Text = "a";
}
if (pictureBox1.Image == WindowsFormsApp1.Properties.Resources.scissors && pictureBox2.Image == WindowsFormsApp1.Properties.Resources.rock)
{
MessageBox.Show("Cpu Wins!!", "", MessageBoxButtons.OK);
label1.Text = "a";
}
if (pictureBox1.Image == WindowsFormsApp1.Properties.Resources.scissors && pictureBox2.Image == WindowsFormsApp1.Properties.Resources.rock)
{
MessageBox.Show("Cpu Wins!!", "", MessageBoxButtons.OK);
label1.Text = "a";
}
if (pictureBox1.Image == WindowsFormsApp1.Properties.Resources.rock && pictureBox2.Image == WindowsFormsApp1.Properties.Resources.rock)
{
MessageBox.Show("Cpu Wins!!", "", MessageBoxButtons.OK);
label1.Text = "a";
}
timer1.Stop();
}
}
Label1.Text="a";
只是验证代码进入if
块。然而,它在任何情况下都没有进入区块,我不知道为什么。下面是我如何为ex分配picturebox1的方法如果我按下岩石按钮,结果如下:
private void button2_Click(object sender, EventArgs e)
{
timer1.Start();
pictureBox1.Image = WindowsFormsApp1.Properties.Resources.paper;
}
我把它放进form_load: int second =3;我该如何解决这个问题?
我看到了使用GUI处理数据产生的一个大问题。我认为如果你使用一个具体的模型并基于你的模型更新GUI会更好。
在这里你定义每个播放器的选项:
public enum Hand
{
Rock,
Paper,
Scissors
}
和游戏的结果:
public enum GameResult
{
Player1,
Draw,
Player2
}
你可以得到任何播放器的随机值,你的播放器或CPU播放器。或者你可以只使用CPU播放器,并以其他方式管理你的播放器:
private static Hand GetRandomHand()
{
var r = new Random();
return (Hand)r.Next(2);
}
添加一个方法来检查获胜者:
private static GameResult CheckWinner(Hand hand1, Hand hand2)
{
if (hand1 == hand2)
{
return GameResult.Draw;
}
switch (hand1)
{
case Hand.Rock:
return hand2 == Hand.Paper ? GameResult.Player2 : GameResult.Player1;
case Hand.Paper:
return hand2 == Hand.Scissors ? GameResult.Player2 : GameResult.Player1;
default: //case Hand.Scissors:
return hand2 == Hand.Rock ? GameResult.Player2 : GameResult.Player1;
}
}
现在,你可以管理一些关于GUI的事情。例如,获取任意手部选项的图像:
private Image GetImage(Hand hand)
{
switch (hand)
{
case Hand.Rock:
return WindowsFormsApp1.Properties.Resources.rock;
case Hand.Paper:
return WindowsFormsApp1.Properties.Resources.paper;
default: //case Hand.Scissors:
return WindowsFormsApp1.Properties.Resources.scissors;
}
}
在表单中添加两个字段(例如,为您添加player1,为CPU添加player2):
private Hand player1Hand = GetRandomHand();
private Hand player2Hand = GetRandomHand();
使用前面的代码,这非常简单,现在很容易管理关于游戏的一切:
// Get a random value for each player. You can choose in other form a value for your player
// and use the random only for CPU player
this.player1Hand = GetRandomHand();
this.player2Hand = GetRandomHand();
// Set the appropiate image
pictureBox1.Image = GetImage(this.player1Hand);
pictureBox2.Image = GetImage(this.player2Hand);
// Check the result
var result = CheckWinner(this.player1Hand, this.player2Hand);
switch (result)
{
case GameResult.Player1:
MessageBox.Show("You win!!", "", MessageBoxButtons.OK);
break;
case GameResult.Draw:
MessageBox.Show("Draw!!", "", MessageBoxButtons.OK);
break;
case GameResult.Player2:
MessageBox.Show("Cpu Wins!!", "", MessageBoxButtons.OK);
break;
}
设置player1Hand
的值,可参考Hand
enum的值。
player1Hand = Hand.rock;
设置这个值后(从下拉菜单,单选按钮…)你可以更新player1图像:
pictureBox1.Image = GetImage(this.player1Hand);