“Enter"关键不工作在c# Winform数字猜测(Hangman)游戏

本文关键字:数字 Winform 游戏 Hangman quot Enter 工作 | 更新日期: 2023-09-27 18:15:10

我已经为一个数字猜谜游戏(Hangman)编写了下面的c#代码。一切似乎工作完美,但我试图实现某种while(或do-while)或for循环到应用程序中,但当这样做时,它会导致不规则的行为,有时会导致应用程序冻结。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Hangman
{
    public partial class frmHangman : Form
    {
        private Bitmap[] hangmanImages = {Properties.Resources.hangman1, Properties.Resources.hangman2,
                                        Properties.Resources.hangman3, Properties.Resources.hangman4,
                                        Properties.Resources.hangman5, Properties.Resources.dead, Properties.Resources.yay};
    //hangman images to be displayed into the game
    int guessedNumber = 0;
    int numGuesses = 0;
    const int maxCount = 6;
    static Random rand = new Random();
    int computerNumber = rand.Next(1, 101);
    static string msg;
    public frmHangman()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        init();
    }
    private void btnGuess_Click(object sender, EventArgs e)
    {
        guessedNumber = int.Parse(txtUserNumber.Text);
            if (guessedNumber > computerNumber)
            {
                msg = "Should be lower than " + guessedNumber;
                txtUserNumber.Focus();
                hangmanPicture.Image = hangmanImages[numGuesses];
                numGuesses++;
            }
            else if (guessedNumber < computerNumber)
            {
                msg = "Should be greater than " + guessedNumber;
                txtUserNumber.Focus();
                hangmanPicture.Image = hangmanImages[numGuesses];
                numGuesses++;
            }
            if (guessedNumber == computerNumber)
            {
                msg = "Great, you got it";
                hangmanPicture.Image = hangmanImages[6];
                btnGuess.Enabled = false;
                newGame.Visible = true;
                noBtn.Visible = true;
                yesBtn.Visible = true;
                yesBtn.Checked = false;
                numGuesses = 0;
            }
            else if (numGuesses == maxCount)
            {
                msg = "You lose. It was " + computerNumber;
                hangmanPicture.Image = hangmanImages[5];
                btnGuess.Enabled = false;
                newGame.Visible = true;
                noBtn.Visible = true;
                yesBtn.Visible = true;
                yesBtn.Checked = false;
                numGuesses = 0;
            }
            labelResult.Text = msg;
        }
        private void init()
        {
            var rand = new Random();
            computerNumber = rand.Next(1, 101);
            numGuesses = 0;
            hangmanPicture.Image = null;
            labelResult.Text = "Guess a number between 1-100";
            btnGuess.Enabled = true;
            txtUserNumber.Clear();
            txtUserNumber.Focus();
            newGame.Visible = false;
            noBtn.Visible = false;
            yesBtn.Visible = false;
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            init();//"yes" button at the end of the game to prompt user to play again
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            Close();//"no" button at the end of game to prompt user to end the game
        }
        private void btnGuess_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnGuess.PerformClick();
            }
        }
    }
}

“Enter"关键不工作在c# Winform数字猜测(Hangman)游戏

您必须将表单的PreviewKey设置为true。
你可以去找设计师,选择表单并将PreviewKey的值设置为true,所有这些都在属性窗口中。