Home > other >  Pong game use enum for direction player in C#
Pong game use enum for direction player in C#

Time:10-08

I am new to enum and my teacher said I should use it in my code, so I tried but it doesn't work, I am trying to make pong. This is my code. I tried using examples form other codes with enum but it doesnt work.

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

CodePudding user response:

enum Deraction {goUp, goDown};

Deraction UpDown;

This is correct. Your problem is that you are not using UpDown later. Instead, you are writing Deraction Down = Deraction.goDown; which does not affect UpDown.

Change your code to assign to UpDown.

Example:

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;
    }
}
  • Related