c#windows窗体猜图片游戏

夏纳

基本上,我在visual studio中用windows form c#创建了一个项目。我试图猜测图像游戏。我做了两个按钮,一个随机化显示的图像,另一个将显示他们是否猜对了。我在第二个按钮上挣扎,因为我不知道如何从第一个按钮到第二个按钮获取图像数量。

private void Button1_Click(object sender, EventArgs e)
{
    Random r = new Random();
    rInt = r.Next(1, 21);
    string path = @"C:/Users/User/Desktop/Badges/Badges/";
    pictureBox1.Image = Image.FromFile(path + rInt.ToString() + ".png");
}

private void Button2_Click(object sender, EventArgs e)
{
    string path = @"C:/Users/User/Desktop/Badges/Badges/";
    if (textBox1.Text = rInt)
    {
        label3.Text = "You Guessed Correctly, Well Done!!";
    }
    else
    {
        label3.Text = "Incorrect Guess, Try Again!";
    }
}
圭多

不清楚这个游戏应该如何运作。
用户是否必须输入一个数字,textBox1然后您的点击检查这个数字?

如果是这样,你可以这样做。
它所做的只是将随机数存储在 pictureBox1 的 tag 属性中,以便您可以从其他点击事件中访问它。

private void Button1_Click(object sender, EventArgs e)
{
    Random r = new Random();
    int rInt = r.Next(1, 21);
    string path = @"C:/Users/User/Desktop/Badges/Badges/";
    int i = r.Next();
    pictureBox1.Image = Image.FromFile(path + rInt.ToString() + ".png");

    // store the random number in the pictureBox
    pictureBox1.Tag = rInt;
}

private void Button2_Click(object sender, EventArgs e)
{
    if (textBox1.Text == pictureBox1.Tag.ToString())
    {
        label3.Text = "You Guessed Correctly, Well Done!!";
    }
    else
    {
        label3.Text = "Incorrect Guess, Try Again!";
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章