/// <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 ) { KeyboardState keyState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); GamePadState padState = GamePad.GetState(PlayerIndex.One); // Allows the game to exit if ((padState.Buttons.Back == ButtonState.Pressed) || (keyState.IsKeyDown(Keys.Escape))) { this.Exit(); } if (activeGameState == GameState.Paused || activeGameState == GameState.Playing) { //Reset if (keyState.IsKeyDown(Keys.R)) { gameResetKeyDown = true; } else if (keyState.IsKeyUp(Keys.R) && gameResetKeyDown) { gameResetKeyDown = false; //Reset The Game Here Reset(); } CheckPauseKey(keyState); } //Screenie if (keyState.IsKeyDown(Keys.PrintScreen)) { gameScreenieKeyDown = true; } else if (keyState.IsKeyUp(Keys.PrintScreen) && gameScreenieKeyDown) { gameScreenieKeyDown = false; activeGameState = GameState.Paused; //Find A Place TO Save Screenshot :) ulong append = 0; if ( !Directory.Exists("Screenshots") ) { Directory.CreateDirectory("Screenshots"); } while (File.Exists("Screenshots\\underAttackScreen-" + append + ".png")) { append++; } Stream fs = File.OpenWrite("Screenshots\\underAttackScreen-" + append + ".png"); Texture2D t2d = (Texture2D)(graphics.GraphicsDevice.GetRenderTargets()[0].RenderTarget); t2d.SaveAsPng(fs, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); fs.Close(); } if (activeGameState == GameState.Playing) { if ((gameTime.TotalGameTime.TotalMilliseconds - lastAnimUpdate) > 100) { foreach (GameUnit unit in animations) { if (unit.IsAnimating) { unit.CurrentFrame = unit.CurrentFrame + 1; if (unit.CurrentFrame >= (sprites[unit.SpriteName].FrameCount)) { deadAnims.Add(unit); } } } //For Time effects timePassed++; timeParam.SetValue(timePassed / 20f); lastAnimUpdate = gameTime.TotalGameTime.TotalMilliseconds; } //Count time alive timeAlive += gameTime.ElapsedGameTime.TotalSeconds; //Motion Input CheckPlayerInput(keyState); //Move all Units where auto-motion is req. MoveGameUnits(); //Check For A Press And Release To Fire...Make Sure Every Release Is atleast 250 Ms apart if (keyState.IsKeyDown(Keys.Space) && !gameShootKeyDown && ((gameTime.TotalGameTime.TotalMilliseconds - lastShotUpdate) > 200) ) { gameShootKeyDown = true; } else if (keyState.IsKeyUp(Keys.Space) && gameShootKeyDown) { GameUnit bullet = new GameUnit("bullet", playerOne.Position - new Vector2(0, 32), new Vector2(4f, 4f)); bullet.Velocity = new Vector2(0, -2); units.Add(bullet); //Update last shot time lastShotUpdate = gameTime.TotalGameTime.TotalMilliseconds; //Reset Press variable gameShootKeyDown = false; } //Remove Dead Units foreach (GameUnit e in deadBullets) { units.Remove(e); } foreach (GameUnit e in deadUnits) { units.Remove(e); } foreach (GameUnit e in deadAnims) { units.Remove(e); } //Clear already dead units. deadBullets.Clear(); deadUnits.Clear(); deadAnims.Clear(); } switch ( activeGameState ) { case GameState.GameOver: case GameState.GameMenu: mainMenuContainer.Update(mouseState); break; case GameState.Paused: pauseMenuContainer.Update(mouseState); break; } base.Update(gameTime); }
private void CheckCollisions( GameUnit unit ) { if (unit.SpriteName.Contains("cloud")) { if (unit.Bounds.Top > 600) { unit.Position = new Vector2(200,-64) + new Vector2(randGen.Next(-180, 180), 0); } } else if (unit.SpriteName.Contains("tree")) { if (unit.Bounds.Top > 600) { unit.Position = unit.Position * Vector2.UnitX + new Vector2(randGen.Next(-10, 10), -64); } } else if (unit.SpriteName.Contains("ebullet")) { if (unit.Bounds.Top > 600) { deadBullets.Add(unit); } else { if (unit.IsCollidingWith(playerOne)) { //GAME OVER D= deadBullets.Add(unit); //Run BOOM Animation GameUnit anim = new GameUnit("explosion", playerOne.Position, new Vector2(64, 64)); anim.CurrentFrame = 0; anim.IsAnimating = true; animations.Add(anim); //Run Sound Effect soundBoom.Play(); //Dead deadUnits.Add(playerOne); activeGameState = GameState.GameOver; this.IsMouseVisible = true; } } } else if (unit.SpriteName.Contains("bullet")) { if (unit.Bounds.Top < 0) { deadBullets.Add(unit); } else { foreach (GameUnit e in enemies) { if (unit.IsCollidingWith(e)) { deadBullets.Add(unit); //Run BOOM Animation GameUnit anim = new GameUnit("explosion", e.Position, new Vector2(64, 64)); anim.CurrentFrame = 0; anim.IsAnimating = true; animations.Add(anim); //Run Sound Effect soundBoom.Play(); playerOne.Score += (float)(Math.Pow(100, ((double)e.Position.Y / 600d))) * (float)(1f / 600f * (float)e.Position.Y); //Spawn 'New' Enemy e.Position = new Vector2(e.Position.X, 0) + new Vector2(0, randGen.Next(-200, -64)); playerOne.Score += (float)timeAlive; break; } } } } else if (unit.SpriteName.Contains("enemy")) { //ENEMY! if (unit.IsCollidingWith(playerOne) || unit.Bounds.Bottom >= 600) { //Game Over... //Run BOOM Animation GameUnit anim = new GameUnit("explosion", unit.Position, new Vector2(64, 64)); anim.CurrentFrame = 0; anim.IsAnimating = true; GameUnit anim2 = new GameUnit("explosion", playerOne.Position, new Vector2(64, 64)); anim2.CurrentFrame = 0; anim2.IsAnimating = true; animations.Add(anim); animations.Add(anim2); //Run Sound Effect soundBoom.Play(); soundBoom.Play(); deadUnits.Add(playerOne); deadUnits.Add(unit); enemies.Remove(unit); playerOne.Score += (float)timeAlive; activeGameState = GameState.GameOver; this.IsMouseVisible = true; } } }
protected void LoadTree( ) { Texture2D treeTex = this.Content.Load<Texture2D>("Environment\\Tree"); sprites.Add("tree", new Sprite(treeTex, new Rectangle(0, 0, 64, 64), 1)); for (int i = 0; i < 18; i++) { for (int y = 0; y < 21; y++) { GameUnit tree = new GameUnit("tree", new Vector2(i * 32, y * 32 - 32), new Vector2(64)); tree.Velocity = new Vector2(0, 0.5f); tree.Position = tree.Position + new Vector2(randGen.Next(-10,10), 0); units.Add(tree); } } }
protected void MoveGameUnits( ) { List<GameUnit> newBullets = new List<GameUnit>(); foreach (GameUnit unit in units) { if (!unit.SpriteName.Contains("player")) { //Move the Game Unit unit.Position = unit.Position + unit.Velocity; //Randomly Shoot at Player [If You ARE AN ENEMY] if ((randGen.Next(0, 100) <= 10) && unit.SpriteName.Contains("enemy")) { //Will The Shot Be Within Means? (Within My Scope Of Fire) GameUnit bullet = new GameUnit("ebullet", unit.Position + new Vector2(0, 32), new Vector2(4f, 4f)); Vector2 bulletVelocity = 2 * Vector2.Normalize(playerOne.Position - bullet.Position + new Vector2(randGen.Next(-2, 2), randGen.Next(-2, 2))); if (bulletVelocity.X > -2 && bulletVelocity.X < 2) { //Now To Check Whether I have to cool down or not.. if (unit.CoolDown == 0) { //No Cooldown? Cool...Let's Shoot at the player =D bullet.Velocity = bulletVelocity; newBullets.Add(bullet); unit.CoolDown = 75; } else { unit.CoolDown--; } } } CheckCollisions(unit); } } units.AddRange(newBullets); newBullets.Clear(); }
protected void LoadClouds( ) { Texture2D cloud1 = this.Content.Load<Texture2D>("Environment\\Cloud1"); sprites.Add("cloud1", new Sprite(cloud1, new Rectangle(0, 0, 128, 128), 1)); for (int i = 0; i < 10; i++) { GameUnit cloud = new GameUnit("cloud1", new Vector2(200, 0), new Vector2(128)); cloud.Velocity = new Vector2(0, 1f); cloud.Position = cloud.Position + new Vector2(randGen.Next(-180,180), randGen.Next(-600,0)); units.Add(cloud); } }
protected void LoadEnemies( ) { Texture2D enemyTex = this.Content.Load<Texture2D>("Sprites\\Enemy"); sprites.Add("enemy", new Sprite(enemyTex, new Rectangle(0, 0, 64, 64), 1)); //Place 5 Enemies In Total..which will be recycled... for (int i = 0; i < 5; i++) { GameUnit e = new GameUnit("enemy", new Vector2(i * 64 + 70, 0), new Vector2(64)); e.Velocity = new Vector2(0, 1f); e.Position = e.Position + new Vector2(randGen.Next(-10, 10), randGen.Next(-600, -100)); units.Add(e); enemies.Add(e); } }
/// <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 ) { KeyboardState keyState = Keyboard.GetState(); GamePadState padState = GamePad.GetState(PlayerIndex.One); // Allows the game to exit if ((padState.Buttons.Back == ButtonState.Pressed) || (keyState.IsKeyDown(Keys.Escape))) { this.Exit(); } //Reset if (keyState.IsKeyDown(Keys.R)) { gameResetKeyDown = true; } else if (keyState.IsKeyUp(Keys.R) && gameResetKeyDown) { gameResetKeyDown = false; //Reset The Game Here Reset(); } CheckPauseMenu(keyState, Mouse.GetState()); //Screenie if (keyState.IsKeyDown(Keys.PrintScreen)) { gameScreenieKeyDown = true; } else if (keyState.IsKeyUp(Keys.PrintScreen) && gameScreenieKeyDown) { gameScreenieKeyDown = false; gamePaused = true; ResolveTexture2D screenShot = new ResolveTexture2D(graphics.GraphicsDevice, 400, 600, 0, SurfaceFormat.Vector4); graphics.GraphicsDevice.ResolveBackBuffer(screenShot); //Find A Place TO Save Screenshot :) ulong append = 0; if ( !Directory.Exists("Screenshots") ) { Directory.CreateDirectory("Screenshots"); } while (File.Exists("Screenshots\\underAttackScreen-" + append + ".png")) { append++; } screenShot.Save("Screenshots\\underAttackScreen-" + append + ".png", ImageFileFormat.Png); } if ((gameTime.TotalGameTime.TotalMilliseconds - lastAnimUpdate) > 100) { foreach (GameUnit unit in animations) { if (unit.IsAnimating) { unit.CurrentFrame = unit.CurrentFrame + 1; if (unit.CurrentFrame >= (sprites[unit.SpriteName].FrameCount)) { deadAnims.Add(unit); } } } //Time Updating here too - ONLY when game is running if (!gameOver && !gamePaused) { timePassed++; timeParam.SetValue(timePassed / 20f); } lastAnimUpdate = gameTime.TotalGameTime.TotalMilliseconds; } if (!gameOver && !gamePaused) { //Count time alive timeAlive += gameTime.ElapsedGameTime.TotalSeconds; //Motion Input CheckPlayerInput(keyState); //Move all Units where auto-motion is req. MoveGameUnits(); //Check For A Press And Release To Fire...Make Sure Every Release Is atleast 250 Ms apart if (keyState.IsKeyDown(Keys.Space) && !gameShootKeyDown && ((gameTime.TotalGameTime.TotalMilliseconds - lastShotUpdate) > 200) ) { gameShootKeyDown = true; } else if (keyState.IsKeyUp(Keys.Space) && gameShootKeyDown) { GameUnit bullet = new GameUnit("bullet", playerOne.Position - new Vector2(0, 32), new Vector2(4f, 4f)); bullet.Velocity = new Vector2(0, -2); units.Add(bullet); //Update last shot time lastShotUpdate = gameTime.TotalGameTime.TotalMilliseconds; //Reset Press variable gameShootKeyDown = false; } //Remove Dead Units foreach (GameUnit e in deadBullets) { units.Remove(e); } foreach (GameUnit e in deadUnits) { units.Remove(e); } foreach (GameUnit e in deadAnims) { units.Remove(e); } //Clear already dead units. deadBullets.Clear(); deadUnits.Clear(); deadAnims.Clear(); } base.Update(gameTime); }
public bool IsCollidingWith( GameUnit unit ) { return (bounds.Intersects(unit.bounds)); }
private void CheckCollisions(GameUnit unit) { if (unit.SpriteName.Contains("cloud")) { if (unit.Bounds.Top > 600) { unit.Position = new Vector2(200, -64) + new Vector2(randGen.Next(-180, 180), 0); } } else if (unit.SpriteName.Contains("tree")) { if (unit.Bounds.Top > 600) { unit.Position = unit.Position * Vector2.UnitX + new Vector2(randGen.Next(-10, 10), -64); } } else if (unit.SpriteName.Contains("ebullet")) { if (unit.Bounds.Top > 600) { deadBullets.Add(unit); } else { if (unit.IsCollidingWith(playerOne)) { //GAME OVER D= deadBullets.Add(unit); //Run BOOM Animation GameUnit anim = new GameUnit("explosion", playerOne.Position, new Vector2(64, 64)); anim.CurrentFrame = 0; anim.IsAnimating = true; animations.Add(anim); //Run Sound Effect soundBoom.Play(); //Dead deadUnits.Add(playerOne); activeGameState = GameState.GameOver; this.IsMouseVisible = true; } } } else if (unit.SpriteName.Contains("bullet")) { if (unit.Bounds.Top < 0) { deadBullets.Add(unit); } else { foreach (GameUnit e in enemies) { if (unit.IsCollidingWith(e)) { deadBullets.Add(unit); //Run BOOM Animation GameUnit anim = new GameUnit("explosion", e.Position, new Vector2(64, 64)); anim.CurrentFrame = 0; anim.IsAnimating = true; animations.Add(anim); //Run Sound Effect soundBoom.Play(); playerOne.Score += (float)(Math.Pow(100, ((double)e.Position.Y / 600d))) * (float)(1f / 600f * (float)e.Position.Y); //Spawn 'New' Enemy e.Position = new Vector2(e.Position.X, 0) + new Vector2(0, randGen.Next(-200, -64)); playerOne.Score += (float)timeAlive; break; } } } } else if (unit.SpriteName.Contains("enemy")) { //ENEMY! if (unit.IsCollidingWith(playerOne) || unit.Bounds.Bottom >= 600) { //Game Over... //Run BOOM Animation GameUnit anim = new GameUnit("explosion", unit.Position, new Vector2(64, 64)); anim.CurrentFrame = 0; anim.IsAnimating = true; GameUnit anim2 = new GameUnit("explosion", playerOne.Position, new Vector2(64, 64)); anim2.CurrentFrame = 0; anim2.IsAnimating = true; animations.Add(anim); animations.Add(anim2); //Run Sound Effect soundBoom.Play(); soundBoom.Play(); deadUnits.Add(playerOne); deadUnits.Add(unit); enemies.Remove(unit); playerOne.Score += (float)timeAlive; activeGameState = GameState.GameOver; this.IsMouseVisible = true; } } }
/// <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) { KeyboardState keyState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); GamePadState padState = GamePad.GetState(PlayerIndex.One); // Allows the game to exit if ((padState.Buttons.Back == ButtonState.Pressed) || (keyState.IsKeyDown(Keys.Escape))) { this.Exit(); } if (activeGameState == GameState.Paused || activeGameState == GameState.Playing) { //Reset if (keyState.IsKeyDown(Keys.R)) { gameResetKeyDown = true; } else if (keyState.IsKeyUp(Keys.R) && gameResetKeyDown) { gameResetKeyDown = false; //Reset The Game Here Reset(); } CheckPauseKey(keyState); } //Screenie if (keyState.IsKeyDown(Keys.PrintScreen)) { gameScreenieKeyDown = true; } else if (keyState.IsKeyUp(Keys.PrintScreen) && gameScreenieKeyDown) { gameScreenieKeyDown = false; activeGameState = GameState.Paused; //Find A Place TO Save Screenshot :) ulong append = 0; if (!Directory.Exists("Screenshots")) { Directory.CreateDirectory("Screenshots"); } while (File.Exists("Screenshots\\underAttackScreen-" + append + ".png")) { append++; } Stream fs = File.OpenWrite("Screenshots\\underAttackScreen-" + append + ".png"); Texture2D t2d = (Texture2D)(graphics.GraphicsDevice.GetRenderTargets()[0].RenderTarget); t2d.SaveAsPng(fs, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); fs.Close(); } if (activeGameState == GameState.Playing) { if ((gameTime.TotalGameTime.TotalMilliseconds - lastAnimUpdate) > 100) { foreach (GameUnit unit in animations) { if (unit.IsAnimating) { unit.CurrentFrame = unit.CurrentFrame + 1; if (unit.CurrentFrame >= (sprites[unit.SpriteName].FrameCount)) { deadAnims.Add(unit); } } } //For Time effects timePassed++; timeParam.SetValue(timePassed / 20f); lastAnimUpdate = gameTime.TotalGameTime.TotalMilliseconds; } //Count time alive timeAlive += gameTime.ElapsedGameTime.TotalSeconds; //Motion Input CheckPlayerInput(keyState); //Move all Units where auto-motion is req. MoveGameUnits(); //Check For A Press And Release To Fire...Make Sure Every Release Is atleast 250 Ms apart if (keyState.IsKeyDown(Keys.Space) && !gameShootKeyDown && ((gameTime.TotalGameTime.TotalMilliseconds - lastShotUpdate) > 200)) { gameShootKeyDown = true; } else if (keyState.IsKeyUp(Keys.Space) && gameShootKeyDown) { GameUnit bullet = new GameUnit("bullet", playerOne.Position - new Vector2(0, 32), new Vector2(4f, 4f)); bullet.Velocity = new Vector2(0, -2); units.Add(bullet); //Update last shot time lastShotUpdate = gameTime.TotalGameTime.TotalMilliseconds; //Reset Press variable gameShootKeyDown = false; } //Remove Dead Units foreach (GameUnit e in deadBullets) { units.Remove(e); } foreach (GameUnit e in deadUnits) { units.Remove(e); } foreach (GameUnit e in deadAnims) { units.Remove(e); } //Clear already dead units. deadBullets.Clear(); deadUnits.Clear(); deadAnims.Clear(); } switch (activeGameState) { case GameState.GameOver: case GameState.GameMenu: mainMenuContainer.Update(mouseState); break; case GameState.Paused: pauseMenuContainer.Update(mouseState); 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) { KeyboardState keyState = Keyboard.GetState(); GamePadState padState = GamePad.GetState(PlayerIndex.One); // Allows the game to exit if ((padState.Buttons.Back == ButtonState.Pressed) || (keyState.IsKeyDown(Keys.Escape))) { this.Exit(); } //Reset if (keyState.IsKeyDown(Keys.R)) { gameResetKeyDown = true; } else if (keyState.IsKeyUp(Keys.R) && gameResetKeyDown) { gameResetKeyDown = false; //Reset The Game Here Reset(); } CheckPauseMenu(keyState, Mouse.GetState()); //Screenie if (keyState.IsKeyDown(Keys.PrintScreen)) { gameScreenieKeyDown = true; } else if (keyState.IsKeyUp(Keys.PrintScreen) && gameScreenieKeyDown) { gameScreenieKeyDown = false; gamePaused = true; ResolveTexture2D screenShot = new ResolveTexture2D(graphics.GraphicsDevice, 400, 600, 0, SurfaceFormat.Vector4); graphics.GraphicsDevice.ResolveBackBuffer(screenShot); //Find A Place TO Save Screenshot :) ulong append = 0; if (!Directory.Exists("Screenshots")) { Directory.CreateDirectory("Screenshots"); } while (File.Exists("Screenshots\\underAttackScreen-" + append + ".png")) { append++; } screenShot.Save("Screenshots\\underAttackScreen-" + append + ".png", ImageFileFormat.Png); } if ((gameTime.TotalGameTime.TotalMilliseconds - lastAnimUpdate) > 100) { foreach (GameUnit unit in animations) { if (unit.IsAnimating) { unit.CurrentFrame = unit.CurrentFrame + 1; if (unit.CurrentFrame >= (sprites[unit.SpriteName].FrameCount)) { deadAnims.Add(unit); } } } //Time Updating here too - ONLY when game is running if (!gameOver && !gamePaused) { timePassed++; timeParam.SetValue(timePassed / 20f); } lastAnimUpdate = gameTime.TotalGameTime.TotalMilliseconds; } if (!gameOver && !gamePaused) { //Count time alive timeAlive += gameTime.ElapsedGameTime.TotalSeconds; //Motion Input CheckPlayerInput(keyState); //Move all Units where auto-motion is req. MoveGameUnits(); //Check For A Press And Release To Fire...Make Sure Every Release Is atleast 250 Ms apart if (keyState.IsKeyDown(Keys.Space) && !gameShootKeyDown && ((gameTime.TotalGameTime.TotalMilliseconds - lastShotUpdate) > 200)) { gameShootKeyDown = true; } else if (keyState.IsKeyUp(Keys.Space) && gameShootKeyDown) { GameUnit bullet = new GameUnit("bullet", playerOne.Position - new Vector2(0, 32), new Vector2(4f, 4f)); bullet.Velocity = new Vector2(0, -2); units.Add(bullet); //Update last shot time lastShotUpdate = gameTime.TotalGameTime.TotalMilliseconds; //Reset Press variable gameShootKeyDown = false; } //Remove Dead Units foreach (GameUnit e in deadBullets) { units.Remove(e); } foreach (GameUnit e in deadUnits) { units.Remove(e); } foreach (GameUnit e in deadAnims) { units.Remove(e); } //Clear already dead units. deadBullets.Clear(); deadUnits.Clear(); deadAnims.Clear(); } base.Update(gameTime); }