纸,剪刀,石头如何显示正确的结果?

时间:2019-06-10 11:08:46

标签: c# windows-forms-designer

我正在尝试使用Windows窗体来创建纸张,剪刀,石头游戏。 单击一个button1开始显示pictureBox1。 然后单击button2以在pictureBox2中显示剪刀,并停止pictureBox1。

如果picture1是Scissor,则标签将显示“ Tie”。 如果picture1是Stone,请显示“您输了”。 如果picture1是Paper,则显示“ You win”。

但是结果是错误的。

剪刀秀“你输了”。 石头秀“领带”。 纸质节目“你赢了”。

    private int state = 1;
    public Exam5()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (state == 1)
        {
            Bitmap bitmap = new Bitmap("Scissor.jpg");
            pictureBox1.Image = bitmap;
        }
        else if (state == 2)
        {
            Bitmap bitmap = new Bitmap("Stone.jpg");
            pictureBox1.Image = bitmap;
        }
        else
        {
            Bitmap bitmap = new Bitmap("Papper.jpg");
            pictureBox1.Image = bitmap;
        }
        state++;
        if (state > 3) state = 1;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        timer1.Enabled = false;
        Bitmap bitmap = new Bitmap("Scissor.jpg");
        pictureBox2.Image = bitmap;

        if (state == 1) label1.Text = "Tie";
        else if (state == 2) label1.Text = "You lose";
        else label1.Text = "You win";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Bitmap bitmap = new Bitmap("Stone.jpg");
        pictureBox2.Image = bitmap;
        timer1.Enabled = false;

        if (state == 1) label1.Text = "You win";
        else if (state == 2) label1.Text = "Tie";
        else label1.Text = "You lose";
    }

    private void button4_Click(object sender, EventArgs e)
    {
        Bitmap bitmap = new Bitmap("Papper.jpg");
        pictureBox2.Image = bitmap;
        timer1.Enabled = false;

        if (state == 1) label1.Text = "You lose";
        else if (state == 2) label1.Text = "You win";
        else label1.Text = "Tie";
    }

1 个答案:

答案 0 :(得分:0)

像这样的事情,请注意这是未经测试的代码,只是很快就将其淘汰了,但是应该给您足够的主意吗?

private int state = 1;
public Exam5()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
    string selectedImage;

    state++;

    if (state > 3) state = 1;

    switch (state)
    {
        case 1: 
            selectedImage = "Scissor.jpg";
            break;

        case 1: 
            selectedImage = "Stone.jpg";
            break;

        case 1: 
            selectedImage = "Paper.jpg";
            break;
    }

    pictureBox1.Image = new Bitmap(selectedImage);
}

private void button2_Click(object sender, EventArgs e)
{
    timer1.Enabled = false;
    pictureBox2.Image = new Bitmap("Scissor.jpg");

    if (state == 1) 
        label1.Text = "Tie";
    else if (state == 2) 
        label1.Text = "You lose";
    else 
        label1.Text = "You win";
}