繁体   English   中英

制作剪刀石头布游戏c#

[英]Making a rock paper scissor game c#

我正在制作一个与 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 分配图片框1,这就是发生的事情:

  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 代表你,player2 代表 CPU):

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枚举值:

player1Hand = Hand.rock;

设置此值后(从下拉菜单、单选按钮...),您可以更新 player1 图像:

pictureBox1.Image = GetImage(this.player1Hand);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM