public override void UpdatePosition(Ball ball) { if (ball.GetDirection() > 1.5 * Math.PI || ball.GetDirection() < 0.5 * Math.PI) { if (ball.GetPosition().Y - 5 > GetPosition().Y + GetSize().Height / 2) { MoveDown(); } else if (ball.GetPosition().Y == GetPosition().Y + GetSize().Height / 2) { } else if (ball.GetPosition().Y + 5 < GetPosition().Y + GetSize().Height / 2) { MoveUp(); } } base.UpdatePosition(ball); }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { // change background color GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(); switch (currentState) { case GameStates.Menu: //MAIN MENU DrawCenteredString(cms, "PONG", new Vector2(450, 200), Color.White); //Main menu text DrawCenteredString(arial, "Press [Space] to start", new Vector2(450, 340), Color.White); DrawCenteredString(arial, "or press [S] to go to settings", new Vector2(450, 370), Color.White); DrawCenteredString(arial, "Controls are {W,S} and {ArrowUp, ArrowDown} for the players", new Vector2(450, 400), Color.Gray); break; case GameStates.GameOver: //GAME OVER DrawCenteredString(cms, "PONG", new Vector2(450, 200), Color.White); //resulting text DrawCenteredString(arial, "Game over, " + (player_turn ? "Red" : "Blue") + " wins!", new Vector2(450, 340), Color.White); //Help text DrawCenteredString(arial, "Press [Enter] to return to the menu", new Vector2(450, 400), Color.Gray); DrawCenteredString(arial, "or press [Space] to play again", new Vector2(450, 430), Color.Gray); break; case GameStates.Settings: //SETTINGS DrawCenteredString(arial, "Settings", new Vector2(450, 60), Color.Gray); //SELECTOR DrawCenteredString(arial, "______________", new Vector2(450, 100 + 40 * (int)selectedSetting), Color.Gray); //The settings DrawCenteredString(arial, "Paddle speed <" + st.paddle_speed + ">", new Vector2(450, 100), Color.White); DrawCenteredString(arial, "Speed multiplier on bounce <" + st.bounce_increase + ">", new Vector2(450, 140), Color.White); DrawCenteredString(arial, "Starting speed of ball <" + st.ball_defaultspeed + ">", new Vector2(450, 180), Color.White); DrawCenteredString(arial, "Amount of lives <" + st.lives + ">", new Vector2(450, 220), Color.White); //Help text DrawCenteredString(arial, "Use [Arrow keys] to change the values", new Vector2(450, 460), Color.Gray); DrawCenteredString(arial, "Press [Enter] to return to the menu", new Vector2(450, 500), Color.Gray); break; case GameStates.Game: //GAME IS RUNNING //draw middle_line spriteBatch.Draw(single_pixel, new Rectangle(450, 0, 1, 600), Color.DarkGray); //draw ball and players spriteBatch.Draw(ball.GetImage(), new Rectangle((int)ball.GetPosition().X, (int)ball.GetPosition().Y, ball.Width, ball.Height), ball.GetColor()); spriteBatch.Draw(redPlayer.GetImage(), new Rectangle((int)redPlayer.GetPosition().X, (int)redPlayer.GetPosition().Y, redPlayer.Width, redPlayer.Height), redPlayer.GetColor()); spriteBatch.Draw(bluePlayer.GetImage(), new Rectangle((int)bluePlayer.GetPosition().X, (int)bluePlayer.GetPosition().Y, bluePlayer.Width, bluePlayer.Height), bluePlayer.GetColor()); //drawing red player's lives for (int i = 0; i < redPlayer.GetLives(); i++) { //make sure we dont overflow on the paddle, and instead just draw the remaining lives on the top of the screen if (i < 3) { spriteBatch.Draw(single_pixel, new Rectangle(4, (int)redPlayer.GetPosition().Y + 31 + i * 12, 8, 8), redPlayer.GetColor()); } else { spriteBatch.Draw(single_pixel, new Rectangle(20 + i * 12, 4, 8, 8), redPlayer.GetColor()); } } //drawing blue player's lives for (int i = 0; i < bluePlayer.GetLives(); i++) { //make sure we dont overflow on the paddle, and instead just draw the remaining lives on the top of the screen if (i < 3) { spriteBatch.Draw(single_pixel, new Rectangle((int)900 - bluePlayer.Width + 4, (int)bluePlayer.GetPosition().Y + 31 + i * 12, 8, 8), bluePlayer.GetColor()); } else { spriteBatch.Draw(single_pixel, new Rectangle(868 - i * 12, 4, 8, 8), bluePlayer.GetColor()); } } //create an indication where the ball will move if the game hasn't started yet if (!has_moved) { for (int i = 1; i <= 5; i++) { Vector2 nextpos = Vector2.Add(ball.GetPosition(), Vector2.Multiply(ball.GetSpeed(), (float)st.ball_defaultspeed * st.bounce_speed * i * 5)); spriteBatch.Draw(ball.GetImage(), new Rectangle((int)nextpos.X + ball.Width / 2, (int)nextpos.Y + ball.Height / 2, ball.Width / 3, ball.Height / 3), ball.GetColor()); } } break; } spriteBatch.End(); base.Draw(gameTime); }