C# WindowsForms应用程序-keyEvent不';不行,问题出在哪里?

C# WindowsForms应用程序-keyEvent不';不行,问题出在哪里?,c#,winforms,events,keyevent,C#,Winforms,Events,Keyevent,我的windowsforms应用程序有点问题-这是一个蛇游戏 一切正常,但在点击“再次播放”按钮后,应用程序并没有看到任何kay事件(我尝试调试并在KeyIsDown(object sender,KeyEventArgs e)中放置断点);但当我点击Key时,它并没有被调用 你能告诉我哪里有问题吗 完整存储库(其他版本很少,但仍然不起作用): 代码的简短版本: namespace SnakeGame { public partial class Form1 : Form {

我的windowsforms应用程序有点问题-这是一个蛇游戏 一切正常,但在点击“再次播放”按钮后,应用程序并没有看到任何kay事件(我尝试调试并在KeyIsDown(object sender,KeyEventArgs e)中放置断点);但当我点击Key时,它并没有被调用 你能告诉我哪里有问题吗

完整存储库(其他版本很少,但仍然不起作用): 代码的简短版本:

namespace SnakeGame
{
    public partial class Form1 : Form
    {

        private List<Circle> Snake = new List<Circle>();
        private Circle food;

        public Form1()
        {
            InitializeComponent();
            new Settings();
            DisplayTopScore();
            gameTimer.Interval = 1000 / Settings.Speed; //TO DO (change speed lvl)
            gameTimer.Tick += UpdateScreen;
            gameTimer.Start();

            StartGame();

        }

        private void StartGame()
        {

           GenerateSnake();
           GenerateFood();

        }

        private void UpdateScreen(object sender, EventArgs e)
        {
            if (Settings.GameOver == false)
            {
                if (Input.KeyPress(Keys.Right) && Settings.Direction != Directions.Left)
                {
                    Settings.Direction = Directions.Right;
                }
                else if (Input.KeyPress(Keys.Left) && Settings.Direction != Directions.Right)
                {
                    Settings.Direction = Directions.Left;
                }
                else if (Input.KeyPress(Keys.Up) && Settings.Direction != Directions.Down)
                {
                    Settings.Direction = Directions.Up;
                }
                else if (Input.KeyPress(Keys.Down) && Settings.Direction != Directions.Up)
                {
                    Settings.Direction = Directions.Down;
                }

                MovePlayer();
            }

            boardCanvas.Invalidate();  //odśwież plansze   
        }

        private void MovePlayer()
        {
            for (int i = Snake.Count - 1; i >= 0; i--)
            {
                if (i == 0)
                {
                    switch (Settings.Direction)
                    {
                        case Directions.Right:
                            Snake[i].X++;
                            break;
                        case Directions.Left:
                            Snake[i].X--;
                            break;
                        case Directions.Up:
                            Snake[i].Y--;
                            break;
                        case Directions.Down:
                            Snake[i].Y++;
                            break;

                    }

                    checkCollision(i);
                    checkFood();                 
                }
                else
                {
                    Snake[i].X = Snake[i - 1].X;
                    Snake[i].Y = Snake[i - 1].Y;
                }
            }
        }

        private void KeyIsDown(object sender, KeyEventArgs e)
        {
            Input.ChangeState(e.KeyCode, true);
        }

        private void KeyIsUp(object sender, KeyEventArgs e)
        {
            Input.ChangeState(e.KeyCode, false);
        }

        private void Die()
        {
            topScore.Write();
            Settings.GameOver = true;
        }

        private void UpdateGraphic(object sender, PaintEventArgs e)
        {
            {
                if (!Settings.GameOver)
                {
                    draw(e);                    
                }
                else
                { //game over
                    string gameOver = "Game Over\n" + "Final Score: " + Settings.Points;
                    endText.Text = gameOver;
                    endText.Visible = true;
                    playAgainButton.Visible = true;
                    exitButton.Visible = true;
                }
            }
        }

