public void ResetME() { while (meteors.Count < 10) { var angle = random.Next() * MathHelper.TwoPi; var m = new Meteor() { positin = new Vector2(GlobalVar.GPlayground.Left + (float)random.NextDouble() * GlobalVar.GPlayground.Width, GlobalVar.GPlayground.Top + (float)random.NextDouble() * GlobalVar.GPlayground.Height), Rotation = angle, speed = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)) * random.Next(20, 60) / 30.0f }; if (!GlobalVar.RSpawnArea.Contains(m.positin)) { meteors.Add(m); } } }
/// <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) { float f = 1f; KeyboardState ks = Keyboard.GetState(); if (ks.IsKeyDown(Keys.W)) { f = f++; foreach (Meteor m in meteors) { m.speed = m.speed + m.speed * f / 10; } } if (ks.IsKeyDown(Keys.Escape)) { using (var game = new Game1()) { game.Run(); } } if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } KeyboardState state = Keyboard.GetState(); if (state.IsKeyDown(Keys.Up)) { Spaceship.Accelerate(); } //Change the Direction Accordong to the is pressed if (state.IsKeyDown(Keys.Left)) { Spaceship.Rotation -= 0.05f; } else if (state.IsKeyDown(Keys.Right)) { Spaceship.Rotation += 0.05f; } if (state.IsKeyDown(Keys.Space)) { //Count the number of shots shooed. //Add a new shot class when space is Pressed Shot s = Spaceship.Shoot(); //Checks the position of shots and draw to pretend overpositions if (s != null) { laserS.Play(); shots.Add(s); } } //Update a time of shots so the shot dont overridden foreach (Shot sh in shots) { sh.Update(gameTime); Meteor m = meteors.FirstOrDefault(x => x.collideWith(sh)); if (m != null) { meteors.Remove(m); Lstexplosions.Add(new Explosion() { positin = m.positin }); sh.isDead = true; NumberOfMeteorKilled++; meteorS.Play(); } } foreach (Explosion explosion in Lstexplosions) { explosion.Update(gameTime); } foreach (Meteor meteor in meteors) { meteor.Update(gameTime); } shots.RemoveAll(s => s.isDead || !GlobalVar.GPlayground.Contains(s.positin)); Lstexplosions.RemoveAll(e => e.isDead); Spaceship.Update(gameTime); KeyboardState = state; base.Update(gameTime); }