protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); audioEngine = new AudioEngine(@"Content\Audio\GameAudio.xgs"); waveBank = new WaveBank(audioEngine, @"Content\Audio\Wave Bank.xwb"); soundBank = new SoundBank(audioEngine, @"Content\Audio\Sound Bank.xsb"); bgm = Content.Load<Song>("irisu_05"); gameOver_sfx = Content.Load<Song>("irisu_gos1"); bg1_texture = Content.Load<Texture2D>("bg1"); bg2_texture = Content.Load<Texture2D>("bg2"); bg3_texture = Content.Load<Texture2D>("bg3"); asteroid_texture = Content.Load<Texture2D>("asteroid"); player_texture = Content.Load<Texture2D>("Ship"); bullet_texture = Content.Load<Texture2D>("Bullet"); gameOver_texture = Content.Load<Texture2D>("GameOver1"); explosion_texture = Content.Load<Texture2D>("explosion"); player_explode = Content.Load<Texture2D>("player_explosion"); powerup_texture = Content.Load<Texture2D>("powerups"); font1 = Content.Load<SpriteFont>("font1"); player = new Player(player_texture, playerStartPos, Vector2.Zero, playerAcc); powerup = new Powerup(powerup_texture, Vector2.Zero, Vector2.Zero, new Rectangle(0,0,0,0), 0); bg1 = new bg(bg1_texture, new Rectangle(0, 0, bg1_texture.Width, bg1_texture.Height), new Rectangle(0, -bg1_texture.Height, bg1_texture.Width, bg1_texture.Height), 1f, 2); bg2 = new bg(bg2_texture, new Rectangle(0, 0, bg2_texture.Width, bg2_texture.Height), new Rectangle(0, -bg2_texture.Height, bg2_texture.Width, bg2_texture.Height), 0.3f, 3); bg3 = new bg(bg3_texture, new Rectangle(0, 0, bg3_texture.Width, bg3_texture.Height), new Rectangle(0, -bg3_texture.Height, bg3_texture.Width, bg3_texture.Height), 0.5f, 4); asteroids = new List<Asteroid>(); bullets = new List<Bullet>(); rand = new Random(); MediaPlayer.IsRepeating = true; MediaPlayer.Volume = 0.1f; MediaPlayer.Play(bgm); }
protected override void Update(GameTime gameTime) { KeyMouseReader.Update(); if (!gameOverSound && gameOver) { MediaPlayer.Stop(); MediaPlayer.IsRepeating = false; MediaPlayer.Play(gameOver_sfx); gameOverSound = true; } if (gameOver) { playerinfo = "Press enter to restart"; showPlayerInfo = true; if (Keyboard.GetState().IsKeyDown(Keys.Enter)) Initialize(); } if (KeyMouseReader.KeyPressed(Keys.P)) { paused = !paused; } if (Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit(); if (showPlayerInfo) playerinfoTime += (float)gameTime.ElapsedGameTime.TotalSeconds; if (playerinfoTime > 9) { showPlayerInfo = false; playerinfoTime = 0; } audioEngine.Update(); if (gameOver || paused) return; this.Window.Title = "AsteroidKiller Score: " + playerScore.ToString(); player.Update(); bg1.Update(); bg2.Update(); bg3.Update(); //Difficulty diffLevel = playerTime1 / 15 + playerScore/10000; maxAsteroids = 10 + diffLevel * 3; rMinSpeed = 1 + diffLevel / 8; rMaxSpeed = 4 + diffLevel / 4; if (SpawnRate > 1) SpawnRate = 200 - diffLevel * 7; if (SpawnRate < 1) SpawnRate = 1; //Bullet logic foreach (Bullet bullet in bullets) { bullet.Update(); foreach (Asteroid asteroid in asteroids) { asteroid.Collision(bullet.hitbox); if(!piercingBullets) bullet.Collision(asteroid.hitbox); } } if (KeyMouseReader.LeftClickPressed() && nextShot >= 60 && !player.dead) { ShootBullet(); nextShot = 0; } if (nextShot < 60) { nextShot += shotRate; } for (int i = bullets.Count - 1; i >= 0; i--) { if (bullets[i].bulletHitAsteroid) playerScore += 100; if (bullets[i].removeBullet) bullets.RemoveAt(i); } //Asteroid Logic foreach (Asteroid asteroid in asteroids) { asteroid.Update(); player.Collision(asteroid.hitbox); } foreach (Asteroid asteroid in asteroids) { foreach (Asteroid asteroid2 in asteroids) { if (asteroid2 != asteroid && asteroid.scale > 0.7 && asteroid2.scale > 0.7) //Collision with other asteroids { asteroid.Collision(asteroid2.hitbox); asteroid2.Collision(asteroid.hitbox); } } if (asteroid.destroyAsteroid) { asteroid.texture = explosion_texture; } } foreach (Asteroid asteroid in asteroids) { if (asteroid.spawnDaughters) //Logic for spawning "daughter" asteroids { rHitbox = new Rectangle((int)asteroid.pos.X, (int)asteroid.pos.Y, asteroid_texture.Width, asteroid_texture.Height); Vector2 daughterSpeed, daughterSpeed2; daughterSpeed = new Vector2(asteroid.speed.X, rand.Next(-2,2)); daughterSpeed2 = new Vector2(rand.Next(-2,2), asteroid.speed.Y); asteroids.Add(new Asteroid(asteroid_texture, asteroid.pos, daughterSpeed, rHitbox, asteroid.scale / 2)); asteroids.Add(new Asteroid(asteroid_texture, asteroid.pos, daughterSpeed2, rHitbox, asteroid.scale / 2)); asteroid.spawnDaughters = false; break; } } nextSpawn += rand.Next(1, 5); if (nextSpawn >= SpawnRate && asteroids.Count < maxAsteroids) { SpawnRandom(); //Randomize position/speed of asteroid rScale = (11 - rand.Next(1, 4)) * 0.12f; //Randomize size of asteroid rHitbox = new Rectangle((int)rPos.X, (int)rPos.Y, asteroid_texture.Width, asteroid_texture.Height); asteroids.Add(new Asteroid(asteroid_texture, rPos, rSpeed, rHitbox, rScale)); nextSpawn = 0; } for (int i = asteroids.Count - 1; i >= 0; i--) //Goes through list in reverse order to avoid rearrangement in list { if (asteroids[i].destroyAsteroid && asteroids[i].animationTime > 10) { if (asteroidSounds < 3) //Prevents sound overlap from more than 3 explosions on the same frame { soundBank.PlayCue("boom6"); ++asteroidSounds; } asteroids.RemoveAt(i); } } asteroidSounds = 0; //Player Logic if (player.dead) { player.texture = player_explode; if (player.animationTime == 1) soundBank.PlayCue("boom1"); if (player.animationTime >= 100) //Checks to see whether animation has ended { player.pos = playerStartPos; player.speed = Vector2.Zero; player.texture = player_texture; playerLives -= 1; player.invulnerable = true; player.dead = false; } if (player.animationTime >= 100) player.animationTime = 0; } if (playerLives <= 0) gameOver = true; if (player.invulnerable) playerInvulnerableCount += (float)gameTime.ElapsedGameTime.TotalSeconds; if (playerInvulnerableCount >= playerInvulnerableTime) { player.invulnerable = false; playerInvulnerableCount = 0; } playerTime += (float)gameTime.ElapsedGameTime.TotalSeconds; playerTime1 = (int)playerTime; //Powerup logic powerup.Update(); if (!powerupExists) powerupCounter += (float)gameTime.ElapsedGameTime.TotalSeconds; if (powerupCounter >= nextPowerup) { SpawnRandom(); rHitbox = new Rectangle((int)rPos.X, (int)rPos.Y, powerup_texture.Width, powerup_texture.Height); powerup = new Powerup(powerup_texture, rPos, rSpeed*0.5f, rHitbox, rand.Next(1, 6)); powerupExists = true; nextPowerup = rand.Next(10,20); powerupCounter = 0; } powerup.Collision(player.hitbox); if (powerup.playerGotPowerup) { switch (powerup.id) { case 1: playerLives += 1; DisplayPlayerInfo(" Life up!"); soundBank.PlayCue("se_bonus"); break; case 2: shotRate = 14; powerupTime = 0; powerupActive = true; DisplayPlayerInfo(" Fire rate doubled!"); soundBank.PlayCue("se_ch02"); break; case 3: foreach (Asteroid asteroid in asteroids) asteroid.destroyAsteroid = true; DisplayPlayerInfo(" KABOOM"); soundBank.PlayCue("se_slash"); break; case 4: player.invulnerable = true; playerInvulnerableCount = -7; DisplayPlayerInfo(" Invulnerability!"); soundBank.PlayCue("se_chargeup"); break; case 5: piercingBullets = true; shotRate = 5; powerupTime = 0; powerupActive = true; DisplayPlayerInfo(" Asteroids are made of cheese!"); soundBank.PlayCue("se_charge00"); break; } powerup = new Powerup(powerup_texture, Vector2.Zero, Vector2.Zero, new Rectangle(-10, -10, 0, 0), 0); powerupExists = false; } if (powerupActive) powerupTime += (float)gameTime.ElapsedGameTime.TotalSeconds; if (powerupTime >= 10) { shotRate = 7; piercingBullets = false; powerupActive = false; powerupTime = 0; } base.Update(gameTime); }
protected override void Update(GameTime gameTime) { KeyMouseReader.Update(); if (!gameOverSound && gameOver) { MediaPlayer.Stop(); MediaPlayer.IsRepeating = false; MediaPlayer.Play(gameOver_sfx); gameOverSound = true; } if (gameOver) { playerinfo = "Press enter to restart"; showPlayerInfo = true; if (Keyboard.GetState().IsKeyDown(Keys.Enter)) { Initialize(); } } if (KeyMouseReader.KeyPressed(Keys.P)) { paused = !paused; } if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } if (showPlayerInfo) { playerinfoTime += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (playerinfoTime > 9) { showPlayerInfo = false; playerinfoTime = 0; } audioEngine.Update(); if (gameOver || paused) { return; } this.Window.Title = "AsteroidKiller Score: " + playerScore.ToString(); player.Update(); bg1.Update(); bg2.Update(); bg3.Update(); //Difficulty diffLevel = playerTime1 / 15 + playerScore / 10000; maxAsteroids = 10 + diffLevel * 3; rMinSpeed = 1 + diffLevel / 8; rMaxSpeed = 4 + diffLevel / 4; if (SpawnRate > 1) { SpawnRate = 200 - diffLevel * 7; } if (SpawnRate < 1) { SpawnRate = 1; } //Bullet logic foreach (Bullet bullet in bullets) { bullet.Update(); foreach (Asteroid asteroid in asteroids) { asteroid.Collision(bullet.hitbox); if (!piercingBullets) { bullet.Collision(asteroid.hitbox); } } } if (KeyMouseReader.LeftClickPressed() && nextShot >= 60 && !player.dead) { ShootBullet(); nextShot = 0; } if (nextShot < 60) { nextShot += shotRate; } for (int i = bullets.Count - 1; i >= 0; i--) { if (bullets[i].bulletHitAsteroid) { playerScore += 100; } if (bullets[i].removeBullet) { bullets.RemoveAt(i); } } //Asteroid Logic foreach (Asteroid asteroid in asteroids) { asteroid.Update(); player.Collision(asteroid.hitbox); } foreach (Asteroid asteroid in asteroids) { foreach (Asteroid asteroid2 in asteroids) { if (asteroid2 != asteroid && asteroid.scale > 0.7 && asteroid2.scale > 0.7) //Collision with other asteroids { asteroid.Collision(asteroid2.hitbox); asteroid2.Collision(asteroid.hitbox); } } if (asteroid.destroyAsteroid) { asteroid.texture = explosion_texture; } } foreach (Asteroid asteroid in asteroids) { if (asteroid.spawnDaughters) //Logic for spawning "daughter" asteroids { rHitbox = new Rectangle((int)asteroid.pos.X, (int)asteroid.pos.Y, asteroid_texture.Width, asteroid_texture.Height); Vector2 daughterSpeed, daughterSpeed2; daughterSpeed = new Vector2(asteroid.speed.X, rand.Next(-2, 2)); daughterSpeed2 = new Vector2(rand.Next(-2, 2), asteroid.speed.Y); asteroids.Add(new Asteroid(asteroid_texture, asteroid.pos, daughterSpeed, rHitbox, asteroid.scale / 2)); asteroids.Add(new Asteroid(asteroid_texture, asteroid.pos, daughterSpeed2, rHitbox, asteroid.scale / 2)); asteroid.spawnDaughters = false; break; } } nextSpawn += rand.Next(1, 5); if (nextSpawn >= SpawnRate && asteroids.Count < maxAsteroids) { SpawnRandom(); //Randomize position/speed of asteroid rScale = (11 - rand.Next(1, 4)) * 0.12f; //Randomize size of asteroid rHitbox = new Rectangle((int)rPos.X, (int)rPos.Y, asteroid_texture.Width, asteroid_texture.Height); asteroids.Add(new Asteroid(asteroid_texture, rPos, rSpeed, rHitbox, rScale)); nextSpawn = 0; } for (int i = asteroids.Count - 1; i >= 0; i--) //Goes through list in reverse order to avoid rearrangement in list { if (asteroids[i].destroyAsteroid && asteroids[i].animationTime > 10) { if (asteroidSounds < 3) //Prevents sound overlap from more than 3 explosions on the same frame { soundBank.PlayCue("boom6"); ++asteroidSounds; } asteroids.RemoveAt(i); } } asteroidSounds = 0; //Player Logic if (player.dead) { player.texture = player_explode; if (player.animationTime == 1) { soundBank.PlayCue("boom1"); } if (player.animationTime >= 100) //Checks to see whether animation has ended { player.pos = playerStartPos; player.speed = Vector2.Zero; player.texture = player_texture; playerLives -= 1; player.invulnerable = true; player.dead = false; } if (player.animationTime >= 100) { player.animationTime = 0; } } if (playerLives <= 0) { gameOver = true; } if (player.invulnerable) { playerInvulnerableCount += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (playerInvulnerableCount >= playerInvulnerableTime) { player.invulnerable = false; playerInvulnerableCount = 0; } playerTime += (float)gameTime.ElapsedGameTime.TotalSeconds; playerTime1 = (int)playerTime; //Powerup logic powerup.Update(); if (!powerupExists) { powerupCounter += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (powerupCounter >= nextPowerup) { SpawnRandom(); rHitbox = new Rectangle((int)rPos.X, (int)rPos.Y, powerup_texture.Width, powerup_texture.Height); powerup = new Powerup(powerup_texture, rPos, rSpeed * 0.5f, rHitbox, rand.Next(1, 6)); powerupExists = true; nextPowerup = rand.Next(10, 20); powerupCounter = 0; } powerup.Collision(player.hitbox); if (powerup.playerGotPowerup) { switch (powerup.id) { case 1: playerLives += 1; DisplayPlayerInfo(" Life up!"); soundBank.PlayCue("se_bonus"); break; case 2: shotRate = 14; powerupTime = 0; powerupActive = true; DisplayPlayerInfo(" Fire rate doubled!"); soundBank.PlayCue("se_ch02"); break; case 3: foreach (Asteroid asteroid in asteroids) { asteroid.destroyAsteroid = true; } DisplayPlayerInfo(" KABOOM"); soundBank.PlayCue("se_slash"); break; case 4: player.invulnerable = true; playerInvulnerableCount = -7; DisplayPlayerInfo(" Invulnerability!"); soundBank.PlayCue("se_chargeup"); break; case 5: piercingBullets = true; shotRate = 5; powerupTime = 0; powerupActive = true; DisplayPlayerInfo(" Asteroids are made of cheese!"); soundBank.PlayCue("se_charge00"); break; } powerup = new Powerup(powerup_texture, Vector2.Zero, Vector2.Zero, new Rectangle(-10, -10, 0, 0), 0); powerupExists = false; } if (powerupActive) { powerupTime += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (powerupTime >= 10) { shotRate = 7; piercingBullets = false; powerupActive = false; powerupTime = 0; } base.Update(gameTime); }