        private void draw(PaintEventArgs e)
        {

            for (int i = 0; i < Snake.Count; i++)
            {
                string location;
                if (i == 0)
                {
                    location = (@"..\..\Img\snakeHead.png");
                }
                else
                {
                    location = (@"..\..\Img\snakeBody.png");

                }

                var img = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + location);
                Rotate(img);

                Point p1 = new Point(Snake[i].X *Settings.Width, Snake[i].Y * Settings.Height);

                e.Graphics.DrawImage(img, p1);
            }
            Point p2 = new Point(food.X * Settings.Width, food.Y * Settings.Height);
            var appleImage = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + @"..\..\Img\apple.png");
            e.Graphics.DrawImage(appleImage, p2);

        }

        private static void Rotate(Bitmap img)
        {
            switch (Settings.Direction)
            {
                case Directions.Right:
                    break;
                case Directions.Left:
                    img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    break;
                case Directions.Up:
                    img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    break;
                case Directions.Down:
                    img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    break;

            }
        }

        private void playAgainButton_Click(object sender, EventArgs e)
        {
            endText.Visible = false;
            playAgainButton.Visible = false;
            exitButton.Visible = false;
            new Settings(Form2.nickname, Form2.speed);
            Snake.Clear();

            StartGame();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }

    }


}


名称空间蛇形游戏
{
公共部分类Form1:Form
{
私有列表Snake=新列表();
私人圈食品;
公共表格1()
{
初始化组件();
新设置();
DisplayTopScore();
gameTimer.Interval=1000/Settings.Speed;//待办事项(更改速度lvl)
gameTimer.Tick+=UpdateScreen;
gameTimer.Start();
StartGame();
}
私有void StartGame()
{
GenerateSnake();
GenerateFood();
}
私有void UpdateScreen(对象发送方,事件参数e)
{
如果(Settings.GameOver==false)
{
if(Input.KeyPress(key.Right)&&Settings.Direction!=Directions.Left)
{
Settings.Direction=Directions.Right;
}
else if(Input.KeyPress(Keys.Left)&&Settings.Direction!=Directions.Right)
{
Settings.Direction=Directions.Left;
}
else if(Input.KeyPress(Keys.Up)&&Settings.Direction!=Directions.Down)
{
Settings.Direction=Directions.Up;
}
else if(Input.KeyPress(Keys.Down)&&Settings.Direction!=Directions.Up)
{
设置。方向=方向。向下;
}
MovePlayer();
}
boardCanvas.Invalidate();//odświeżplansze
}
私人玩家()
{
对于(inti=Snake.Count-1;i>=0;i--)
{
如果(i==0)
{
开关(设置。方向)
{
案例说明。右:
Snake[i].X++;
打破
案例说明。左:
Snake[i].X--;
打破
案例说明。向上:
蛇[i].Y--;
打破
案例说明。向下:
Snake[i].Y++;
打破
}
检查碰撞(i);
检查食物();
}
其他的
{
Snake[i].X=Snake[i-1].X;
Snake[i].Y=Snake[i-1].Y;
}
}
}
私有void KeyIsDown(对象发送方,KeyEventArgs e)
{
输入.ChangeState(例如KeyCode,true);
}
私有void KeyIsUp(对象发送方,KeyEventArgs e)
{
输入.ChangeState(例如KeyCode,false);
}
私人空模()
{
topScore.Write();
Settings.GameOver=true;
}
私有void UpdateGraphic(对象发送方,PaintEventArgs e)
{
{
如果(!Settings.GameOver)
{
抽签(e);
}
其他的
{//游戏结束
string gameOver=“游戏结束\n”+“最终分数:”+设置。分数;
Text=gameOver;
endText.Visible=true;
playAgainButton.Visible=true;
exitButton.Visible=true;
}
}
}
私有虚空绘制(PaintEventArgs e)
{
for(int i=0;iplayAgainButton.KeyDown += KeyIsDown;