Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Pong
- {
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using System;
- public class Bat
- {
- public Vector2 Position;
- public float moveSpeed;
- public Rectangle size;
- private int points;
- private int yHeight;
- private Texture2D leftBat;
- public float turbo;
- public float recharge;
- public float interval;
- public bool isTurbo;
- // SlowMo stuff
- private float Elapsed = 0f;
- public float MoveTimer = 0f;
- public bool Slowmo = false;
- /// <summary>
- /// Constructor for the bat
- /// </summary>
- public Bat(ContentManager contentManager, Vector2 screenSize, bool side)
- {
- moveSpeed = 7f;
- turbo = 15f;
- recharge = 100f;
- points = 0;
- interval = 5f;
- leftBat = contentManager.Load<Texture2D>(@"gfx/bats/batGrey");
- size = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
- // True means left bat, false means right bat.
- if (side) Position = new Vector2(30, screenSize.Y / 2 - size.Height / 2);
- else Position = new Vector2(screenSize.X - 30, screenSize.Y / 2 - size.Height / 2);
- yHeight = (int)screenSize.Y;
- }
- public void IncreaseSpeed()
- {
- moveSpeed += .5f;
- }
- /// <summary>
- /// The speed of the bat when Turbo is activated
- /// </summary>
- public void Turbo()
- {
- moveSpeed += 8.0f;
- }
- /// <summary>
- /// Returns the speed of the bat back to normal after Turbo is deactivated
- /// </summary>
- public void DisableTurbo()
- {
- moveSpeed = 7.0f;
- isTurbo = false;
- }
- /// <summary>
- /// Returns the bat to the nrmal size after the Grow/Shrink powerup has expired
- /// </summary>
- public void NormalSize()
- {
- size = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
- }
- /// <summary>
- /// Checks for the size of the bat
- /// </summary>
- public Rectangle GetSize()
- {
- return size;
- }
- /// <summary>
- /// Adds point to the player or the AI after scoring. Currently Disabled.
- /// </summary>
- public void IncrementPoints()
- {
- points++;
- }
- /// <summary>
- /// Checks for the number of points at the moment
- /// </summary>
- public int GetPoints()
- {
- return points;
- }
- /// <summary>
- /// Sets thedefault starting position for the bats
- /// </summary>
- /// <param name="position"></param>
- public void SetPosition(Vector2 position)
- {
- if (position.Y < 0)
- {
- position.Y = 0;
- }
- if (position.Y > yHeight - size.Height)
- {
- position.Y = yHeight - size.Height;
- }
- this.Position = position;
- }
- /// <summary>
- /// Checks for the current position of the bat
- /// </summary>
- public Vector2 GetPosition()
- {
- return Position;
- }
- /// <summary>
- /// Controls the bat moving up the screen
- /// </summary>
- public void MoveUp()
- {
- SetPosition(Position + new Vector2(0, -moveSpeed * MoveTimer));
- }
- /// <summary>
- /// Controls the bat moving down the screen
- /// </summary>
- public void MoveDown()
- {
- SetPosition(Position + new Vector2(0, moveSpeed * MoveTimer));
- }
- /// <summary>
- /// Updates the position of the AI bat, in order to track the ball
- /// </summary>
- /// <param name="ball"></param>
- public virtual void UpdatePosition(Ball ball)
- {
- size.X = (int)Position.X;
- size.Y = (int)Position.Y;
- }
- /// <summary>
- /// Resets the bat to the center location after a new game starts
- /// </summary>
- public void ResetPosition()
- {
- SetPosition(new Vector2(GetPosition().X, yHeight / 2 - size.Height));
- }
- /// <summary>
- /// Used for the Growbat powerup
- /// </summary>
- public void GrowBat()
- {
- // Doubles the size of the bat collision
- size = new Rectangle(0, 0, leftBat.Width * 2, leftBat.Height * 2);
- }
- /// <summary>
- /// Used for the Shrinkbat powerup
- /// </summary>
- public void ShrinkBat()
- {
- // 1/2 the size of the bat collision
- size = new Rectangle(0, 0, leftBat.Width / 2, leftBat.Height / 2);
- }
- /// <summary>
- /// Draws the bats
- /// </summary>
- public virtual void Draw(SpriteBatch batch)
- {
- batch.Draw(leftBat, size, Color.White);
- }
- }
- }
- *******************************************
- /// <summary>
- /// This screen implements the actual game logic. It is just a
- /// placeholder to get the idea across: you'll probably want to
- /// put some more interesting gameplay in here!
- /// </summary>
- public class GameplayScreen : GameScreen
- {
- private float Elapsed = 0f;
- public float MoveTimer = 0f;
- public bool Slowmo = false;
- /// <summary>
- /// Lets the game respond to player input. Unlike the Update method,
- /// this will only be called when the gameplay screen is active.
- /// </summary>
- public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
- {
- base.Update(gameTime, otherScreenHasFocus, false);
- // Gradually fade in or out depending on whether we are covered by the pause screen.
- if (coveredByOtherScreen)
- {
- pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
- MediaPlayer.Pause(); // Pause gameplay music
- }
- else
- {
- pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
- MediaPlayer.Resume(); // Resum gameplay music
- }
- ////////////////////// If the game is *not* paused, then update the following: ///////////////////////////
- //frameTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
- //if (slowTime > 0f)
- //{
- // slowTime -= frameTime;
- // frameTime /= 10f;
- //}
- if (IsActive)
- {
- // SlowMo Stuff
- Elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- if (Slowmo) Elapsed *= .8f;
- MoveTimer += Elapsed;
- GamePadState state = GamePad.GetState(PlayerIndex.One);
- // Updates the input
- input.Update();
- #region Movement of bat update
- // Controls movement of the bats
- if (rightBat.GetType() != typeof(AIBat))
- {
- if (input.IsLeftPaddleMovingDown(ControllingPlayer))
- {
- leftBat.MoveDown();
- }
- else if (input.IsLeftPaddleMovingUp(ControllingPlayer))
- {
- leftBat.MoveUp();
- }
- if (input.IsRightPaddleMovingDown(ControllingPlayer))
- {
- rightBat.MoveDown();
- }
- else if (input.IsRightPaddleMovingUp(ControllingPlayer))
- {
- rightBat.MoveUp();
- }
- }
- else if (rightBat.GetType() == typeof(AIBat))
- {
- if (input.IsLeftPaddleMovingDown(ControllingPlayer))
- {
- leftBat.MoveDown();
- }
- else if (input.IsLeftPaddleMovingUp(ControllingPlayer))
- {
- leftBat.MoveUp();
- }
- if (input.IsRightPaddleMovingDown(ControllingPlayer))
- {
- leftBat.MoveDown();
- }
- else if (input.IsRightPaddleMovingUp(ControllingPlayer))
- {
- leftBat.MoveUp();
- }
- }
- #endregion
- // Updating ball and bat position
- leftBat.UpdatePosition(ball);
- rightBat.UpdatePosition(ball);
- ball.UpdatePosition();
- // Checking for collision of the bats
- if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
- {
- if (rightBat.GetSize().Intersects(ball.GetSize()))
- {
- ball.BatHit(CheckHitLocation(rightBat));
- }
- }
- else if (leftBat.GetSize().Intersects(ball.GetSize()))
- {
- ball.BatHit(CheckHitLocation(leftBat));
- }
- }
- }
RAW Paste Data