public override void Update(GameTime gameTime) { KeyboardState state = Keyboard.GetState(); if (state.IsKeyDown(Keys.Up)) { Accelerate(); } if (state.IsKeyDown(Keys.Left)) { Rotation -= 0.05f; } if (state.IsKeyDown(Keys.Down)) { Break(); } else if (state.IsKeyDown(Keys.Right)) { Rotation += 0.05f; } if (state.IsKeyDown(Keys.Space)) { Shot s = Shoot(); if (s != null) { //laserSound.Play(); //shotList.Add(s); } } Position += Speed; if (reloadTimer > 0) { reloadTimer--; } if (Position.X < Globals.GameArea.Left) { Position = new Vector2(Globals.GameArea.Right, Position.Y); } if (Position.X > Globals.GameArea.Right) { Position = new Vector2(Globals.GameArea.Left, Position.Y); } if (Position.Y < Globals.GameArea.Top) { Position = new Vector2(Position.X, Globals.GameArea.Bottom); } if (Position.Y > Globals.GameArea.Bottom) { Position = new Vector2(Position.X, Globals.GameArea.Top); } 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } KeyboardState state = Keyboard.GetState(); if (state.IsKeyDown(Keys.Up)) { player.Accelerate(); } if (state.IsKeyDown(Keys.Left)) { player.Rotation -= 0.05f; } if (state.IsKeyDown(Keys.Down)) { player.Break(); } else if (state.IsKeyDown(Keys.Right)) { player.Rotation += 0.05f; } if (state.IsKeyDown(Keys.Space)) { Shot s = player.Shoot(); if (s != null) { laserSound.Play(); shotList.Add(s); } } foreach (Shot shot in shotList) { shot.Update(gameTime); Meteor meteor = meteorList.FirstOrDefault(m => m.CollidesWith(shot)); if (meteor != null) { meteorList.Remove(meteor); meteorList.AddRange(Meteor.BreakMeteor(meteor)); explosionList.Add(new Explosion() { Position = meteor.Position, Scale = meteor.ExplosionScale }); shot.IsDead = true; explosionSound.Play(); } } foreach (Explosion explosion in explosionList) { explosion.Update(gameTime); } foreach (Meteor meteor in meteorList) { meteor.Update(gameTime); } shotList.RemoveAll(s => s.IsDead || !Globals.GameArea.Contains(s.Position)); explosionList.RemoveAll(e => e.IsDead); player.Update(gameTime); prevousKbState = state; // TODO: Add your update logic here base.Update(gameTime); }