Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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.
- 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.
- 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.
- 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.
- How would you go about resolving this?
- */
- namespace Pong
- {
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Media;
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- public static GameStates gamestate;
- private GraphicsDeviceManager graphics;
- private Texture2D backgroundTexture;
- private SpriteBatch spriteBatch;
- private Bat rightBat;
- private Bat leftBat;
- private Ball ball;
- private Menu menu;
- private SpriteFont arial;
- private int resetTimer;
- private bool resetTimerInUse;
- private bool lastScored;
- private SoundEffect menuButton;
- private SoundEffect menuClose;
- public Song MainMenuSong { get; private set; }
- public Song PlayingSong { get; private set; }
- // public Song mySong;
- private Input input;
- private int screenWidth;
- private int screenHeight;
- public enum GameStates
- {
- Menu,
- Running,
- End
- }
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to run.
- /// This is where it can query for any required services and load any non-graphic
- /// related content. Calling base.Initialize will enumerate through any components
- /// and initialize them as well.
- /// </summary>
- protected override void Initialize()
- {
- screenWidth = 1280;
- screenHeight = 720;
- menu = new Menu();
- gamestate = GameStates.Menu;
- resetTimer = 0;
- resetTimerInUse = true;
- lastScored = false;
- graphics.PreferredBackBufferWidth = screenWidth;
- graphics.PreferredBackBufferHeight = screenHeight;
- graphics.IsFullScreen = false;
- graphics.ApplyChanges();
- // TODO: Add your initialization logic here
- ball = new Ball(Content, new Vector2(screenWidth, screenHeight));
- SetUpMulti();
- input = new Input();
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- arial = Content.Load<SpriteFont>("Arial");
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- backgroundTexture = Content.Load<Texture2D>(@"gfx/background");
- menuButton = Content.Load<SoundEffect>(@"sfx/menuButton");
- menuClose = Content.Load<SoundEffect>(@"sfx/menuClose");
- MainMenuSong = Content.Load<Song>(@"sfx/getWeapon");
- PlayingSong = Content.Load<Song>(@"sfx/boomer");
- MediaPlayer.IsRepeating = true;
- MediaPlayer.Play(MainMenuSong);
- // mySong = Content.Load<Song>(@"sfx/getWeapon");
- // MediaPlayer.Play(mySong);
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- private void SetUpSingle()
- {
- rightBat = new AIBat(Content, new Vector2(screenWidth, screenHeight), false);
- leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
- }
- private void SetUpMulti()
- {
- rightBat = new Bat(Content, new Vector2(screenWidth, screenHeight), false);
- leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
- }
- private void IncreaseSpeed()
- {
- ball.IncreaseSpeed();
- leftBat.IncreaseSpeed();
- rightBat.IncreaseSpeed();
- }
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- ///
- protected override void Update(GameTime gameTime)
- {
- input.Update();
- if (gamestate == GameStates.Running)
- {
- if (leftBat.GetPoints() > 5)
- {
- menu.InfoText = "Game, blouses.";
- gamestate = GameStates.End;
- }
- else if (rightBat.GetPoints() > 5)
- {
- menu.InfoText = "You just let the AI beat you.";
- gamestate = GameStates.End;
- }
- if (resetTimerInUse)
- {
- resetTimer++;
- ball.Stop();
- }
- if (resetTimer == 120)
- {
- resetTimerInUse = false;
- ball.Reset(lastScored);
- resetTimer = 0;
- }
- if (rightBat.GetType() != typeof(Pong.AIBat))
- {
- if (input.LeftDown) leftBat.MoveDown();
- else if ((input.LeftUp)) leftBat.MoveUp();
- if (input.RightDown) rightBat.MoveDown();
- else if (input.RightUp) rightBat.MoveUp();
- }
- else if (rightBat.GetType() == typeof(Pong.AIBat))
- {
- if (input.LeftDown) leftBat.MoveDown();
- else if ((input.LeftUp)) leftBat.MoveUp();
- if (input.RightDown) leftBat.MoveDown();
- else if (input.RightUp) leftBat.MoveUp();
- // Turbo speed for the bats
- if (input.SpaceDown) leftBat.Turbo();
- }
- leftBat.UpdatePosition(ball);
- rightBat.UpdatePosition(ball);
- ball.UpdatePosition();
- 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));
- }
- if (!resetTimerInUse)
- {
- if (ball.GetPosition().X > screenWidth)
- {
- resetTimerInUse = true;
- lastScored = true;
- // Checks to see if ball went out of bounds, and triggers warp sfx
- ball.OutOfBounds();
- leftBat.IncrementPoints();
- // Usually coded in.
- // IncreaseSpeed();
- }
- else if (ball.GetPosition().X < 0)
- {
- resetTimerInUse = true;
- lastScored = false;
- // Checks to see if ball went out of bounds, and triggers warp sfx
- ball.OutOfBounds();
- rightBat.IncrementPoints();
- // Usually coded in.
- // IncreaseSpeed();
- }
- }
- }
- else if (gamestate == GameStates.Menu)
- {
- if (input.RightDown || input.LeftDown)
- {
- menu.Iterator++;
- menuButton.Play();
- }
- else if (input.RightUp || input.LeftUp)
- {
- menu.Iterator--;
- menuButton.Play();
- }
- if (input.MenuSelect)
- {
- if (menu.Iterator == 0)
- {
- gamestate = GameStates.Running;
- SetUpSingle();
- menuClose.Play();
- }
- else if (menu.Iterator == 1)
- {
- gamestate = GameStates.Running;
- SetUpMulti();
- menuClose.Play();
- }
- else if (menu.Iterator == 2)
- {
- this.Exit();
- menuClose.Play();
- }
- menu.Iterator = 0;
- }
- }
- else if (gamestate == GameStates.End)
- {
- if (input.MenuSelect)
- {
- gamestate = GameStates.Menu;
- }
- }
- base.Update(gameTime);
- }
- private int CheckHitLocation(Bat bat)
- {
- int block = 0;
- if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20) block = 1;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 2) block = 2;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 3) block = 3;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 4) block = 4;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 5) block = 5;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 6) block = 6;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 7) block = 7;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 8) block = 8;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20 * 19) block = 9;
- else block = 10;
- return block;
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.Black);
- // TODO: Add your drawing code here
- spriteBatch.Begin();
- if (gamestate == GameStates.Running)
- {
- spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
- leftBat.Draw(spriteBatch);
- rightBat.Draw(spriteBatch);
- ball.Draw(spriteBatch);
- spriteBatch.DrawString(arial, leftBat.GetPoints().ToString(), new Vector2(screenWidth / 4 - arial.MeasureString
- (rightBat.GetPoints().ToString()).X, 20), Color.White);
- spriteBatch.DrawString(arial, rightBat.GetPoints().ToString(), new Vector2(screenWidth / 4 * 3 - arial.MeasureString
- (rightBat.GetPoints().ToString()).X, 20), Color.White);
- }
- else if (gamestate == GameStates.Menu)
- {
- spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
- menu.DrawMenu(spriteBatch, screenWidth, arial);
- }
- else if (gamestate == GameStates.End)
- {
- spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
- menu.DrawEndScreen(spriteBatch, screenWidth, arial);
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- 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 float turbo;
- public float interval;
- public Rectangle size;
- private int points;
- private int yHeight;
- private Texture2D leftBat;
- public Bat(ContentManager content, Vector2 screenSize, bool side)
- {
- moveSpeed = 7f;
- turbo = 15f;
- points = 0;
- interval = 5f;
- leftBat = content.Load<Texture2D>(@"gfx/batGrey");
- size = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
- 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;
- }
- public void Turbo()
- {
- moveSpeed += 7.0f;
- interval = 5;
- }
- public Rectangle GetSize()
- {
- return size;
- }
- public void IncrementPoints()
- {
- points++;
- }
- public int GetPoints()
- {
- return points;
- }
- 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;
- }
- public Vector2 GetPosition()
- {
- return position;
- }
- public void MoveUp()
- {
- SetPosition(position + new Vector2(0, -moveSpeed));
- }
- public void MoveDown()
- {
- SetPosition(position + new Vector2(0, moveSpeed));
- }
- public virtual void UpdatePosition(Ball ball)
- {
- size.X = (int)position.X;
- size.Y = (int)position.Y;
- }
- public void ResetPosition()
- {
- SetPosition(new Vector2(GetPosition().X, yHeight / 2 - size.Height));
- }
- public virtual void Draw(SpriteBatch batch)
- {
- batch.Draw(leftBat, position, Color.White);
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework.Input;
- namespace Pong
- {
- public class Input
- {
- public KeyboardState keyboardState;
- public KeyboardState lastState;
- public Input()
- {
- keyboardState = Keyboard.GetState();
- lastState = keyboardState;
- }
- public void Update()
- {
- lastState = keyboardState;
- keyboardState = Keyboard.GetState();
- }
- public bool RightUp
- {
- get
- {
- if (Game1.gamestate == Game1.GameStates.Menu)
- {
- return keyboardState.IsKeyDown(Keys.Up) && lastState.IsKeyUp(Keys.Up);
- }
- else
- {
- return keyboardState.IsKeyDown(Keys.Up);
- }
- }
- }
- public bool RightDown
- {
- get
- {
- if (Game1.gamestate == Game1.GameStates.Menu)
- {
- return keyboardState.IsKeyDown(Keys.Down) && lastState.IsKeyUp(Keys.Down);
- }
- else
- {
- return keyboardState.IsKeyDown(Keys.Down);
- }
- }
- }
- public bool LeftUp
- {
- get
- {
- if (Game1.gamestate == Game1.GameStates.Menu)
- {
- return keyboardState.IsKeyDown(Keys.W) && lastState.IsKeyUp(Keys.W);
- }
- else
- {
- return keyboardState.IsKeyDown(Keys.W);
- }
- }
- }
- public bool LeftDown
- {
- get
- {
- if (Game1.gamestate == Game1.GameStates.Menu)
- {
- return keyboardState.IsKeyDown(Keys.S) && lastState.IsKeyUp(Keys.S);
- }
- else
- {
- return keyboardState.IsKeyDown(Keys.S);
- }
- }
- }
- public bool SpaceDown
- {
- get
- {
- if (Game1.gamestate == Game1.GameStates.Menu)
- {
- return keyboardState.IsKeyDown(Keys.Space) && lastState.IsKeyUp(Keys.Space);
- }
- else
- {
- return keyboardState.IsKeyDown(Keys.Space);
- }
- }
- }
- public bool SpaceUp
- {
- get
- {
- return keyboardState.IsKeyUp(Keys.Space);
- }
- }
- public bool MenuSelect
- {
- get
- {
- return keyboardState.IsKeyDown(Keys.Enter) && lastState.IsKeyUp(Keys.Enter);
- }
- }
- public bool exitKeyPressed
- {
- get
- {
- return keyboardState.IsKeyDown(Keys.Escape);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement