WindowsForms应用-keyEvent不起作用,问题出在哪里?

问题描述 投票:0回答:1

我的Windowsforms应用程序没什么问题-它是一款蛇游戏一切正常,但单击“再次播放”按钮后,应用程序看不到任何kay事件(我尝试调试并将断点放在KeyIsDown(object sender,KeyEventArgs e)中;但是单击Key时不会调用您能告诉我哪里有问题吗?

完整的存储库(其他版本很少,但仍然无法使用):https://github.com/ldidil/snake-game代码的简短版本:

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();
        }

    }


}


c# winforms events keyevent
1个回答
0
投票

您需要将KeyIsDown函数注册为控件的事件处理程序-Control.KeyDown Event

© www.soinside.com 2019 - 2024. All rights reserved.