Пример #1
0
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            spriteBatch.Draw(StarfieldImg, Vector2.Zero, Color.White);

            spriteBatch.Draw(InvaderImg, new Vector2(Alien.GetXPos(), Alien.GetYPos()), Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }
Пример #2
0
        /// <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);
        }
Пример #3
0
        // 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;
            }
        }