protected override void Initialize() { //create a rectangle that is the same size as the screen viewPortRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); //initialize asteroids Asteroid.Initialize(graphics); //initialize player debris PlayerDebris.Initialize(graphics); base.Initialize(); }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (gameState == "menu") //update this part only if the game is at the menu { //if enter key is pressed, start the game KeyboardState kState = Keyboard.GetState(); if (kState.IsKeyDown(Keys.Enter)) { gameState = "game"; } } if (gameState == "gameover" || gameState == "gamecomplete") //update this part ony if the game os over or complete { //if enter key is pressed, start the game KeyboardState kState = Keyboard.GetState(); if (kState.IsKeyDown(Keys.Enter)) { gameState = "game"; Player.life = 3; //reset number of player lifes //delete all asteroids remain from previous game foreach (Asteroid a in Asteroid.largeAsteroids) { a.Destroy = true; } foreach (Asteroid a in Asteroid.mediumAsteroids) { a.Destroy = true; } foreach (Asteroid a in Asteroid.smallAsteroids) { a.Destroy = true; } //create new asteroids for the new game CreateLargeAsteroids(); } } if (gameState == "game" || gameState == "gameover") //update this part is the game is running or game over { /****************** * PLAYER DEBRIS *******************/ PlayerDebris.Update(gameTime); } if (gameState == "game") //update all this if the game is running { /****************** * PLAYER *******************/ //update player player.Update(gameTime); //check collision between the player and asteroids, only of player is not destroyed if (player.Destroyed == false) { if (player.CanDestroy == true) //if player can be destroyed { foreach (Asteroid a in Asteroid.largeAsteroids) { if (Vector2.Distance(a.Position, player.Position) < a.Radius) { player.Destroyed = true; //create player parts in position of the player for (var i = 0; i < spritePlayerDebris.Length; i++) { PlayerDebris.debris.Add(new PlayerDebris(player.Position, spritePlayerDebris[i], player.Angle)); ExplosionSound.Play(); } player.Position = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height / 2); Player.life--; player.CanDestroy = false; //if player life = 0 then set game to game over if (Player.life == 0) { SetGameOver(gameTime); } } } foreach (Asteroid a in Asteroid.mediumAsteroids) { if (Vector2.Distance(a.Position, player.Position) < a.Radius) { player.Destroyed = true; //create player parts in position of the player for (var i = 0; i < spritePlayerDebris.Length; i++) { PlayerDebris.debris.Add(new PlayerDebris(player.Position, spritePlayerDebris[i], player.Angle)); ExplosionSound.Play(); } player.Position = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height / 2); Player.life--; player.CanDestroy = false; //if player life = 0 then set game to game over if (Player.life == 0) { SetGameOver(gameTime); } } } foreach (Asteroid a in Asteroid.smallAsteroids) { if (Vector2.Distance(a.Position, player.Position) < a.Radius) { player.Destroyed = true; //create player parts in position of the player for (var i = 0; i < spritePlayerDebris.Length; i++) { PlayerDebris.debris.Add(new PlayerDebris(player.Position, spritePlayerDebris[i], player.Angle)); ExplosionSound.Play(); } player.Position = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height / 2); Player.life--; player.CanDestroy = false; //if player life = 0 then set game to game over if (Player.life == 0) { SetGameOver(gameTime); } } } } } else //if player is destroyed, check if player debris is present, if there is no more debris, set destroy to false { if (PlayerDebris.debris.Count == 0) { player.Destroyed = false; } } /*************** * BULLET ****************/ //update bullet foreach (Bullet b in Bullet.bullets) { b.Update(gameTime, viewPortRect); } //if bullet is in collision with asteroid, and the bullet is not set to be destroyed, play explosion sound and set bullet and asteroid to destroy and create smaller asteroids in position foreach (Bullet b in Bullet.bullets) { // check collision with large asteroid foreach (Asteroid a in Asteroid.largeAsteroids) { if (Vector2.Distance(b.Position, a.Position) < a.Radius && b.Destroy == false) { b.Destroy = true; a.Destroy = true; var randomPitch = (rnd.NextDouble() * 1.8) - 1; ExplosionSound.Play(1, (float)randomPitch, 1); //create 2 medium asteroids in position of the large asteroid for (var i = 0; i < 2; i++) { Asteroid.mediumAsteroids.Add(new Asteroid(a.Position, spriteMediumAsteroid[rnd.Next(0, 2)])); } //create some debris CreateDebris(20, a.Position); } } // check collision with medium asteroid foreach (Asteroid a in Asteroid.mediumAsteroids) { if (Vector2.Distance(b.Position, a.Position) < a.Radius && b.Destroy == false) { b.Destroy = true; a.Destroy = true; var randomPitch = (rnd.NextDouble() * 1.8) - 1; ExplosionSound.Play(1, (float)randomPitch, 1); //create 3 small asteroids in position of the medium asteroid for (var i = 0; i < 3; i++) { Asteroid.smallAsteroids.Add(new Asteroid(a.Position, spriteSmallAsteroid[rnd.Next(0, 2)])); } //create some debris CreateDebris(10, a.Position); } } // check collision with small asteroid foreach (Asteroid a in Asteroid.smallAsteroids) { if (Vector2.Distance(b.Position, a.Position) < a.Radius && b.Destroy == false) { b.Destroy = true; a.Destroy = true; var randomPitch = (rnd.NextDouble() * 1.8) - 1; ExplosionSound.Play(1, (float)randomPitch, 1); //create some debris CreateDebris(5, a.Position); } } } //remove bullets marked to be destroyed Bullet.bullets.RemoveAll(b => b.Destroy == true); //remove asteroids marked to be destroyed Asteroid.largeAsteroids.RemoveAll(a => a.Destroy == true); Asteroid.mediumAsteroids.RemoveAll(a => a.Destroy == true); Asteroid.smallAsteroids.RemoveAll(a => a.Destroy == true); //delete any particles if faded out Particle.particles.RemoveAll(p => p.Color.A == 0); /************** * PARTICLES ***************/ //update particles (fade all particles out the same way) foreach (Particle p in Particle.particles) { p.Update(gameTime); } //update debris particles (move only debris particles in to random angle slowly) foreach (DebrisParticle d in Particle.particles) { d.Update(); } } //end of if statement check if the game is running /****************** * ASTEROID *******************/ //update asteroids foreach (Asteroid a in Asteroid.largeAsteroids) { a.Update(gameTime); } foreach (Asteroid a in Asteroid.mediumAsteroids) { a.Update(gameTime); } foreach (Asteroid a in Asteroid.smallAsteroids) { a.Update(gameTime); } //if all asterods destroyed, set game to complete if (Asteroid.largeAsteroids.Count == 0) { if (Asteroid.mediumAsteroids.Count == 0) { if (Asteroid.smallAsteroids.Count == 0) { gameState = "gamecomplete"; } } } base.Update(gameTime); }