public void Update(Sprite playerSprite, GameTime gameTime) { if (IsAlive) { if (alienSprite.position.X < playerSprite.position.X) { alienSprite.position.X++; } else if (alienSprite.position.X > playerSprite.position.X) { alienSprite.position.X--; } if (alienSprite.position.Y < playerSprite.position.Y) { alienSprite.position.Y++; } else if (alienSprite.position.Y > playerSprite.position.Y) { alienSprite.position.Y--; } alienSprite.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) { if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } keyboard.Update(Keyboard.GetState(), graphics, player, bullets); player.Update(gameTime); alien.Update(player, gameTime); bullets.Update(gameTime); int previousWave = wave; wave = asteroids.Update(wave, gameTime); if (wave != previousWave) { counter = 100; alien.alienSprite.position = Vector2.Zero; alien.LiveAlien(); } lives = collision.Update(graphics, lives); base.Update(gameTime); }
public static void Update(GameTime gameTime) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; KeyboardState keyState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); GamePadState padState = GamePad.GetState(PlayerIndex.One); switch (AsteroidsGame.ControlStyle) { case ControlStyles.KBM_RH: { shipSprite.Velocity = Vector2.Zero; // Movement if (keyState.IsKeyDown(Keys.W)) { shipSprite.Velocity += new Vector2(0, -1); } if (keyState.IsKeyDown(Keys.S)) { shipSprite.Velocity += new Vector2(0, 1); } if (keyState.IsKeyDown(Keys.A)) { shipSprite.Velocity += new Vector2(-1, 0); } if (keyState.IsKeyDown(Keys.D)) { shipSprite.Velocity += new Vector2(1, 0); } shipSprite.Velocity.Normalize(); shipSprite.Velocity *= playerSpeed; fireShotsMouse(mouseState, elapsed); pointToMouseCursor(mouseState); } break; case ControlStyles.KBM_LH: { shipSprite.Velocity = Vector2.Zero; // Movement if (keyState.IsKeyDown(Keys.O)) { shipSprite.Velocity += new Vector2(0, -1); } if (keyState.IsKeyDown(Keys.L)) { shipSprite.Velocity += new Vector2(0, 1); } if (keyState.IsKeyDown(Keys.K)) { shipSprite.Velocity += new Vector2(-1, 0); } if (keyState.IsKeyDown(Keys.OemSemicolon)) { shipSprite.Velocity += new Vector2(1, 0); } shipSprite.Velocity.Normalize(); shipSprite.Velocity *= playerSpeed; fireShotsMouse(mouseState, elapsed); pointToMouseCursor(mouseState); } break; case ControlStyles.GamePad: { shipSprite.Velocity = Vector2.Zero; shipSprite.Velocity = new Vector2( padState.ThumbSticks.Left.X, -padState.ThumbSticks.Left.Y); shipSprite.Velocity.Normalize(); shipSprite.Velocity *= playerSpeed; fireShotsGamePad(padState, elapsed); shipSprite.RotateTo(new Vector2( padState.ThumbSticks.Right.X, padState.ThumbSticks.Right.Y) - shipSprite.Position); } break; } imposeMovementLimits(); shipSprite.Update(gameTime); shotManager.Update(gameTime); }