Exemplo n.º 1
0
        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Space) && !ball.gameRun)
            {
                ball.gameRun = true;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.R) && ball.gameRun)
            {
                ball.gameRun = false;
                ball.ResetBall();
            }


            P1.posY = Mouse.GetState().Y;
            P2.posY = ball.posY / 1.4 + 25;


            if (ball.dirX > 0)
            {
                // ball.CheckRacketCollision(P2);
                if (ball.posY >= P2.posY && ball.posY + ballSize < P2.posY + racketHeight &&
                    ball.posX + ballSize >= P2.posX)
                {
                    ball.dirX = -ball.dirX;
                }
                else if (ball.posX >= GraphicsDevice.Viewport.Width - ballSize)
                {
                    scoreP1++;
                    ball.gameRun = false;
                    ball.ResetBall();
                }
            }
            else if (ball.dirX < 0)
            {
                // ball.CheckRacketCollision(P1);
                if (ball.posY >= P1.posY && ball.posY + ballSize <= P1.posY + racketHeight &&
                    ball.posX <= P1.posX + racketWidht)
                {
                    ball.dirX = -ball.dirX;
                }
                else if (ball.posX <= 0)
                {
                    scoreP2++;
                    ball.gameRun = false;
                    ball.ResetBall();
                }
            }

            base.Update(gameTime);
        }
Exemplo n.º 2
0
        protected override void LoadContent()
        {
            font        = Content.Load <SpriteFont>("font");
            spriteBatch = new SpriteBatch(GraphicsDevice);
            ball        = new ClassBall(GraphicsDevice, spriteBatch, this, ballSize);
            P1          = new ClassRacket(GraphicsDevice, spriteBatch, this, racketwidth, racketheight, 10, GraphicsDevice.Viewport.Height / 2 - racketheight / 2);
            P2          = new ClassRacket(GraphicsDevice, spriteBatch, this, racketwidth, racketheight, GraphicsDevice.Viewport.Width - 10 - racketwidth, GraphicsDevice.Viewport.Height / 2 - racketheight / 2);

            Components.Add(ball);
            Components.Add(P1);
            Components.Add(P2);
            ball.ResetBall();
        }
Exemplo n.º 3
0
        protected override void LoadContent()
        {
            //The font for the scores that show.
            font = Content.Load <SpriteFont>("font");

            //The balls size will be loaded from the ClassBall to the program.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            ball        = new ClassBall(GraphicsDevice, spriteBatch, this, ballSize);

            //Both players will be loaded from the ClassRacket to the program
            P1 = new ClassRacket(GraphicsDevice, spriteBatch, this, racketWidth, racketHeight, 10, GraphicsDevice.Viewport.Height / 2 - racketHeight / 2);
            P2 = new ClassRacket(GraphicsDevice, spriteBatch, this, racketWidth, racketHeight, GraphicsDevice.Viewport.Width - 10 - racketWidth, GraphicsDevice.Viewport.Height / 2 - racketHeight / 2);

            Components.Add(P1);
            Components.Add(P2);
            Components.Add(ball);

            ball.ResetBall();
        }
Exemplo n.º 4
0
        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Space) && !ball.gameRun)
            {
                ball.gameRun = true;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.R) && ball.gameRun)
            {
                ball.gameRun = false;
                ball.ResetBall();
            }

            P1.posY = Mouse.GetState().Y;
            rnd     = new Random();

            if (P1.posY > 400)
            {
                P1.posY = 400;
            }
            if (P1.posY < 0)
            {
                P1.posY = 0;
            }

            if (P2.posY > 400)
            {
                P2.posY = 400;
            }
            if (P2.posY < 0)
            {
                P2.posY = 0;
            }

            int odbicie = rnd.Next(1, 100);

            if (ball.posX > 400)
            {
                if (odbicie >= 85)
                {
                    P2.posY = ball.posY;

                    if (P1.posY > 400)
                    {
                        P1.posY = 400;
                    }
                    if (P1.posY < 0)
                    {
                        P1.posY = 0;
                    }

                    if (P2.posY > 400)
                    {
                        P2.posY = 400;
                    }
                    if (P2.posY < 0)
                    {
                        P2.posY = 0;
                    }
                }
                else
                {
                    for (int i = 0; i < 15; i++)
                    {
                        P2.posY = ball.posY + 1;
                    }
                }
            }
            else
            {
                P2.posY = Mouse.GetState().Y;
            }

            //ball goes to P2
            if (ball.dirX > 0)
            {
                if (ball.posY >= P2.posY && ball.posY + ballSize < P2.posY + racketheight && ball.posX + ballSize >= P2.posX)
                {
                    ball.dirX = -ball.dirX;
                }
                else if (ball.posX >= GraphicsDevice.Viewport.Width - ballSize)
                {
                    scoreP1++;
                    ball.gameRun = false;
                    ball.ResetBall();
                }
            }
            //ball goes to P1
            else if (ball.dirX < 0)
            {
                if (ball.posY >= P1.posY && ball.posY + ballSize <= P1.posY + racketheight && ball.posX <= P1.posX + racketwidth)
                {
                    ball.dirX = -ball.dirX;
                }
                else if (ball.posX <= 0)
                {
                    scoreP2++;
                    ball.gameRun = false;
                    ball.ResetBall();
                }
            }


            base.Update(gameTime);
        }
Exemplo n.º 5
0
        //This is for what happens when the game is running.
        protected override void Update(GameTime deltaTime)
        {
            //When Escape key is pressed it closes the game.
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            //When the spacebar is pressed than the game will start and the ball will move.
            else if (Keyboard.GetState().IsKeyDown(Keys.Space) && !ball.gameRun)
            {
                ball.gameRun = true;
            }

            //When someone presses the key R it will reset.
            else if (Keyboard.GetState().IsKeyDown(Keys.R) && ball.gameRun)
            {
                ball.gameRun = false;
                ball.ResetBall();
            }

            //The paddles will move where the mouse is going.
            P1.posY = Mouse.GetState().Y;
            P2.posY = Mouse.GetState().Y;

            // Ball goes to P2
            if (ball.dirX > 0)
            {
                if (ball.posY >= P2.posY && ball.posY + ballSize < P2.posY + racketHeight && ball.posX >= P2.posX)
                {
                    ball.dirX = -ball.dirX;
                }

                //If it goes behind the paddle than a score will go for the player of the opposite pladdle
                else if (ball.posX >= GraphicsDevice.Viewport.Width - ballSize)
                {
                    scoreP1++;
                    ball.gameRun = false;
                    ball.ResetBall();
                }
            }
            // Ball goes to P1

            else if (ball.dirX < 0)
            {
                {
                    if (ball.posY >= P1.posY && ball.posY + ballSize <= P1.posY + racketHeight && ball.posX <= P1.posX + racketWidth)
                    {
                        ball.dirX = -ball.dirX;
                    }

                    //If it goes behind the paddle than a score will go for the player of the opposite pladdle
                    else if (ball.posX <= 0)
                    {
                        scoreP2++;
                        ball.gameRun = false;
                        ball.ResetBall();
                    }
                }
            }


            {
                base.Update(deltaTime);
                switch (_state)
                {
                case GameState.MainMenu:
                    UpdateMainMenu(deltaTime);
                    break;

                case GameState.Gameplay:
                    UpdateGameplay(deltaTime);
                    break;

                case GameState.EndOfGame:
                    UpdateEndOfGame(deltaTime);
                    break;
                }
            }
        }