/// <summary> /// Update game /// </summary> public void Update(double deltaT) { if (currentState == GameState.Intro) { //Play "IntroMusic" sound Sounds[17].settings.volume = 200; Sounds[17].controls.play(); if (Intro.timeIntro.ElapsedMilliseconds > 2000.0) { if (Intro.timeIntro.ElapsedMilliseconds > 4400.0) { intro.Move(3000, deltaT); } if (Intro.timeIntro.ElapsedMilliseconds > 9200.0) { currentState = GameState.Menu; Intro.timeIntro.Reset(); } } } else if (currentState == GameState.Menu) { mainMenu.Blink(); if (KeyDownPressed && mainMenu.currentSelection < MainMenu.DifficultyState.Hard) { mainMenu.currentSelection++; KeyDownPressed = false; //Play "Down" sound Sounds[7].settings.volume = 100; Sounds[7].controls.play(); } if (KeyUpPressed && mainMenu.currentSelection > MainMenu.DifficultyState.Easy) { mainMenu.currentSelection--; KeyUpPressed = false; //Play "Up" sound Sounds[8].settings.volume = 100; Sounds[8].controls.play(); } if (KeyEnterPressed) { mainMenu.LevelSelection(); if (mainMenu.currentSelection == MainMenu.DifficultyState.Easy) { //Play "Easy" sound Sounds[9].settings.volume = 100; Sounds[9].controls.play(); } if (mainMenu.currentSelection == MainMenu.DifficultyState.Medium) { //Play "Medium" sound Sounds[10].settings.volume = 100; Sounds[10].controls.play(); } if (mainMenu.currentSelection == MainMenu.DifficultyState.Hard) { //Play "Hard" sound Sounds[11].settings.volume = 100; Sounds[11].controls.play(); } currentState = GameState.Play; } } else if (currentState == GameState.Play) { Sounds[17].controls.stop(); KeyEnterPressed = false; //Play the level music if (mainMenu.currentSelection == MainMenu.DifficultyState.Easy) { Sounds[12].settings.volume = 100; Sounds[12].controls.play(); } else if (mainMenu.currentSelection == MainMenu.DifficultyState.Medium) { Sounds[13].settings.volume = 100; Sounds[13].controls.play(); } else if (mainMenu.currentSelection == MainMenu.DifficultyState.Hard) { Sounds[14].settings.volume = 100; Sounds[14].controls.play(); } //move left if (KeyLeftPressed) { if (playerShip != null) { if (playerShip.Position.x <= 0) { playerShip.Position.x = 0; } else { playerShip.MoveLeft(deltaT, playerSpeed); } } } //move right if (KeyRightPressed) { if (playerShip != null) { if (playerShip.Position.x >= game.gameSize.Width - playerShip.imageWidth) { playerShip.Position.x = game.gameSize.Width - playerShip.imageWidth; } else { playerShip.MoveRight(deltaT, playerSpeed); } } } //Draw missile if (KeySpacePressed) { if (playerMissile == null) { playerMissile = new Missile(new Vecteur2D(200, 200), new Vecteur2D(0, 0), 1); playerMissile.Position.x = playerShip.Position.x + playerShip.imageWidth / 2 - 1; playerMissile.Position.y = playerShip.Position.y - playerMissile.imageHeight; //Play "pew" sound Sounds[0].settings.volume = 100; Sounds[0].controls.play(); } } //Move Player missile if (playerMissile != null) { playerMissile.Move(deltaT, playerMissile.Vitesse.y); } //Test Player missile collision if (playerMissile != null) { foreach (Bunker b in bunkers) { b.Collision(playerMissile); } if (enemyBlock.Collision(playerMissile)) { //Play "Boom" sound Sounds[2].settings.volume = 100; Sounds[2].controls.play(); } // maybe dead if (!playerMissile.Alive) { playerMissile = null; } } //Pause the game if (KeyPPressed) { if (currentState == GameState.Play) { Sounds[12].settings.volume = 20; Sounds[13].settings.volume = 20; Sounds[14].settings.volume = 20; currentState = GameState.Pause; KeyPPressed = false; } } //Random enemy shoots if (enemyBlock != null) { foreach (SpaceShip s in enemyBlock.Ships) { enemyBlock.RandomShoot(s, deltaT); } } //Move Enemy Block and enemy's missiles if (enemyBlock != null) { enemyBlock.Move(deltaT); List <Missile> index = new List <Missile>(); List <Bunker> indexBunker = new List <Bunker>();; foreach (Missile m in missiles) { m.Move(deltaT, m.Vitesse.y); foreach (Bunker b in bunkers) { if (b.Collision(m)) { index.Add(m); } } if (playerShip.Collision(m)) { playerShip.Position = new Vecteur2D(gameSize.Width / 2 - space_invaders.Properties.Resources.ship1.Width / 2, gameSize.Height - space_invaders.Properties.Resources.ship1.Height); index.Add(m); //Play "Touched" sound Sounds[6].settings.volume = 100; Sounds[6].controls.play(); } } if (index.Count != 0) { foreach (Missile m in index) { missiles.Remove(m); } } foreach (Bunker b in bunkers) { if (enemyBlock.Collision(b)) { indexBunker.Add(b); } } if (indexBunker.Count != 0) { foreach (Bunker b in indexBunker) { bunkers.Remove(b); } } } //Move Bonus if (bonus.Count != 0) { foreach (Bonus b in bonus) { b.Move(deltaT); } int index = -1; foreach (Bonus b in bonus) { if (b.Collision(playerShip)) { playerBonus = b; index = bonus.IndexOf(b); //Play "Suck" sound Sounds[3].settings.volume = 100; Sounds[3].controls.play(); } else if (b.Position.y > gameSize.Height) { index = bonus.IndexOf(b); } } if (index != -1) { bonus.RemoveAt(index); } } //Use Bonus if (KeyBPressed) { if (playerBonus != null) { if (playerMissile == null) { playerBonus.action(); if (playerBonus.bonusName == "DoublePoints") { //Play "DoublePoints" sound Sounds[1].settings.volume = 100; Sounds[1].controls.play(); bonus3Activated = true; } if (playerBonus.bonusName == "InstantKill") { //Play "Bop" sound Sounds[5].settings.volume = 100; Sounds[5].controls.play(); } if (playerBonus.bonusName == "BigMissile") { //Play "BigPew" sound Sounds[4].settings.volume = 100; Sounds[4].controls.play(); } playerBonus = null; KeyBPressed = false; } } } //Increment bonus3's time if (bonus3Activated) { timeBonus.Start(); if (timeBonus.ElapsedMilliseconds > 5000.0) { bonus3Activated = false; timeBonus.Reset(); playerScore.BonusFactor = 1; } } //Win the game if (!enemyBlock.Alive) { currentState = GameState.Win; } //Lost the game if (!playerShip.Alive || (enemyBlock.Position.y + enemyBlock.Size.Height) >= playerShip.Position.y) { currentState = GameState.Lost; } //Check player's lives if (!playerShip.Alive) { playerShip = null; } } else if (currentState == GameState.Pause) { //Unpause game if (KeyPPressed) { currentState = GameState.Play; KeyPPressed = false; } } else if (currentState == GameState.Win) { //Stop game music Sounds[12].controls.stop(); Sounds[13].controls.stop(); Sounds[14].controls.stop(); //Play "WinMusic" sound /*if (timeWinMusic.ElapsedMilliseconds < 9000.0) * { * Sounds[15].settings.volume = 100; * Sounds[15].controls.play(); * } * * timeWinMusic.Start(); * * if (timeWinMusic.ElapsedMilliseconds > 9500.0) * { * Sounds[15].settings.volume = 0; * Sounds[15].controls.stop(); * }*/ if (!winMusicPlayed) { Sounds[15].settings.volume = 100; Sounds[15].controls.play(); winMusicPlayed = true; } if ((winScreen.Position.y + winScreen.imageHeight / 2) < gameSize.Height / 2) { winScreen.Move(deltaT); } // Choose after game end winScreen.Blink(); if (KeyDownPressed && winScreen.currentSelection < Win.AfterState.Quit) { winScreen.currentSelection++; KeyDownPressed = false; //Play "Down" sound Sounds[7].settings.volume = 100; Sounds[7].controls.play(); } if (KeyUpPressed && winScreen.currentSelection > Win.AfterState.MainMenu) { winScreen.currentSelection--; KeyUpPressed = false; //Play "Up" sound Sounds[8].settings.volume = 100; Sounds[8].controls.play(); } if (KeyEnterPressed) { winScreen.AfterSelection(); KeyEnterPressed = false; } } else if (currentState == GameState.Lost) { //Stop game music Sounds[12].controls.stop(); Sounds[13].controls.stop(); Sounds[14].controls.stop(); //Play "LostMusic" sound /*if (timeLostMusic.ElapsedMilliseconds < 2650.0) * { * Sounds[16].settings.volume = 100; * Sounds[16].controls.play(); * } * * timeLostMusic.Start(); * * if (timeLostMusic.ElapsedMilliseconds > 2650.0) * { * Sounds[16].settings.volume = 0; * Sounds[16].controls.stop(); * }*/ if (!lostMusicPlayed) { Sounds[16].settings.volume = 100; Sounds[16].controls.play(); lostMusicPlayed = true; } if ((lostScreen.Position.y + lostScreen.imageHeight / 2) < gameSize.Height / 2) { lostScreen.Move(deltaT); } // Choose after game end lostScreen.Blink(); if (KeyDownPressed && lostScreen.currentSelection < Lost.AfterState.Quit) { lostScreen.currentSelection++; KeyDownPressed = false; //Play "Down" sound Sounds[7].settings.volume = 100; Sounds[7].controls.play(); } if (KeyUpPressed && lostScreen.currentSelection > Lost.AfterState.MainMenu) { lostScreen.currentSelection--; KeyUpPressed = false; //Play "Up" sound Sounds[8].settings.volume = 100; Sounds[8].controls.play(); } if (KeyEnterPressed) { lostScreen.AfterSelection(); KeyEnterPressed = false; } } }
/// <summary> /// Allows the game to perform any initialization it needs /// to before starting to run. This is where it can query for /// any required services and load any non-graphic related content. /// Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { Texture2D motherShipBombsTexture = this.Content.Load<Texture2D> ("motherShipBombs"); Texture2D alienBombsTexture = this.Content.Load<Texture2D>("laser2"); Texture2D bonusImage = this.Content.Load<Texture2D>("bonus1"); playerLaser = new LaserFactory(this); alienBomb = new BombFactory(this, alienBombsTexture, 1, 3); shipBomb = new BombFactory(this, motherShipBombsTexture, 2, 3); bonusBomb = new BombFactory(this, bonusImage, -1, 2); player = new PlayerSprite(this, playerLaser); bonus = new Bonus(this, bonusBomb); aliens = new AlienSquad(this, alienBomb, playerLaser); score = new ScoreSprite(this, playerLaser, alienBomb, shipBomb, bonusBomb); ship = new MotherShipSprite(this, shipBomb, playerLaser); Components.Add(bonus); Components.Add(player); Components.Add(aliens); Components.Add(playerLaser); Components.Add(alienBomb); Components.Add(score); Components.Add(ship); Components.Add(shipBomb); Components.Add(bonusBomb); playerLaser.AddOpponent(aliens); playerLaser.AddOpponent(ship); alienBomb.AddOpponent(player); shipBomb.AddOpponent(player); bonusBomb.AddOpponent(player); ScoreSprite.GameOver += onGameOver; AlienSquad.GameOver += onGameOver; AlienSquad.NewWave += onNewWave; base.Initialize(); }