Exemple #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here
            _playerPaddle.Update(gameTime, GameObjects);
            _computerPaddle.Update(gameTime, GameObjects);
            _ball.Update(gameTime, GameObjects);
            Score.Update(gameTime, GameObjects);

            base.Update(gameTime);
        }
Exemple #2
0
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                ResetBall();
            }


            ball.Update(gameTime);

            //if(ball.Position.X > Window.ClientBounds.Width || ball.Position.X < 0)
            //{
            //    Vector2 new_direction = Vector2.Reflect(ball.Direction, Vector2.Normalize(ball.Direction));

            //    //new_direction.X -= Vector2.Normalize(
            //    //    new Vector2(Window.ClientBounds.Y + Window.ClientBounds.Height / 2 - intersection.Center.Y, 0)).X;



            //    ball.SetDirection(new_direction);
            //}

            //Scoring
            if (ball.Position.Y > Window.ClientBounds.Height)
            {
                //ball.LastTouch.Score += 1;

                players[1].Score++;
                this.ResetBall();
            }
            else if (ball.Position.Y < 0)
            {
                players[0].Score++;
                this.ResetBall();
            }

            //ball direction reflection
            for (int i = 0; i < players.Length; ++i)
            {
                players[i].Update(gameTime);

                intersection = Rectangle.Intersect(players[i].Rectangle, ball.Rectangle);

                if (intersection != Rectangle.Empty)
                {
                    intersections.Add(intersection);
                    ball.LastTouch = players[i];
                    if (ball.Rectangle.Bottom > players[i].Rectangle.Top)
                    {
                        Vector2 new_direction = Vector2.Reflect(ball.Direction, Vector2.Normalize(ball.Direction));


                        new_direction.X -= Vector2.Normalize(
                            new Vector2(players[i].Rectangle.Center.X - intersection.Center.X, 0)).X;

                        ball.SetSpeed(ball.Speed + (Vector2.One / 5));

                        ball.SetDirection(new_direction);

                        ballBounce.Play();
                    }
                }
            }

            //collision for walls
            //ball.Rectangle.Contains(wall_left.Rectangle);
            if (Rectangle.Intersect(ball.Rectangle, wall_left.Rectangle) != Rectangle.Empty)
            {
                ball.SetDirection(new Vector2(1, ball.Direction.Y));
                //Power up
                //ball.SetDirection(new Vector2(ball.Direction.Y, 1));
            }
            else if (Rectangle.Intersect(ball.Rectangle, wall_right.Rectangle) != Rectangle.Empty)
            {
                ball.SetDirection(new Vector2(-1, ball.Direction.Y));
            }

            wall_left.Update(gameTime);
            wall_right.Update(gameTime);

            //Spawn powerup
            System.Random rng = new System.Random();
            if (rng.Next(0, 60000) < 10000)
            {
                float rngPUx = rng.Next(16, Window.ClientBounds.Width - 16);
                float rngPUy = Window.ClientBounds.Height / 2;
                if (powerups.Count < 5)
                {
                    powerup = null;
                }
                switch (rng.Next(0, 10))
                {
                case 0:
                    powerup = new ScoreSnack(new Vector2(rngPUx, rngPUy), this.Content);
                    powerups.Add(powerup);
                    break;

                case 1:
                    powerup = new ResetToZero(new Vector2(rngPUx, rngPUy), this.Content);
                    powerups.Add(powerup);
                    break;
                }
            }

            //remove the powerup that's hit
            foreach (Powerup p in powerups)
            {
                if (p.Rectangle.Intersects(ball.Rectangle))
                {
                    if (ball.LastTouch != null)
                    {
                        p.ApplyEffect(ball.LastTouch);
                        this.powerups.Remove(p);
                        break;
                    }
                }
            }

            base.Update(gameTime);
        }
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here
            MouseState mouse = Mouse.GetState();

            switch (CurrentGameState)
            {
            case GameState.MainMenu:
                if (btnPlay.isClicked == true)
                {
                    CurrentGameState = GameState.Playing;
                }
                if (btnExit.isClicked == true)
                {
                    Exit();
                }
                btnPlay.Update(mouse);
                btnExit.Update(mouse);
                break;

            case GameState.Playing:
                //player 1 movement
                if (Keyboard.GetState().IsKeyDown(Keys.W))
                {
                    player1.KeyUp();
                }
                if (Keyboard.GetState().IsKeyDown(Keys.S))
                {
                    player1.KeyDown();
                }

                //player 2 movement
                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                {
                    player2.KeyUp();
                }
                if (Keyboard.GetState().IsKeyDown(Keys.Down))
                {
                    player2.KeyDown();
                }
                if (Keyboard.GetState().IsKeyDown(Keys.Space) && !ball.gameRun)
                {
                    ball.gameRun = true;
                }
                if (ball.gameRun)
                {
                    ball.Update(gameTime);



                    //player collision logic
                    if (ball.dirx > 0)
                    {
                        if (ball.posy >= player2.posY && ball.posy + ballSize < player2.posY + batheigth && ball.posx + ballSize >= player2.posX)
                        {
                            ball.dirx = -ball.dirx;
                        }
                    }

                    else if (ball.dirx < 0)
                    {
                        if (ball.posy >= player1.posY && ball.posy + ballSize <= player1.posY + batheigth && ball.posx <= player1.posX + batwidth)
                        {
                            ball.dirx = -ball.dirx;
                        }
                    }
                    if (score1 == 10 || score2 == 10)
                    {
                        ball.gameRun     = false;
                        CurrentGameState = GameState.GameOver;
                        if (score1 < score2)
                        {
                            winner = "Player 2";
                        }
                        else
                        {
                            winner = "Player 1";
                        }
                    }
                }
                break;
            }

            base.Update(gameTime);
        }