Advertisement
DaveVoyles

Pong Turbo w/ Cooldown

Apr 11th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.87 KB | None | 0 0
  1. /* This is for my pong game. I have the turbo working fine, in that I hit spacebar and my speed increases from 7 (standard) to 12. After each point is scored, it increases the speed of the game by 0.5f as well, as you'll notice in the "IncreaseSpeed()" method.
  2.  
  3. The problem however, is that I can't turn this off. I would like for it to turn off after 2 seconds, and then have a brief cooldown of around 8 seconds.
  4.  
  5. I've attached my Bat class, Game1 class, and Input class. Under the Bat class, the "interval" property in my "Turbo()" method seems to have no affect, however the "moveSpeed += 7.0f" works fine.
  6.  
  7. Inside of the Game1 class you will see my if / else statements for moving the bats. I know that I'm missing something here, but not sure of how to implement it. I've looked at a few different tutorials, including Kilorn's, (http://kilorn.wordpress.com/) but haven't had much success.
  8.  
  9. How would you go about resolving this?
  10. */
  11.  
  12.  
  13. namespace Pong
  14. {
  15.     using System;
  16.     using Microsoft.Xna.Framework;
  17.     using Microsoft.Xna.Framework.Graphics;
  18.     using Microsoft.Xna.Framework.Input;
  19.     using Microsoft.Xna.Framework.Audio;
  20.     using Microsoft.Xna.Framework.Media;
  21.  
  22.  
  23.     /// <summary>
  24.     /// This is the main type for your game
  25.     /// </summary>
  26.     public class Game1 : Microsoft.Xna.Framework.Game
  27.     {
  28.        
  29.         public static GameStates gamestate;
  30.         private GraphicsDeviceManager graphics;
  31.         private Texture2D backgroundTexture;
  32.         private SpriteBatch spriteBatch;
  33.         private Bat rightBat;
  34.         private Bat leftBat;
  35.         private Ball ball;
  36.         private Menu menu;
  37.         private SpriteFont arial;
  38.         private int resetTimer;
  39.         private bool resetTimerInUse;
  40.         private bool lastScored;
  41.  
  42.         private SoundEffect menuButton;
  43.         private SoundEffect menuClose;
  44.         public Song MainMenuSong { get; private set; }
  45.         public Song PlayingSong { get; private set; }
  46. //        public Song mySong;
  47.        
  48.         private Input input;
  49.         private int screenWidth;
  50.         private int screenHeight;
  51.  
  52.         public enum GameStates
  53.         {
  54.             Menu,
  55.             Running,
  56.             End
  57.         }
  58.  
  59.         public Game1()
  60.         {
  61.             graphics = new GraphicsDeviceManager(this);
  62.             Content.RootDirectory = "Content";
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Allows the game to perform any initialization it needs to before starting to run.
  67.         /// This is where it can query for any required services and load any non-graphic
  68.         /// related content.  Calling base.Initialize will enumerate through any components
  69.         /// and initialize them as well.
  70.         /// </summary>
  71.         protected override void Initialize()
  72.         {
  73.             screenWidth = 1280;
  74.             screenHeight = 720;
  75.             menu = new Menu();
  76.             gamestate = GameStates.Menu;
  77.             resetTimer = 0;
  78.             resetTimerInUse = true;
  79.             lastScored = false;
  80.             graphics.PreferredBackBufferWidth = screenWidth;
  81.             graphics.PreferredBackBufferHeight = screenHeight;
  82.             graphics.IsFullScreen = false;
  83.             graphics.ApplyChanges();
  84.  
  85.  
  86.             // TODO: Add your initialization logic here
  87.             ball = new Ball(Content, new Vector2(screenWidth, screenHeight));
  88.             SetUpMulti();
  89.             input = new Input();
  90.             base.Initialize();
  91.         }
  92.  
  93.  
  94.         /// <summary>
  95.         /// LoadContent will be called once per game and is the place to load
  96.         /// all of your content.
  97.         /// </summary>
  98.         protected override void LoadContent()
  99.         {
  100.             arial = Content.Load<SpriteFont>("Arial");
  101.             // Create a new SpriteBatch, which can be used to draw textures.
  102.             spriteBatch = new SpriteBatch(GraphicsDevice);
  103.             backgroundTexture = Content.Load<Texture2D>(@"gfx/background");
  104.             menuButton = Content.Load<SoundEffect>(@"sfx/menuButton");
  105.             menuClose = Content.Load<SoundEffect>(@"sfx/menuClose");
  106.             MainMenuSong = Content.Load<Song>(@"sfx/getWeapon");
  107.             PlayingSong = Content.Load<Song>(@"sfx/boomer");
  108.             MediaPlayer.IsRepeating = true;
  109.             MediaPlayer.Play(MainMenuSong);
  110.             //            mySong = Content.Load<Song>(@"sfx/getWeapon");
  111.             //            MediaPlayer.Play(mySong);
  112.  
  113.            
  114.         }
  115.  
  116.         /// <summary>
  117.         /// UnloadContent will be called once per game and is the place to unload
  118.         /// all content.
  119.         /// </summary>
  120.         protected override void UnloadContent()
  121.         {
  122.             // TODO: Unload any non ContentManager content here
  123.         }
  124.  
  125.         private void SetUpSingle()
  126.         {
  127.             rightBat = new AIBat(Content, new Vector2(screenWidth, screenHeight), false);
  128.             leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
  129.         }
  130.  
  131.         private void SetUpMulti()
  132.         {
  133.             rightBat = new Bat(Content, new Vector2(screenWidth, screenHeight), false);
  134.             leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
  135.         }
  136.  
  137.         private void IncreaseSpeed()
  138.         {
  139.             ball.IncreaseSpeed();
  140.             leftBat.IncreaseSpeed();
  141.             rightBat.IncreaseSpeed();
  142.         }
  143.  
  144.  
  145.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  146.         ///
  147.         protected override void Update(GameTime gameTime)
  148.         {
  149.  
  150.             input.Update();
  151.  
  152.             if (gamestate == GameStates.Running)
  153.             {
  154.                 if (leftBat.GetPoints() > 5)
  155.                 {
  156.                     menu.InfoText = "Game, blouses.";
  157.                     gamestate = GameStates.End;
  158.                 }
  159.                 else if (rightBat.GetPoints() > 5)
  160.                 {
  161.                     menu.InfoText = "You just let the AI beat you.";
  162.                     gamestate = GameStates.End;
  163.                 }
  164.                 if (resetTimerInUse)
  165.                 {
  166.                     resetTimer++;
  167.                     ball.Stop();                
  168.                 }
  169.  
  170.                 if (resetTimer == 120)
  171.                 {
  172.                     resetTimerInUse = false;
  173.                     ball.Reset(lastScored);
  174.                     resetTimer = 0;
  175.                 }
  176.  
  177.                 if (rightBat.GetType() != typeof(Pong.AIBat))
  178.                 {
  179.                     if (input.LeftDown) leftBat.MoveDown();
  180.                     else if ((input.LeftUp)) leftBat.MoveUp();
  181.                     if (input.RightDown) rightBat.MoveDown();
  182.                     else if (input.RightUp) rightBat.MoveUp();
  183.                                                    
  184.                 }
  185.                 else if (rightBat.GetType() == typeof(Pong.AIBat))
  186.                 {
  187.                     if (input.LeftDown) leftBat.MoveDown();
  188.                     else if ((input.LeftUp)) leftBat.MoveUp();
  189.                     if (input.RightDown) leftBat.MoveDown();
  190.                     else if (input.RightUp) leftBat.MoveUp();
  191.             // Turbo speed for the bats                    
  192.             if (input.SpaceDown) leftBat.Turbo();
  193.            
  194.                 }
  195.  
  196.              
  197.                 leftBat.UpdatePosition(ball);
  198.                 rightBat.UpdatePosition(ball);
  199.                 ball.UpdatePosition();
  200.                 if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
  201.                 {
  202.                     if (rightBat.GetSize().Intersects(ball.GetSize()))
  203.                     {
  204.                         ball.BatHit(CheckHitLocation(rightBat));
  205.                     }
  206.                 }
  207.                 else if (leftBat.GetSize().Intersects(ball.GetSize()))
  208.                 {
  209.                     ball.BatHit(CheckHitLocation(leftBat));
  210.                 }
  211.  
  212.  
  213.  
  214.                 if (!resetTimerInUse)
  215.                 {
  216.                     if (ball.GetPosition().X > screenWidth)
  217.                     {
  218.                         resetTimerInUse = true;
  219.                         lastScored = true;
  220.  
  221.                         // Checks to see if ball went out of bounds, and triggers warp sfx
  222.                         ball.OutOfBounds();
  223.                         leftBat.IncrementPoints();
  224.                        // Usually coded in.
  225.                       //  IncreaseSpeed();
  226.                     }
  227.                     else if (ball.GetPosition().X < 0)
  228.                     {
  229.  
  230.                         resetTimerInUse = true;
  231.                         lastScored = false;
  232.  
  233.                         // Checks to see if ball went out of bounds, and triggers warp sfx
  234.                         ball.OutOfBounds();
  235.                         rightBat.IncrementPoints();
  236.                         // Usually coded in.
  237.                       //  IncreaseSpeed();
  238.                     }
  239.                 }
  240.             }
  241.             else if (gamestate == GameStates.Menu)
  242.                  
  243.             {
  244.  
  245.  
  246.                 if (input.RightDown || input.LeftDown)
  247.                 {
  248.                     menu.Iterator++;
  249.                     menuButton.Play();
  250.                 }
  251.                 else if (input.RightUp || input.LeftUp)
  252.                 {
  253.                     menu.Iterator--;
  254.                     menuButton.Play();
  255.                 }
  256.  
  257.                 if (input.MenuSelect)
  258.                 {
  259.  
  260.                     if (menu.Iterator == 0)
  261.                     {
  262.                         gamestate = GameStates.Running;
  263.                         SetUpSingle();
  264.                         menuClose.Play();
  265.                     }
  266.                     else if (menu.Iterator == 1)
  267.                     {
  268.                         gamestate = GameStates.Running;
  269.                         SetUpMulti();
  270.                         menuClose.Play();
  271.                     }
  272.                     else if (menu.Iterator == 2)
  273.                     {
  274.                         this.Exit();
  275.                         menuClose.Play();
  276.                     }
  277.                     menu.Iterator = 0;
  278.  
  279.                 }
  280.  
  281.             }
  282.  
  283.             else if (gamestate == GameStates.End)
  284.             {
  285.                 if (input.MenuSelect)
  286.                 {
  287.                     gamestate = GameStates.Menu;
  288.  
  289.                 }
  290.             }
  291.  
  292.  
  293.             base.Update(gameTime);
  294.         }
  295.  
  296.         private int CheckHitLocation(Bat bat)
  297.         {
  298.             int block = 0;
  299.             if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20) block = 1;
  300.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 2) block = 2;
  301.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 3) block = 3;
  302.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 4) block = 4;
  303.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 5) block = 5;
  304.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 6) block = 6;
  305.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 7) block = 7;
  306.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 8) block = 8;
  307.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20 * 19) block = 9;
  308.             else block = 10;
  309.             return block;
  310.         }
  311.  
  312.  
  313.         protected override void Draw(GameTime gameTime)
  314.         {
  315.             GraphicsDevice.Clear(Color.Black);
  316.  
  317.             // TODO: Add your drawing code here
  318.             spriteBatch.Begin();
  319.             if (gamestate == GameStates.Running)
  320.             {
  321.                 spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  322.                 leftBat.Draw(spriteBatch);
  323.                 rightBat.Draw(spriteBatch);
  324.                 ball.Draw(spriteBatch);
  325.                 spriteBatch.DrawString(arial, leftBat.GetPoints().ToString(), new Vector2(screenWidth / 4 - arial.MeasureString
  326.                     (rightBat.GetPoints().ToString()).X, 20), Color.White);
  327.                 spriteBatch.DrawString(arial, rightBat.GetPoints().ToString(), new Vector2(screenWidth / 4 * 3 - arial.MeasureString
  328.                     (rightBat.GetPoints().ToString()).X, 20), Color.White);
  329.                                
  330.             }
  331.             else if (gamestate == GameStates.Menu)
  332.             {
  333.                 spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  334.                 menu.DrawMenu(spriteBatch, screenWidth, arial);
  335.             }
  336.             else if (gamestate == GameStates.End)
  337.             {
  338.                 spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  339.                 menu.DrawEndScreen(spriteBatch, screenWidth, arial);
  340.             }
  341.             spriteBatch.End();
  342.  
  343.             base.Draw(gameTime);
  344.         }
  345.  
  346.     }
  347. }
  348.  
  349.  
  350.  
  351. //////////////////////////////////////////////////////////////////////////////////////////////
  352.  
  353. namespace Pong
  354. {
  355.     using Microsoft.Xna.Framework;
  356.     using Microsoft.Xna.Framework.Content;
  357.     using Microsoft.Xna.Framework.Graphics;
  358.     using System;
  359.  
  360.     public class Bat
  361.     {
  362.         public Vector2 position;
  363.         public float moveSpeed;
  364.         public float turbo;
  365.         public float interval;
  366.         public Rectangle size;
  367.         private int points;
  368.         private int yHeight;
  369.         private Texture2D leftBat;
  370.        
  371.  
  372.         public Bat(ContentManager content, Vector2 screenSize, bool side)
  373.         {
  374.             moveSpeed = 7f;
  375.             turbo = 15f;
  376.             points = 0;
  377.             interval = 5f;
  378.             leftBat = content.Load<Texture2D>(@"gfx/batGrey");
  379.             size = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
  380.             if (side) position = new Vector2(30, screenSize.Y / 2 - size.Height / 2);
  381.             else position = new Vector2(screenSize.X - 30, screenSize.Y / 2 - size.Height / 2);
  382.             yHeight = (int)screenSize.Y;
  383.         }
  384.  
  385.         public void IncreaseSpeed()
  386.         {
  387.             moveSpeed += .5f;
  388.            
  389.         }
  390.  
  391.         public void Turbo()
  392.         {
  393.             moveSpeed += 7.0f;
  394.             interval = 5;
  395.         }
  396.  
  397.         public Rectangle GetSize()
  398.         {
  399.             return size;
  400.         }
  401.  
  402.         public void IncrementPoints()
  403.         {
  404.             points++;
  405.         }
  406.  
  407.         public int GetPoints()
  408.         {
  409.             return points;
  410.         }
  411.  
  412.         public void SetPosition(Vector2 position)
  413.         {
  414.             if (position.Y < 0)
  415.             {
  416.                 position.Y = 0;
  417.             }
  418.             if (position.Y > yHeight - size.Height)
  419.             {
  420.                 position.Y = yHeight - size.Height;
  421.             }
  422.             this.position = position;
  423.         }
  424.  
  425.         public Vector2 GetPosition()
  426.         {
  427.             return position;
  428.         }
  429.  
  430.         public void MoveUp()
  431.         {
  432.             SetPosition(position + new Vector2(0, -moveSpeed));
  433.         }
  434.  
  435.         public void MoveDown()
  436.         {
  437.             SetPosition(position + new Vector2(0, moveSpeed));
  438.         }
  439.  
  440.  
  441.         public virtual void UpdatePosition(Ball ball)
  442.         {
  443.             size.X = (int)position.X;
  444.             size.Y = (int)position.Y;
  445.         }
  446.  
  447.         public void ResetPosition()
  448.         {
  449.             SetPosition(new Vector2(GetPosition().X, yHeight / 2 - size.Height));
  450.         }
  451.  
  452.  
  453.  
  454.         public virtual void Draw(SpriteBatch batch)
  455.         {
  456.             batch.Draw(leftBat, position, Color.White);
  457.         }
  458.  
  459.    
  460.     }
  461. }
  462.  
  463.  
  464.  
  465. ////////////////////////////////////////////////////////////////////////////////////////////
  466.  
  467.  
  468. using System;
  469. using System.Collections.Generic;
  470. using System.Linq;
  471. using System.Text;
  472. using Microsoft.Xna.Framework.Input;
  473.  
  474. namespace Pong
  475. {
  476.     public class Input
  477.     {
  478.        
  479.         public KeyboardState keyboardState;
  480.         public KeyboardState lastState;
  481.  
  482.         public Input()
  483.         {
  484.             keyboardState = Keyboard.GetState();
  485.             lastState = keyboardState;
  486.         }
  487.  
  488.         public void Update()
  489.         {
  490.             lastState = keyboardState;
  491.             keyboardState = Keyboard.GetState();
  492.         }
  493.  
  494.  
  495.  
  496.         public bool RightUp
  497.         {
  498.             get
  499.             {
  500.                 if (Game1.gamestate == Game1.GameStates.Menu)
  501.                 {
  502.                     return keyboardState.IsKeyDown(Keys.Up) && lastState.IsKeyUp(Keys.Up);
  503.                 }
  504.                 else
  505.                 {
  506.                     return keyboardState.IsKeyDown(Keys.Up);
  507.                 }
  508.             }
  509.         }
  510.  
  511.         public bool RightDown
  512.         {
  513.             get
  514.             {
  515.                 if (Game1.gamestate == Game1.GameStates.Menu)
  516.                 {
  517.                     return keyboardState.IsKeyDown(Keys.Down) && lastState.IsKeyUp(Keys.Down);
  518.                 }
  519.                 else
  520.                 {
  521.                     return keyboardState.IsKeyDown(Keys.Down);
  522.                 }
  523.             }
  524.         }
  525.  
  526.         public bool LeftUp
  527.         {
  528.             get
  529.             {
  530.                 if (Game1.gamestate == Game1.GameStates.Menu)
  531.                 {
  532.                     return keyboardState.IsKeyDown(Keys.W) && lastState.IsKeyUp(Keys.W);
  533.                 }
  534.                 else
  535.                 {
  536.                     return keyboardState.IsKeyDown(Keys.W);
  537.                 }
  538.             }
  539.         }
  540.  
  541.         public bool LeftDown
  542.         {
  543.             get
  544.             {
  545.                 if (Game1.gamestate == Game1.GameStates.Menu)
  546.                 {
  547.                     return keyboardState.IsKeyDown(Keys.S) && lastState.IsKeyUp(Keys.S);
  548.                 }
  549.                 else
  550.                 {
  551.                     return keyboardState.IsKeyDown(Keys.S);
  552.                 }
  553.             }
  554.         }
  555.  
  556.         public bool SpaceDown
  557.         {
  558.             get
  559.             {
  560.                 if (Game1.gamestate == Game1.GameStates.Menu)
  561.                 {
  562.                     return keyboardState.IsKeyDown(Keys.Space) && lastState.IsKeyUp(Keys.Space);
  563.                 }
  564.                 else
  565.                 {
  566.                     return keyboardState.IsKeyDown(Keys.Space);
  567.                 }
  568.             }
  569.         }
  570.  
  571.         public bool SpaceUp
  572.         {
  573.             get
  574.             {    
  575.                     return keyboardState.IsKeyUp(Keys.Space);
  576.             }
  577.         }
  578.  
  579.         public bool MenuSelect
  580.         {
  581.             get
  582.             {
  583.                 return keyboardState.IsKeyDown(Keys.Enter) && lastState.IsKeyUp(Keys.Enter);
  584.             }
  585.         }
  586.  
  587.        public bool exitKeyPressed
  588.         {
  589.            get
  590.            {
  591.                return keyboardState.IsKeyDown(Keys.Escape);
  592.            }
  593.         }
  594.  
  595.     }
  596. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement