/// <summary> /// Private constructor /// </summary> /// <param name="gameSize">Size of the game area</param> private Game(Size gameSize) { Bitmap imageShip = new Bitmap("../../Resources/Ship4.png"); playerShip = new SpaceShip(new Vecteur2D(gameSize.Width / 2-30, gameSize.Height-35), 5, imageShip); bunkers = new List<Bunker>(); for(int i=0;i<3;i++) { bunker = new Bunker(new Vecteur2D(i * 250, gameSize.Height - 150)); bunkers.Add(bunker); } this.gameSize = gameSize; }
/// <summary> /// Makes an enemy fire a missile /// </summary> /// <param name="ship">Enemy ship</param> /// <param name="deltaT">Time ellapsed since last move.</param> public void RandomShoot(SpaceShip ship, double deltaT) { if (Game.prob.NextDouble() < deltaT * ShootProbability) { Missile shipMissile = new Missile(new Vecteur2D(200, -200), new Vecteur2D(0, 0), 1); shipMissile.Position.x = ship.Position.x + ship.imageWidth / 2 - 1; shipMissile.Position.y = ship.Position.y + ship.imageHeight + shipMissile.imageHeight; Game.game.missiles.Add(shipMissile); } }
/// <summary> /// Method to create a new game /// Used when the program is started or if the player /// want do one more party /// </summary> /// <param name="gameSize"></param> private void InitGame(Size gameSize) { state = GameState.Play; this.gameSize = gameSize; Bitmap image = Properties.Resources.ship3; int centerX = (gameSize.Width / 2) - (image.Width / 2); int downY = (gameSize.Height - 10) - (image.Height); CreateBunkers(); CreateEnemies(); playerShip = new PlayerSpaceShip(new Vecteur2d(centerX, downY), 3, image, Side.Ally); pendingNewGameObjects.Add(playerShip); }
/// <summary> /// Private constructor /// </summary> /// <param name="gameSize">Size of the game area</param> private Game(Size gameSize) { this.gameSize = gameSize; this.state = GameState.Play; for (int i = 1; i <= 3; i++) { AddNewGameObject(new Bunker(i * 135, gameSize.Height - 130)); } this.playerShip = new PlayerSpaceship(this.gameSize.Width / 2, this.gameSize.Height, 3); this.AddNewGameObject(this.playerShip); }
/// <summary> /// Update bloc d'ennemi /// </summary> /// <param name="gameInstance">>Instance du jeu</param> /// <param name="deltaT">Nombre de mili sconde écoulées depuis le dernier update</param> public override void Update(Game gameInstance, double deltaT) { double r = rand.NextDouble(); int randomShip = rand.Next(0, enemyShips.Count); if (!leftDirection) { Position.X += enemyBlockSpeed * deltaT; } if (leftDirection) { Position.X -= enemyBlockSpeed * deltaT; } if (Size.Width + Position.X >= gameInstance.gameSize.Width) { ChangeMovement(false, ConstantsDeJeu.NewRandomShootProbabilityForEnemyBloc, ConstantsDeJeu.YNewEnemyBlocPosition, ConstantsDeJeu.NewEnemyBlocSpeed); } if (Position.X <= 0) { ChangeMovement(true, ConstantsDeJeu.NewRandomShootProbabilityForEnemyBloc, ConstantsDeJeu.YNewEnemyBlocPosition, ConstantsDeJeu.NewEnemyBlocSpeed); } SpaceShip randomSpaceShip = enemyShips.ElementAt(randomShip); foreach (SpaceShip spaceShip in enemyShips) { spaceShip.Update(gameInstance, deltaT); // Test de fin de jeu if (spaceShip.Position.Y + spaceShip.Image.Height >= gameInstance.PlayerShip.Position.Y) { gameInstance.PlayerShip.Lives = 0; } } //Tirs aléatoire if (r <= randomShootProbability * deltaT) { randomSpaceShip.Shoot(gameInstance); } UpdateSize(); }
private void spaceShip_Disposed(object sender, EventArgs e) { SpaceShip ship = sender as SpaceShip; if (Components != null) { m_SpaceShips.Remove(ship); this.Components.Remove(ship); } if (m_SpaceShips.Count == 0) { if (!m_GameOver) { gameOver(); } } }
/// <summary> /// Initialiser les composants du jeu /// </summary> /// <param name="gameSize">La taille du jeu</param> public void InitGame(Size gameSize) { this.gameSize = gameSize; for (int i = 1; i <= 3; i++) { AddNewGameObject(new Bunker(i * 135, gameSize.Height - 130, Side.Neutral)); } PlayerShip = new PlayerSpaceship(this.gameSize.Width / 2, this.gameSize.Height, 50, Side.Ally); this.AddNewGameObject(this.PlayerShip); this.enemies = new EnemyBlock(50, 50, 400, Side.Enemy); enemies.AddLine(6, 5, SpaceInvaders.Properties.Resources.ship7); enemies.AddLine(6, 10, SpaceInvaders.Properties.Resources.ship2); enemies.AddLine(6, 10, SpaceInvaders.Properties.Resources.ship5); this.AddNewGameObject(this.enemies); state = GameState.Play; }
/// <summary> /// Ajoute une nouvelle ligne d’ennemis au bloc d'ennemi /// </summary> /// <param name="nbShips">Nombre de vaisseau ennemi</param> /// <param name="nbLives">Nombre de points de vie de vie de chaque vaisseau</param> /// <param name="shipImage">L'image des vaisseaux</param> public void AddLine(int nbShips, int nbLives, Bitmap shipImage) { float posY; shipHorizonalSpace = baseWidth / nbShips; posY = (float)Position.Y + blockHeight + shipVerticlaSapce; if (lineNumber == 1) { posY = (float)Position.Y + blockHeight; } for (int i = 0; i < nbShips; i++) { SpaceShip spaceShip = new SpaceShip((float)(Position.X + (i * shipHorizonalSpace)), posY, nbLives, Side.Enemy) { Image = shipImage, }; enemyShips.Add(spaceShip); } blockHeight += shipImage.Height + shipVerticlaSapce; lineNumber++; UpdateSize(); }
/// <summary> /// Determines Collision between the player's ship and the bonus /// </summary> /// <param name="s">player ship</param> public bool Collision(SpaceShip s) { if (s.Position.x > Position.x + imageWidth) { return(false); } else if (s.Position.y > Position.y + imageHeight) { return(false); } else if (Position.x > s.Position.x + s.imageWidth) { return(false); } else if (Position.y > s.Position.y + s.imageHeight) { return(false); } else { Vecteur2D[] vs = new Vecteur2D[s.imageWidth * s.imageHeight]; Vecteur2D[] vb = new Vecteur2D[imageWidth * imageHeight]; Color currentcolor; for (int i = 0; i < s.imageWidth; i++) { for (int j = 0; j < s.imageHeight; j++) { currentcolor = s.image.GetPixel(i, j); if (currentcolor == Color.FromArgb(255, 0, 0, 0)) { vs[(i * s.imageHeight) + j] = new Vecteur2D((i + s.Position.x) - Position.x, (j + s.Position.y) - Position.y); } } } for (int i = 0; i < image.Width; i++) { for (int j = 0; j < image.Height; j++) { currentcolor = image.GetPixel(i, j); if (currentcolor == Color.FromArgb(255, 0, 0, 0)) { vb[(i * imageHeight) + j] = new Vecteur2D(i, j); } } } for (int i = 0; i < vb.Length; i++) { for (int j = 0; j < vs.Length; j++) { if (vs[j] != null && vb[i] != null) { if ((int)vs[j].x == (int)vb[i].x && (int)vs[j].y == (int)vb[i].y) { return(true); } } } } return(false); } }
/// <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> /// Private constructor /// </summary> /// <param name="gameSize">Size of the game area</param> private Game(Size gameSize) { this.gameSize = gameSize; playerShip = new SpaceShip(space_invaders.Properties.Resources.ship1, new Vecteur2D(gameSize.Width / 2 - space_invaders.Properties.Resources.ship1.Width / 2, gameSize.Height - space_invaders.Properties.Resources.ship1.Height), 5); bunkers = new List <Bunker>(); for (int i = gameSize.Width / 10; i < gameSize.Width; i += gameSize.Width / 3) { bunkers.Add(new Bunker(new Vecteur2D(i, playerShip.Position.y - 100))); } WindowsMediaPlayer Pew = new WindowsMediaPlayer(); WindowsMediaPlayer DoublePoints = new WindowsMediaPlayer(); WindowsMediaPlayer Boom = new WindowsMediaPlayer(); WindowsMediaPlayer Suck = new WindowsMediaPlayer(); WindowsMediaPlayer BigPew = new WindowsMediaPlayer(); WindowsMediaPlayer Bop = new WindowsMediaPlayer(); WindowsMediaPlayer Touched = new WindowsMediaPlayer(); WindowsMediaPlayer Down = new WindowsMediaPlayer(); WindowsMediaPlayer Up = new WindowsMediaPlayer(); WindowsMediaPlayer Easy = new WindowsMediaPlayer(); WindowsMediaPlayer Medium = new WindowsMediaPlayer(); WindowsMediaPlayer Hard = new WindowsMediaPlayer(); WindowsMediaPlayer EasyMusic = new WindowsMediaPlayer(); WindowsMediaPlayer MediumMusic = new WindowsMediaPlayer(); WindowsMediaPlayer HardMusic = new WindowsMediaPlayer(); WindowsMediaPlayer WinMusic = new WindowsMediaPlayer(); WindowsMediaPlayer LostMusic = new WindowsMediaPlayer(); WindowsMediaPlayer IntroMusic = new WindowsMediaPlayer(); /*Pew.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\Pew.wav"); * DoublePoints.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\DoublePoints.wav"); * Boom.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\Boom.wav"); * Suck.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\Suck.wav"); * BigPew.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\BigPew.wav"); * Bop.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\Bop.wav"); * Touched.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\Touched.wav"); * Down.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\Down.wav"); * Up.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\Up.wav"); * Easy.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\Easy.wav"); * Medium.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\Medium.wav"); * Hard.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\Hard.wav"); * EasyMusic.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\EasyMusic.wav"); * MediumMusic.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\MediumMusic.wav"); * HardMusic.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\HardMusic.wav"); * WinMusic.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\WinMusic.wav"); * LostMusic.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\LostMusic.wav"); * IntroMusic.URL = Environment.CurrentDirectory.Replace("bin\\Debug", "Resources\\IntroMusic.wav");*/ Pew.URL = "Resources\\Pew.wav"; DoublePoints.URL = "Resources\\DoublePoints.wav"; Boom.URL = "Resources\\Boom.wav"; Suck.URL = "Resources\\Suck.wav"; BigPew.URL = "Resources\\BigPew.wav"; Bop.URL = "Resources\\Bop.wav"; Touched.URL = "Resources\\Touched.wav"; Down.URL = "Resources\\Down.wav"; Up.URL = "Resources\\Up.wav"; Easy.URL = "Resources\\Easy.wav"; Medium.URL = "Resources\\Medium.wav"; Hard.URL = "Resources\\Hard.wav"; EasyMusic.URL = "Resources\\EasyMusic.mp3"; MediumMusic.URL = "Resources\\MediumMusic.mp3"; HardMusic.URL = "Resources\\HardMusic.mp3"; WinMusic.URL = "Resources\\WinMusic.wav"; LostMusic.URL = "Resources\\LostMusic.wav"; IntroMusic.URL = "Resources\\IntroMusic.mp3"; Sounds.Add(Pew); Sounds.Add(DoublePoints); Sounds.Add(Boom); Sounds.Add(Suck); Sounds.Add(BigPew); Sounds.Add(Bop); Sounds.Add(Touched); Sounds.Add(Down); Sounds.Add(Up); Sounds.Add(Easy); Sounds.Add(Medium); Sounds.Add(Hard); Sounds.Add(EasyMusic); Sounds.Add(MediumMusic); Sounds.Add(HardMusic); Sounds.Add(WinMusic); Sounds.Add(LostMusic); Sounds.Add(IntroMusic); foreach (WindowsMediaPlayer s in Sounds) { s.settings.volume = 0; s.controls.stop(); } }