首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WindowsForms app - keyEvent不能工作,问题出在哪里?

WindowsForms app - keyEvent不能工作,问题出在哪里?
EN

Stack Overflow用户
提问于 2020-01-27 08:42:15
回答 1查看 41关注 0票数 0

我有一个小问题与我的KeyEventArgs窗体应用程序-它是一个蛇游戏每时每刻都可以,但点击“再次播放”按钮后,应用程序没有看到任何kay事件(我尝试调试和放置断点在KeyIsDown(对象发送器,windowsforms e);但它不调用当我点击按键你能告诉我哪里有问题吗?

完整存储库(几乎没有其他版本,但仍然不起作用):https://github.com/ldidil/snake-game代码的简短版本:

代码语言:javascript
复制
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();
        }

    }


}
EN

回答 1

Stack Overflow用户

发布于 2020-01-27 08:48:37

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

代码语言:javascript
复制
playAgainButton.KeyDown += KeyIsDown;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59924098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档