/// <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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // These statements check to see if there are any invaders remaining to shoot bool IsInvaderRemaining = false; for (int Count = 0; Count < 11; Count++) { if (Invaders[Count] != null) { IsInvaderRemaining = true; break; } } // If there are no invaders then move to end game state if (!IsInvaderRemaining) { this.Exit(); } if (MissileFired != null) { MissileFired.Move(); } if (Keyboard.GetState().IsKeyDown(Keys.Space)) { MissileFired = new Missile(RocketXPos, 650); } // TODO: Add your update logic here if (Keyboard.GetState().IsKeyDown(Keys.Left)) { RocketXPos = RocketXPos - 4; } if (Keyboard.GetState().IsKeyDown(Keys.Right)) { RocketXPos = RocketXPos + 4; } if (RocketXPos < 100) { RocketXPos = 100; } if (RocketXPos > 924) { RocketXPos = 924; } Ticks = Ticks + gameTime.ElapsedGameTime.TotalMilliseconds; if (Ticks > 500) { for (int Count = 0; Count < 11; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveHorizontal(AlienSpeed * AlienDirection); } } Invader LeftMostInvader = null; Invader RightMostInvader = null; for (int Count = 0; Count < 11; Count++) { if (Invaders[Count] != null) { LeftMostInvader = Invaders[Count]; break; } } for (int Count = 10; Count > 0; Count--) { if (Invaders[Count] != null) { RightMostInvader = Invaders[Count]; break; } } if (LeftMostInvader.GetXPos() < 96) { AlienDirection = +1; int XPos = 96; for (int Count = 0; Count < 11; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(4); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } } if (RightMostInvader.GetXPos() > 924) { AlienDirection = -1; int XPos = 924 - InvaderImg.Width * 10; for (int Count = 0; Count < 11; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(4); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } } Ticks = 0; } if (MissileFired != null) { Rectangle rectMissile = new Rectangle((int)MissileFired.GetPosition().X, (int)MissileFired.GetPosition().Y, MissileImg.Width, MissileImg.Height); for (int Count = 0; Count < 11; Count++) { if (Invaders[Count] != null) { Rectangle rectInvader = new Rectangle(Invaders[Count].GetXPos(), Invaders[Count].GetYPos(), InvaderImg.Width, InvaderImg.Height); if (rectMissile.Intersects(rectInvader)) { Invaders[Count] = null; MissileFired = null; break; } } } } base.Update(gameTime); }
//Update method for Gameplay screen // TODO: Add your update logic here public void UpdatePlaying(GameTime currentTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { GameState = 3; return; } //Implemented to make testing easier //easily switch through different game states if (Keyboard.GetState().IsKeyDown(Keys.F1)) { StarwarsSoundInstance.Stop(); GameState = 1; return; } if (Keyboard.GetState().IsKeyDown(Keys.F2)) { GameState = 2; return; } if (Keyboard.GetState().IsKeyDown(Keys.F3)) { GameState = 3; return; } if (Keyboard.GetState().IsKeyDown(Keys.F4)) { GameState = 4; return; } if (Keyboard.GetState().IsKeyDown(Keys.F5)) { GameState = 5; return; } //Allows switching between weapons (Also allows easier testing cos of faster gameplay) if (Keyboard.GetState().IsKeyDown(Keys.D1)) { WeaponsCount = 1; Weapon1SoundInstance.Play(); } if (Keyboard.GetState().IsKeyDown(Keys.D2)) { WeaponsCount = 2; Weapon2SoundInstance.Play(); } // These statements check to see if there are any invaders remaining to shoot bool IsBossInvaderRemaining = false; if (BossAlien != null) { IsBossInvaderRemaining = true; //If statement checks if the Boss alien is still remaining } bool IsInvaderRemaining = false; for (int Count = 0; Count < 11; Count++) { //First Array if (Invaders[Count] != null) //If statement is null that means there are no Aliens left for that particular Array { IsInvaderRemaining = true; break; } //Second Array if (InvadersOne[Count] != null) { IsInvaderRemaining = true; break; } //Third Array if (InvadersTwo[Count] != null) { IsInvaderRemaining = true; break; } //Fourth Array if (InvadersThree[Count] != null) { IsInvaderRemaining = true; break; } //Fifth Array if (InvadersFour[Count] != null) { IsInvaderRemaining = true; break; } } // If there are no invaders then move to end game state (Gamestate 3) if (!IsInvaderRemaining & !IsBossInvaderRemaining) { GameState = 3; return; } //Player Missile if (BossAlien != null & (WeaponsCount == 1 || WeaponsCount ==2)) //Checks if Boss Alien is destroyed and what weapon count it is on { //This allows weapons power up if Boss is destroyed and switching of weapons if (MissileFired != null) { MissileFired.Move(); } if (Keyboard.GetState().IsKeyDown(Keys.Space)) //Fires missile from the location of rocket, if Space bar is pressed { MissileFired = new Missile(RocketXPos, 650); ShootSoundInstance.Play(); //Sound is also played } } //Switch weapons //Used for testing to save time if (WeaponsCount == 2) { if (MissileFired != null) { MissileFired.Move2(); } if (Keyboard.GetState().IsKeyDown(Keys.Space)) { MissileFired = new Missile(RocketXPos, 650); Shoot3SoundInstance.Play(); } } //If Boss alien has been destroyed use second missile (Upgrade weapons/ Power up) if (BossAlien == null) { if (MissileFired != null) { MissileFired.Move3(); } if (Keyboard.GetState().IsKeyDown(Keys.Space)) { MissileFired = new Missile(RocketXPos, 650); if (WeaponsCount == 1) { Shoot2SoundInstance.Play(); } } } //Alien Missile if (AlienMissileFired != null) //If Alien missile is not equal to Null then alien missile starts moving { AlienMissileFired.Move(); } if (AlienMissileFired2 != null) { AlienMissileFired2.Move(); } if (AlienMissileFired3 != null) { AlienMissileFired3.Move(); } if (AlienMissileFired4 != null) { AlienMissileFired4.Move(); } if (BossMissileFired != null) { BossMissileFired.Move(); } Timer = Timer + currentTime.ElapsedGameTime.Milliseconds; //Timer to control the automation of alien/Boss shooting Timer2 = Timer2 + currentTime.ElapsedGameTime.Milliseconds; Timer3 = Timer3 + currentTime.ElapsedGameTime.Milliseconds; Timer4 = Timer4 + currentTime.ElapsedGameTime.Milliseconds; BossTimer = BossTimer + currentTime.ElapsedGameTime.Milliseconds; //First Array if (Timer > 2865) //The timer values can be adjusted at each array to change the frequency of the automatic shooting { for (int Count = 0; Count < 11; Count++) //Checks the amount of aliens { if (Invaders[Count] != null) //If available then fires from alien at the player { AlienMissileFired = new AlienMissile(Invaders[Count].GetXPos(), Invaders[Count].GetYPos()); //Fires from the X and Y coordinates of existing aliens AlienShootSoundInstance.Play(); Timer = 0; //Timer is reset back to 0 for continious shooting } } } //Second Array if (Timer2 > 4234) { for (int Count = 0; Count < 11; Count++) { if (InvadersTwo[Count] != null) { AlienMissileFired2 = new AlienMissile(InvadersTwo[Count].GetXPos(), InvadersTwo[Count].GetYPos()); AlienShootSoundInstance.Play(); Timer2 = 0; } } } //Third Array if (Timer3 > 6403) { for (int Count = 0; Count < 11; Count++) { if (InvadersThree[Count] != null) { AlienMissileFired3 = new AlienMissile(InvadersThree[Count].GetXPos(), InvadersThree[Count].GetYPos()); AlienShootSoundInstance.Play(); Timer3 = 0; } } } //Fourth Array if (Timer4 > 8000) { for (int Count = 0; Count < 11; Count++) { if (InvadersFour[Count] != null) { AlienMissileFired4 = new AlienMissile(InvadersFour[Count].GetXPos(), InvadersFour[Count].GetYPos()); AlienShootSoundInstance.Play(); Timer4 = 0; } } } //Boss Missile if (BossTimer > 7800) { if (BossAlien != null) { BossMissileFired = new AlienMissile(BossAlien.GetXPos(), BossAlien.GetYPos()); BossShootSoundInstance.Play(); BossTimer = 0; } } //Controls movement of the Aircraft and sound effects if (Keyboard.GetState().IsKeyDown(Keys.Left)) { if (BossAlien != null) { RocketXPos = RocketXPos - 4; //Changing this will affect the speed of the rocket JetSoundInstance.Play(); } if (BossLife == 2) { RocketXPos = RocketXPos - 5; //Upgrade if boss is hit (Move faster) JetSoundInstance.Play(); } if (BossLife == 1) { RocketXPos = RocketXPos - 6; //Upgrade if boss is hit (Move faster) JetSoundInstance.Play(); } if (BossAlien == null & BossLife == 0) { RocketXPos = RocketXPos - 8; //Upgrade if boss is destroyed (Move faster) JetSoundInstance.Play(); } } if (Keyboard.GetState().IsKeyDown(Keys.Right)) { if (BossAlien != null) { RocketXPos = RocketXPos + 4; //Changing this will affect the speed of the rocket JetSoundInstance.Play(); } if (BossLife == 2) { RocketXPos = RocketXPos + 5; //Upgrade if boss is hit (Move faster) JetSoundInstance.Play(); } if (BossLife == 1) { RocketXPos = RocketXPos + 6; //Upgrade if boss is hit (Move faster) JetSoundInstance.Play(); } if (BossAlien == null & BossLife == 0) { RocketXPos = RocketXPos + 8; //Upgrade if boss is destroyed (Move faster) JetSoundInstance.Play(); } } if (RocketXPos < 100) //Limits the Rockets movement to prevent it from going off the screen { RocketXPos = 100; } if (RocketXPos > 924) { RocketXPos = 924; } //Controls movement of Alien Ticks = Ticks + currentTime.ElapsedGameTime.TotalMilliseconds; //Calculates the ingame time for the movement of aliens if (Ticks > 500) //if the timer is more than 500 then moves the alien horizontal { for (int Count = 0; Count < 11; Count++) { //First Array if (Invaders[Count] != null) { Invaders[Count].MoveHorizontal(AlienSpeed * AlienDirection); } //second array if (InvadersOne[Count] != null) { InvadersOne[Count].MoveHorizontal(AlienSpeed * AlienDirection); } //Third array if (InvadersTwo[Count] != null) { InvadersTwo[Count].MoveHorizontal(AlienSpeed * AlienDirection); } //Fourth array if (InvadersThree[Count] != null) { InvadersThree[Count].MoveHorizontal(AlienSpeed * AlienDirection); } //Fifth array if (InvadersFour[Count] != null) { InvadersFour[Count].MoveHorizontal(AlienSpeed * AlienDirection); } } //Boss Alien movement if (Ticks > 500) { if (BossAlien != null) { BossAlien.MoveHorizontal(BossAlienSpeed * BossAlienDirection); } if (BossLife == 1) { BossAlienSpeed = 105; BossAlien.MoveHorizontal(BossAlienSpeed * BossAlienDirection); } } Invader LeftMostInvader = null; //Aliens set to null Invader RightMostInvader = null; for (int Count = 0; Count < 11; Count++) { //First Array if (Invaders[Count] != null) { LeftMostInvader = Invaders[Count]; break; } //second array if (InvadersOne[Count] != null) { LeftMostInvader = InvadersOne[Count]; break; } //Third array if (InvadersTwo[Count] != null) { LeftMostInvader = InvadersTwo[Count]; break; } //Fourth array if (InvadersThree[Count] != null) { LeftMostInvader = InvadersThree[Count]; break; } //Fifth array if (InvadersFour[Count] != null) { LeftMostInvader = InvadersFour[Count]; break; } } for (int Count = 10; Count > 0; Count--) { //First Array if (Invaders[Count] != null) { RightMostInvader = Invaders[Count]; break; } //second array if (InvadersOne[Count] != null) { RightMostInvader = InvadersOne[Count]; break; } //Third array if (InvadersTwo[Count] != null) { RightMostInvader = InvadersTwo[Count]; break; } //Fourth array if (InvadersThree[Count] != null) { RightMostInvader = InvadersThree[Count]; break; } //Fifth array if (InvadersFour[Count] != null) { RightMostInvader = InvadersFour[Count]; break; } } //Controls alien speed/difficulty if (LeftMostInvader != null) //Checks if aliens is available { if (LeftMostInvader.GetYPos() > 200) //Increases alien speed after to goes pass a certain Y coordinate { AlienSpeed = 50; } } if (RightMostInvader != null) { if (RightMostInvader.GetYPos() > 200) { AlienSpeed = 50; } } if (LeftMostInvader != null) { if (LeftMostInvader.GetYPos() > 300) { AlienSpeed = 60; } } if (RightMostInvader != null) { if (RightMostInvader.GetYPos() > 300) { AlienSpeed = 60; } } //Game Over when aliens reach the bottom, navigates to Gameover screen if (LeftMostInvader != null) { if (LeftMostInvader.GetYPos() > 550) { GameState = 4; //Goes to gameover screen return; } } if (RightMostInvader != null) { if (RightMostInvader.GetYPos() > 550) { GameState = 4; return; } } //Checks Boss alien direction if (BossAlien != null) { if (BossAlien.GetXPos() < 96) //Limits the aliens direction to prevent it from going off the screen { BossAlienDirection = +1; int BossXPos = 96; if (BossAlien != null) { BossAlien.SetXPos(BossXPos); } BossXPos = BossXPos + BossInvaderImg.Width; } } if (BossAlien != null) { if (BossAlien.GetXPos() > 924) { BossAlienDirection = -1; int BossXPos = 924 - BossInvaderImg.Width * 10; if (BossAlien != null) { BossAlien.SetXPos(BossXPos); } BossXPos = BossXPos + BossInvaderImg.Width; } } //Checks alien direction if (LeftMostInvader != null) { if (LeftMostInvader.GetXPos() < 96) { AlienDirection = +1; int XPos = 96; for (int Count = 0; Count < 11; Count++) //Checks the array for the position of the aliens before moving them vertically once they reached the side { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(20); Invaders[Count].SetXPos(XPos); } //Second Array if (InvadersOne[Count] != null) { InvadersOne[Count].MoveVertical(20); InvadersOne[Count].SetXPos(XPos); } //Third Array if (InvadersTwo[Count] != null) { InvadersTwo[Count].MoveVertical(20); InvadersTwo[Count].SetXPos(XPos); } //Fourth Array if (InvadersThree[Count] != null) { InvadersThree[Count].MoveVertical(20); InvadersThree[Count].SetXPos(XPos); } //Fifth Array if (InvadersFour[Count] != null) { InvadersFour[Count].MoveVertical(20); InvadersFour[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } } } if (RightMostInvader != null) //Does the same check with rightmost alien { if (RightMostInvader.GetXPos() > 924) { AlienDirection = -1; int XPos = 924 - InvaderImg.Width * 10; for (int Count = 0; Count < 11; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(20); Invaders[Count].SetXPos(XPos); } //Second Array if (InvadersOne[Count] != null) { InvadersOne[Count].MoveVertical(20); InvadersOne[Count].SetXPos(XPos); } //Third Array if (InvadersTwo[Count] != null) { InvadersTwo[Count].MoveVertical(20); InvadersTwo[Count].SetXPos(XPos); } //Fourth Array if (InvadersThree[Count] != null) { InvadersThree[Count].MoveVertical(20); InvadersThree[Count].SetXPos(XPos); } //Fifth Array if (InvadersFour[Count] != null) { InvadersFour[Count].MoveVertical(20); InvadersFour[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } } } Ticks = 0; } //Checks whether alien missile intersects with Player //If intesects then Lose life or Game Over //First array if (AlienMissileFired != null) { Rectangle rectAlienMissile = new Rectangle((int)AlienMissileFired.GetPosition().X, (int)AlienMissileFired.GetPosition().Y, AlienMissileImg.Width, AlienMissileImg.Height); Rectangle rectRocket = new Rectangle(RocketXPos, 700, RocketLauncherImg.Width, RocketLauncherImg.Height); if (rectAlienMissile.Intersects(rectRocket)) { if (AmountofLives == 0) { AlienMissileFired = null; GameState = 4; PlayerKilledSoundInstance.Play(); } AmountofLives = AmountofLives - 1; //Player life is subtract from total life AlienMissileFired = null; //When this reaches 0 then it switches over to Game Over screen PlayerKilledSoundInstance.Play(); } } //Second array if (AlienMissileFired2 != null) { Rectangle rectAlienMissile2 = new Rectangle((int)AlienMissileFired2.GetPosition().X, (int)AlienMissileFired2.GetPosition().Y, AlienMissileImg2.Width, AlienMissileImg2.Height); Rectangle rectRocket = new Rectangle(RocketXPos, 700, RocketLauncherImg.Width, RocketLauncherImg.Height); if (rectAlienMissile2.Intersects(rectRocket)) { if (AmountofLives == 0) { AlienMissileFired2 = null; GameState = 4; PlayerKilledSoundInstance.Play(); } AmountofLives = AmountofLives - 1; AlienMissileFired2 = null; PlayerKilledSoundInstance.Play(); } } //Third array if (AlienMissileFired3 != null) { Rectangle rectAlienMissile3 = new Rectangle((int)AlienMissileFired3.GetPosition().X, (int)AlienMissileFired3.GetPosition().Y, AlienMissileImg2.Width, AlienMissileImg2.Height); Rectangle rectRocket = new Rectangle(RocketXPos, 700, RocketLauncherImg.Width, RocketLauncherImg.Height); if (rectAlienMissile3.Intersects(rectRocket)) { if (AmountofLives == 0) { AlienMissileFired3 = null; GameState = 4; PlayerKilledSoundInstance.Play(); } AmountofLives = AmountofLives - 1; AlienMissileFired3 = null; PlayerKilledSoundInstance.Play(); } } //Fourth array if (AlienMissileFired4 != null) { Rectangle rectAlienMissile4 = new Rectangle((int)AlienMissileFired4.GetPosition().X, (int)AlienMissileFired4.GetPosition().Y, AlienMissileImg2.Width, AlienMissileImg2.Height); Rectangle rectRocket = new Rectangle(RocketXPos, 700, RocketLauncherImg.Width, RocketLauncherImg.Height); if (rectAlienMissile4.Intersects(rectRocket)) { if (AmountofLives == 0) { AlienMissileFired4 = null; GameState = 4; PlayerKilledSoundInstance.Play(); } AmountofLives = AmountofLives - 1; AlienMissileFired4 = null; PlayerKilledSoundInstance.Play(); } } //Boss Missile if (BossMissileFired != null) { Rectangle rectBossMissile = new Rectangle((int)BossMissileFired.GetPosition().X, (int)BossMissileFired.GetPosition().Y, BossMissileImg.Width, BossMissileImg.Height); Rectangle rectRocket = new Rectangle(RocketXPos, 700, RocketLauncherImg.Width, RocketLauncherImg.Height); if (rectBossMissile.Intersects(rectRocket)) { if (AmountofLives == 0) { BossMissileFired = null; GameState = 4; PlayerKilledSoundInstance.Play(); } AmountofLives = AmountofLives - 1; BossMissileFired = null; PlayerKilledSoundInstance.Play(); } } //Controls missle being fired //Checks whether missile intersects with Boss if (MissileFired != null) { Rectangle rectMissile = new Rectangle((int)MissileFired.GetPosition().X, (int)MissileFired.GetPosition().Y, MissileImg.Width, MissileImg.Height); //Creates a rectangle in order to compare whether the missile intercepts with alien if (BossAlien != null) { Rectangle rectBossInvader = new Rectangle(BossAlien.GetXPos(), BossAlien.GetYPos(), BossInvaderImg.Width, BossInvaderImg.Height); if (rectMissile.Intersects(rectBossInvader)) { BossLife = BossLife - 1; MissileFired = null; PlayerScore = PlayerScore + 1000; //Killing boss gives more score ExplosionSoundInstance.Play(); if (BossLife == 0) { BossAlien = null; WeaponsCount = 1; //Also gives a power up (Weapon upgrade) PlayerScore = PlayerScore + 1000; BosskilledSoundInstance.Play(); ExplosionSoundInstance.Play(); } } } } // Checks whether the missle intersects with Alien if (MissileFired != null) { Rectangle rectMissile = new Rectangle((int)MissileFired.GetPosition().X, (int)MissileFired.GetPosition().Y, MissileImg.Width, MissileImg.Height); for (int Count = 0; Count < 11; Count++) //Rectangle is created to check whether both object intesect each other { if (Invaders[Count] != null) //If it does then the alien is declared as null (destroyed) { if (BossAlien == null) { rectMissile = new Rectangle((int)MissileFired.GetPosition().X, (int)MissileFired.GetPosition().Y, MissileImg2.Width, MissileImg2.Height); } Rectangle rectInvader = new Rectangle(Invaders[Count].GetXPos(), Invaders[Count].GetYPos(), InvaderImg.Width, InvaderImg.Height); if (rectMissile.Intersects(rectInvader)) { Invaders[Count] = null; MissileFired = null; PlayerScore = PlayerScore + 150; ExplosionSoundInstance.Play(); InvaderkilledSoundInstance.Play(); break; } } //Second Array if (InvadersOne[Count] != null) { if (BossAlien == null) { rectMissile = new Rectangle((int)MissileFired.GetPosition().X, (int)MissileFired.GetPosition().Y, MissileImg2.Width, MissileImg2.Height); } Rectangle rectInvader = new Rectangle(InvadersOne[Count].GetXPos(), InvadersOne[Count].GetYPos(), InvaderImg.Width, InvaderImg.Height); if (rectMissile.Intersects(rectInvader)) { InvadersOne[Count] = null; MissileFired = null; PlayerScore = PlayerScore + 100; ExplosionSoundInstance.Play(); InvaderkilledSoundInstance.Play(); break; } } //Third Array if (InvadersTwo[Count] != null) { if (BossAlien == null) { rectMissile = new Rectangle((int)MissileFired.GetPosition().X, (int)MissileFired.GetPosition().Y, MissileImg2.Width, MissileImg2.Height); } Rectangle rectInvader = new Rectangle(InvadersTwo[Count].GetXPos(), InvadersTwo[Count].GetYPos(), InvaderImg.Width, InvaderImg.Height); if (rectMissile.Intersects(rectInvader)) { InvadersTwo[Count] = null; MissileFired = null; PlayerScore = PlayerScore + 50; ExplosionSoundInstance.Play(); InvaderkilledSoundInstance.Play(); break; } } //Fourth Array if (InvadersThree[Count] != null) { if (BossAlien == null) { rectMissile = new Rectangle((int)MissileFired.GetPosition().X, (int)MissileFired.GetPosition().Y, MissileImg2.Width, MissileImg2.Height); } Rectangle rectInvader = new Rectangle(InvadersThree[Count].GetXPos(), InvadersThree[Count].GetYPos(), InvaderImg.Width, InvaderImg.Height); if (rectMissile.Intersects(rectInvader)) { InvadersThree[Count] = null; MissileFired = null; PlayerScore = PlayerScore + 25; ExplosionSoundInstance.Play(); InvaderkilledSoundInstance.Play(); break; } } //Fifth Array if (InvadersFour[Count] != null) { if (BossAlien == null) { rectMissile = new Rectangle((int)MissileFired.GetPosition().X, (int)MissileFired.GetPosition().Y, MissileImg2.Width, MissileImg2.Height); } Rectangle rectInvader = new Rectangle(InvadersFour[Count].GetXPos(), InvadersFour[Count].GetYPos(), InvaderImg.Width, InvaderImg.Height); if (rectMissile.Intersects(rectInvader)) { InvadersFour[Count] = null; MissileFired = null; PlayerScore = PlayerScore + 25; ExplosionSoundInstance.Play(); InvaderkilledSoundInstance.Play(); break; } } } } }
// Main Update public void UpdatePlaying(GameTime currentTime) { //stop the theme song during the gameplay ThemeSoundInstance.Stop(); // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // These statements check to see if there are any invaders remaining to shoot bool IsInvaderRemaining = false; for (int Count = 0; Count < 55; Count++) { if (Invaders[Count] != null) { IsInvaderRemaining = true; break; } } // If there are no invaders then move to end game state if (!IsInvaderRemaining) { GameState = 4; return; } //move missle if active if (MissileFired != null) { MissileFired.Move(); } //if space is pressed, initiate missile if (Keyboard.GetState().IsKeyDown(Keys.Space)) { //i found it irritating that pressing space would remove the existing missile //now, a missile will only shoot if there is no existing missile if (MissileFired == null) { //at location of rocket MissileFired = new Missile(RocketXPos, 650); //play shooting sound ShootSoundInstance.Play(); } } //with the added code for pressing space, //we need to kill the missile if it goes off the screen. //otherwise if the missile misses an invader, it will continue forever you you will be unable to shoot another //if there is a missile if (MissileFired != null) { //above 1 Y co-ordinate if (MissileFired.GetPosition().Y < 1) { //kill MissileFired = null; } } // Move the rocket left and right if (Keyboard.GetState().IsKeyDown(Keys.Left)) { RocketXPos = RocketXPos - 4; } if (Keyboard.GetState().IsKeyDown(Keys.Right)) { RocketXPos = RocketXPos + 4; } //Keep the rocket on the screen if (RocketXPos < 100) { RocketXPos = 100; } if (RocketXPos > 924) { RocketXPos = 924; } //Moving The Invaders Ticks = Ticks + currentTime.ElapsedGameTime.TotalMilliseconds; //variable for changing speed int speedInt = 500; //vairable for choosing invader int invaderChoose = 0; //find the first invader which isnt dead do { invaderChoose = invaderChoose + 1; }while (Invaders[invaderChoose] == null); //assign speed depending on y position of selected invader if (Invaders[invaderChoose].GetYPos() < 200) { speedInt = 500; } else if (Invaders[invaderChoose].GetYPos() > 200) { speedInt = 300; } else if (Invaders[invaderChoose].GetYPos() > 400) { speedInt = 100; } // move invaders if (Ticks > speedInt) { //Play sound each time they move InvaderSoundInstance.Play(); //setting initial speed and direction for (int Count = 0; Count < 55; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveHorizontal(AlienSpeed * AlienDirection); } } Invader LeftMostInvader = null; Invader RightMostInvader = null; for (int Count = 0; Count < 11; Count++) { if (Invaders[Count] != null) { LeftMostInvader = Invaders[Count]; break; } } for (int Count = 10; Count > 0; Count--) { if (Invaders[Count] != null) { RightMostInvader = Invaders[Count]; break; } } if (LeftMostInvader.GetXPos() < 96) { AlienDirection = +1; int XPos = 96; for (int Count = 0; Count < 11; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(8); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } XPos = 96; for (int Count = 11; Count < 22; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(8); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } XPos = 96; for (int Count = 22; Count < 33; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(8); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } XPos = 96; for (int Count = 33; Count < 44; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(8); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } XPos = 96; for (int Count = 44; Count < 55; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(8); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } } // Keeping launcher in constraints of invaders if (RocketXPos < LeftMostInvader.GetXPos()) { RocketXPos = LeftMostInvader.GetXPos(); } if (RocketXPos > RightMostInvader.GetXPos()) { RocketXPos = RightMostInvader.GetXPos(); } // Moving Invaders left and right if (RightMostInvader.GetXPos() > 924) { AlienDirection = -1; int XPos = 924 - InvaderImg.Width * 11; for (int Count = 0; Count < 11; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(8); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } XPos = 924 - InvaderImg.Width * 11; for (int Count = 11; Count < 22; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(8); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } XPos = 924 - InvaderImg.Width * 11; for (int Count = 22; Count < 33; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(8); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } XPos = 924 - InvaderImg.Width * 11; for (int Count = 33; Count < 44; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(8); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } XPos = 924 - InvaderImg.Width * 11; for (int Count = 44; Count < 55; Count++) { if (Invaders[Count] != null) { Invaders[Count].MoveVertical(8); Invaders[Count].SetXPos(XPos); } XPos = XPos + InvaderImg.Width; } } //Resetting millisecond count for moving invaders. Ticks = 0; } // UFO //Generate Random Value Between 1 and 500 int ReturnValue = RandomNumber(1, 500); //If There is no active UFO and The Random Number Is 1, Then Set ufoalive To True if (ufo.ufoAlive == false && ReturnValue == 1) { ufo.ufoAlive = true; } //Count milliseconds ufoTime = ufoTime + currentTime.ElapsedGameTime.TotalMilliseconds; // If Ufoalive is true, i.e. there is an active ufo, increment X position by 10 pixels every 100 milliseconds // then reset ufotime variable to zero if (ufo.ufoAlive == true) { if (ufoTime > 100) { ufo.UFOxPos = ufo.UFOxPos + 10; ufoTime = 0; } } // if ufo position goes further than 1000 pixels, set ufoalive to false, i.e. kill the ufo. so a new one can be generated. if (ufo.UFOxPos > 1000) { ufo.ufoAlive = false; ufo.UFOxPos = 10; } // if a missile is fired if (MissileFired != null) { //rectangle for missile Rectangle rectMissile = new Rectangle((int)MissileFired.GetPosition().X, (int)MissileFired.GetPosition().Y, MissileImg.Width, MissileImg.Height); //for each invader for (int Count = 0; Count < 55; Count++) { if (Invaders[Count] != null) { Rectangle rectInvader = new Rectangle(Invaders[Count].GetXPos(), Invaders[Count].GetYPos(), InvaderImg.Width, InvaderImg.Height); //Check for invader collision with the missile if (rectMissile.Intersects(rectInvader)) { //set invader and missile to null Invaders[Count] = null; MissileFired = null; //increase score and play collision sound PlayerScore = PlayerScore + 100; CollisionSoundInstance.Play(); break; } } } //checking for collison with ufo Rectangle rectUfo = new Rectangle(ufo.UFOxPos, 10, UFOimg.Width, UFOimg.Height); if (rectMissile.Intersects(rectUfo)) { //add score, jump UFO to end position, Play Collision Sound PlayerScore = PlayerScore + 1000; ufo.UFOxPos = 1000; CollisionSoundInstance.Play(); } } //check for collision between invader and rocker launcher for (int Count2 = 0; Count2 < 55; Count2++) { if (Invaders[Count2] != null) { Rectangle rectInvader2 = new Rectangle(Invaders[Count2].GetXPos(), Invaders[Count2].GetYPos(), InvaderImg.Width, InvaderImg.Height); Rectangle rectRocket = new Rectangle(RocketXPos, 650, RocketLauncherImg.Width, RocketLauncherImg.Height); if (rectInvader2.Intersects(rectRocket)) { //if collision occurs, remove a life GameLives = GameLives - 1; } } } //Makeing the invaders fire back //using a new missile class which goes the opposite direction to the existing missile class // i felt it was easier to seperate the two classes completely than create another instance of the existing missile class and creating a reverse move. //if an invader isnt currently firing if (invaderMissile == null) { //generate random number for possibility of fire int RandomInvaderMissileFired = RandomNumber(1, 500); //if random number is equal to 1, fire a missle if (RandomInvaderMissileFired == 1) { //generate second random number for choosing which invader int ChooseInvader = RandomNumber(1, 55); //if the invader is not dead if (Invaders[ChooseInvader] != null) { //create new missile invaderMissile = new Missile(Invaders[ChooseInvader].GetXPos(), Invaders[ChooseInvader].GetYPos()); } } } //if missile is existant if (invaderMissile != null) { //check for collisions Rectangle rectRocket = new Rectangle(RocketXPos, 650, RocketLauncherImg.Width, RocketLauncherImg.Height); Rectangle rectInvaderMissile = new Rectangle((int)invaderMissile.GetPosition().X, (int)invaderMissile.GetPosition().Y, MissileImg.Width, MissileImg.Height); if (rectInvaderMissile.Intersects(rectRocket)) { //if collision, remove life GameLives = GameLives - 1; invaderMissile = null; } //kill if no longer visible else if (invaderMissile.GetPosition().Y > 700) { invaderMissile = null; } // else move the missile else { invaderMissile.MoveReverse(); } } //check remaining lives if (GameLives < 0) { //end game to end screen if none left GameState = 3; return; } }
//Initialise Ingame Variables public void InitialiseGameVariables() { RocketXPos = 650; //Set Rocket X value AlienDirection = -1; //Set default direction AlienSpeed = 25; //Sets a default speed (These can be changed in-game to increase difficulty) BossAlienDirection = -1; BossAlienSpeed = 100; //Declaring the Alien arrays: Invaders = new Invader[11]; //11 columns across InvadersOne = new Invader[11]; InvadersTwo = new Invader[11]; InvadersThree = new Invader[11]; InvadersFour = new Invader[11]; int XPos = 512; // Sets the default X coordinate for Aliens for (int Count = 0; Count < 11; Count++) //Checks the count for amount of aliens { //First Array Invaders[Count] = new Invader(); Invaders[Count].SetXPos(XPos); Invaders[Count].SetYPos(100); //Each line of alien has a different Y coordinate //Second Array InvadersOne[Count] = new Invader(); InvadersOne[Count].SetXPos(XPos); InvadersOne[Count].SetYPos(135); //Third Array InvadersTwo[Count] = new Invader(); InvadersTwo[Count].SetXPos(XPos); InvadersTwo[Count].SetYPos(155); //Fourth Array InvadersThree[Count] = new Invader(); InvadersThree[Count].SetXPos(XPos); InvadersThree[Count].SetYPos(180); //Fifth Array InvadersFour[Count] = new Invader(); InvadersFour[Count].SetXPos(XPos); InvadersFour[Count].SetYPos(205); XPos = XPos + 32; } int BossXPos = 512; //Sets defualt X position for Boss Alien BossAlien= new BossAlien(); BossAlien.SetXPos(BossXPos); BossAlien.SetYPos(0); Ticks = 0; //Shows the amount of time passed Timer = 0; //Timer for each variable e.g. Alien & Boss Timer2 = 0; Timer3 = 0; Timer4 = 0; MissileFired = null; //Missile values are set to null at start AlienMissileFired = null; AlienMissileFired2 = null; BossMissileFired = null; PlayerScore = 0; //Player score set to 0 at start AmountofLives = 3; //Amount of lives the player at start of game BossLife = 3; //Amount of lives Boss has at start of game WeaponsCount = 1; //Checks what weapon the player is using & allows switching between them }
protected void InitialiseGameVariables() { //initialise variables. GameLives = 3; RocketXPos = 512; AlienDirection = -1; AlienSpeed = 16; Invaders = new Invader[55]; int XPos = 512; int posY = 100; int posX = 512; //Generating the invaders. 5 blocks of code for each row for (int Count = 0; Count < 11; Count++) { Invaders[Count] = new Invader(); Invaders[Count].SetXPos(posX); Invaders[Count].SetYPos(posY); posX = posX + 32; XPos = XPos + 32; } //Resetting the X position posX = 512; for (int Count = 11; Count < 22; Count++) { posY = 150; Invaders[Count] = new Invader(); Invaders[Count].SetXPos(posX); Invaders[Count].SetYPos(posY); posX = posX + 32; XPos = XPos + 32; } posX = 512; for (int Count = 22; Count < 33; Count++) { posY = 200; Invaders[Count] = new Invader(); Invaders[Count].SetXPos(posX); Invaders[Count].SetYPos(posY); posX = posX + 32; XPos = XPos + 32; } posX = 512; for (int Count = 33; Count < 44; Count++) { posY = 250; Invaders[Count] = new Invader(); Invaders[Count].SetXPos(posX); Invaders[Count].SetYPos(posY); posX = posX + 32; XPos = XPos + 32; } posX = 512; for (int Count = 44; Count < 55; Count++) { posY = 300; Invaders[Count] = new Invader(); Invaders[Count].SetXPos(posX); Invaders[Count].SetYPos(posY); posX = posX + 32; XPos = XPos + 32; } //Setting More Game Variables & Instances Ticks = 0; MissileFired = null; PlayerScore = 0; ufo = new UFO(); base.Initialize(); invaderMissile = null; }