Pong 游戏在 C# 中使用枚举作为方向玩家

Pong game use enum for direction player in C#

我是枚举的新手,我的老师说我应该在我的代码中使用它,所以我尝试了但它不起作用,我正在尝试制作乒乓球。这是我的代码。我尝试使用枚举形式的其他代码示例,但它不起作用。

using System;
using System.Windows.Forms;

namespace pong_1._13                

{
    public partial class Form1 : Form
    {
        int MaxScore = 10;          // the max score you can get
        int ActivateBotCheat = 9;   // makes the bot always win when the playerscore = the value of AcitvateBotCheat, To turn cheat off set value to 10
        int speed = 6;              // bot speed
        int playerSpeed = 8;        // the speed of the player
        int topScreen = 0;          // the top of the screen 
        int bottemScreen = 350;     // the boytom of th screen
        int middelScreen = 300;     // respawnpoint of the ball
        int leftSideScreen = 0;     // the left side of the screen
                                    // right side of screen = ClientSize.Width
        int YIs0 = 10;              // the value Y gets when it ends up at 0 so the bal keeps going up and down
        int YcheatOffset = 30;      // offset bot when cheat is enabled
        enum Deraction {goUp, goDown}
        Deraction UpDown;
        int ballX = 5;              // the x angle of the ball left/right
        int ballY = 5;              // the y angle of the ball up/down
        int ballXspeedAdd = 1;      // after round add ball speed X
        int ballYspeedAdd = 5;      // after round add ball speed Y
        int score = 0;              // the score of the player
        int scoreAI = 0;            // the score of the bot
        

        public Form1()
        {
            InitializeComponent();
        }
        private void keyIsUp(object sender, KeyEventArgs e) // looks if the player wants to go up or down if not then makes the player stand still
        {
            if (e.KeyCode == Keys.Up)
            {
                
            }
            else if (e.KeyCode == Keys.Down)
            {
                Deraction Down = Deraction.goDown;
            }
        }
        private void keyIsDown(object sender, KeyEventArgs e) // looks if the player wants to go up or down if so makes the player go up and down
        {
            if (e.KeyCode == Keys.Down)
            {
                Deraction Down = Deraction.goDown;
            }
            else if (e.KeyCode == Keys.Up)
            {
                Deraction Up = Deraction.goUp;
            }
        }
        private void timerTick(object sender, EventArgs e)
        {
            playerScore.Text = "" + score;          // dispays player score
            aiScore.Text = "" + scoreAI;            // displays bot score
            ball.Top += ballY;                      // makes the ball bouwns
            ball.Left -= ballX;                     // makes the ball bouwns
            bot.Top += speed;                       // makes bot faster
            
            if (score < ActivateBotCheat) 
            {
                if (bot.Top < topScreen || bot.Top > bottemScreen)  // normal game play for the bot
                {
                    speed = -speed;                 // makes bot slower
                }
            }
            else                                    // cheat code for bot
            {
                bot.Top = ball.Top - YcheatOffset;  // set the x cordinats of the bot = to x cordinats of the ball mines the offset 
            }

            if (ball.Left < leftSideScreen)         // when bot scores
            {
                ball.Left = middelScreen;           // respawnpoit ball
                ballX = -ballX;                     // makes the ball go the other way
                ballX -= ballXspeedAdd;             // makes the bal go faster
                ballY = -ballY;                     // makes the ball go the other way
                ballY += ballYspeedAdd;             // makes the bal go faster
                scoreAI++; 
            }

            if (ball.Left + ball.Width > ClientSize.Width) // when player scores
            {
                ball.Left = middelScreen;           // respawnpoit ball
                ballX = -ballX;                     // makes the ball go the other way
                ballX += ballXspeedAdd;             // makes the bal go faster
                ballY = -ballY;                     // makes the ball go the other way
                ballY += ballYspeedAdd;             // makes the bal go faster
                score++; 
            }
            if (ball.Top < leftSideScreen || ball.Top + ball.Height > ClientSize.Height)                // when ball hits border of the screen
            {
                ballY = -ballY;                     // makes the ball go the other way
            }

            if (ball.Bounds.IntersectsWith(player.Bounds) || ball.Bounds.IntersectsWith(bot.Bounds))    // when ball hits border of the screen
            {
                ballX = -ballX;                     // makes the ball go the other way
            }

            if(ballY == 0)                          // makes sure ball Y is not 0
            {
                ballY += YIs0; 
            }

            if (UpDown == Deraction.goUp && player.Top > topScreen)     // makes player go up
            {
                player.Top -= playerSpeed;
            }

            if (UpDown == Deraction.goDown && player.Top < bottemScreen)    // makes player go down
            { 
                player.Top += playerSpeed;
            }

            if (score == MaxScore)                      // show message when player wins
            {
                score++;
                gameTimer.Stop();
                MessageBox.Show("You win this game");
            }

             if (scoreAI == MaxScore)                   // show message when bot wins
            {
                scoreAI++;
                gameTimer.Stop();
                MessageBox.Show("The bot wins, you lose");
            }
        }
    }
}

enum Deraction {goUp, goDown};

Deraction UpDown;

这是正确的。你的问题是你以后没有使用 UpDown 。相反,您写的 Deraction Down = Deraction.goDown; 不会影响 UpDown.

更改您的代码以分配给 UpDown

示例:

private void keyIsUp(object sender, KeyEventArgs e) // looks if the player wants to go up or down if not then makes the player stand still
{
    if (e.KeyCode == Keys.Up)
    {
        
    }
    else if (e.KeyCode == Keys.Down)
    {
        UpDown = Deraction.goDown;
    }
}
private void keyIsDown(object sender, KeyEventArgs e) // looks if the player wants to go up or down if so makes the player go up and down
{
    if (e.KeyCode == Keys.Down)
    {
        UpDown = Deraction.goDown;
    }
    else if (e.KeyCode == Keys.Up)
    {
        UpDown = Deraction.goUp;
    }
}