Beispiel #1
0
        /// <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;
                }
            }
        }
        public void UpdatePlaying(GameTime currentTime) // Main gameplay
        {
            Ticks = Ticks + currentTime.ElapsedGameTime.TotalMilliseconds;

            // Invader movement
            if (Ticks > 500)
            {
                // Invader sideways movement
                for (int rows = 0; rows < 5; rows++)
                {
                    for (int cols = 0; cols < 11; cols++)
                    {
                        if (InvaderDir.Equals("Right"))
                        {
                            InvaderArray[rows, cols].X = InvaderArray[rows, cols].X + InvaderSpd;
                        }
                        if (InvaderDir.Equals("Left"))
                        {
                            InvaderArray[rows, cols].X = InvaderArray[rows, cols].X - InvaderSpd;
                        }
                    }
                }

                // Invader Limits
                string ChangeDir = "No";
                for (int rows = 0; rows < 5; rows++)
                {
                    for (int cols = 0; cols < 11; cols++)
                    {
                        if (InvaderLiving[rows, cols].Equals("Yes"))
                        {
                            if (InvaderArray[rows, cols].X + InvaderArray[rows, cols].Width > 870)
                            {
                                InvaderDir = "Left";
                                ChangeDir  = "Yes";
                            }

                            if (InvaderArray[rows, cols].X < 30)
                            {
                                InvaderDir = "Right";
                                ChangeDir  = "Yes";
                            }
                        }
                    }
                }

                // Invader downward movement
                if (ChangeDir.Equals("Yes"))
                {
                    for (int rows = 0; rows < 5; rows++)
                    {
                        for (int cols = 0; cols < 11; cols++)
                        {
                            InvaderArray[rows, cols].Y = InvaderArray[rows, cols].Y + 8;
                        }
                    }
                }


                Ticks = 0;
            }

            // Gunship control
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                ShipXPos = ShipXPos + 3;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                ShipXPos = ShipXPos - 3;
            }

            //Gunship limits
            if (ShipXPos < 15)
            {
                ShipXPos = 15;
            }

            if (ShipXPos > 780)
            {
                ShipXPos = 780;
            }

            // Firing missile
            if (ShotsFired != null) // Moves missile when it is visible
            {
                ShotsFired.Move();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Space) & ShotsFired == null) // Fires missile when space is pressed
            {
                ShotsFired = new Missile(ShipXPos + 45, 565);
            }

            if (ShotsFired != null) // Deletes the missile when it passes the top of the screen
            {
                if (ShotsFired.GetMissilePos().Y < 0)
                {
                    ShotsFired = null;
                }
            }

            // Missile/invader collision detection
            if (ShotsFired != null)
            {
                MissileHitbox = new Rectangle((int)ShotsFired.GetMissilePos().X, (int)ShotsFired.GetMissilePos().Y,
                                              Missile.Width, Missile.Height);

                for (int rows = 0; rows < 5; rows++)
                {
                    for (int cols = 0; cols < 11; cols++)
                    {
                        if (InvaderLiving[rows, cols].Equals("Yes"))
                        {
                            if (MissileHitbox.Intersects(InvaderArray[rows, cols]))
                            {
                                ShotsFired = null;
                                InvaderLiving[rows, cols] = "No";
                                Score = Score + 50;
                            }
                        }
                    }
                }
            }

            // Gunship/Invader collision detection
            ShipHitbox = new Rectangle(ShipXPos, 565, Gunship.Width, Gunship.Height);

            for (int rows = 0; rows < 5; rows++)
            {
                for (int cols = 0; cols < 11; cols++)
                {
                    if (InvaderLiving[rows, cols].Equals("Yes"))
                    {
                        if (InvaderArray[rows, cols].Y + InvaderArray[rows, cols].Height > ShipHitbox.Y)
                        {
                            GameState = 3;
                        }
                    }
                }
            }

            // Speeds up invaders when they reach a certain point
            for (int rows = 0; rows < 5; rows++)
            {
                for (int cols = 0; cols < 11; cols++)
                {
                    if (InvaderLiving[rows, cols].Equals("Yes"))
                    {
                        if (InvaderArray[rows, cols].Y > 350)
                        {
                            InvaderSpd = 30;
                        }
                    }
                }
            }

            int Count = 0;

            for (int rows = 0; rows < 5; rows++)
            {
                for (int cols = 0; cols < 11; cols++)
                {
                    if (InvaderLiving[rows, cols].Equals("Yes"))
                    {
                        Count = Count + 1;
                    }
                }
            }

            // Finishes when all invaders killed
            if (Count == 0)
            {
                this.Exit();
            }
        }