Beispiel #1
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            Alien = new Invader();

            base.Initialize();
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            RocketXPos = 512;

            AlienDirection = -1;
            AlienSpeed     = 16;

            Invaders = new Invader[11];

            int XPos = 512;

            for (int Count = 0; Count < 11; Count++)
            {
                Invaders[Count] = new Invader();
                Invaders[Count].SetXPos(XPos);
                Invaders[Count].SetYPos(100);

                XPos = XPos + 32;
            }

            Ticks = 0;

            MissileFired = null;

            base.Initialize();
        }
Beispiel #3
0
 static void InitializeInvaders(ref Invader[,] invaders)
 {
     for (int i = 0; i < invaders.GetLength(0); i++)
     {
         for (int j = 0; j < invaders.GetLength(1); j++)
         {
             invaders[i, j] = new Invader(new Vector2i(j, i), j, i); // j is row, i is column
         }
     }
 }
Beispiel #4
0
 public void Init(IPlayer p)
 {
     m_player   = p;
     m_Invaders = new IInvader[12, 5];
     for (int y = 0; y < 4; y++)
     {
         for (int x = 0; x < 12; x++)
         {
             m_Invaders[x, y] = new Invader(x, y, this);
         }
     }
 }
Beispiel #5
0
        static void Main(string[] args)
        {
            Globals g = new Globals();                // DO NOT USE

            Barrier[] barriers = new Barrier[4];      // The original had 4 barriers
            InitializeBarriers(ref barriers);
            Invader[,] invaders = new Invader[5, 11]; // The original had a grid of 5 x 11 invaders
            InitializeInvaders(ref invaders);

            Player player = Player.GetInstance();

            Display display = Display.GetInstance();

            display.Init();
            while (display.IsOpen())
            {
                display.Clear();          // Clears the window from the previous display
                display.CheckForEvents(); // Checks for events such as closing the window

                player.PlayerControls();
                Loop(ref player, ref invaders, ref barriers);

                if (player.isDead || gameOver)
                {
                    display.Close();
                    for (int i = 0; i < 100; i++)
                    {
                        System.Console.WriteLine("YOU LOST!");
                    }
                }

                display.DrawPlayer(ref player);     // Player rectangle being passed to draw
                display.DrawInvaders(ref invaders); // Invader rectangle being passed to draw
                display.DrawBarriers(ref barriers);
                display.Update();                   // Draws on the window from the buffer
            }
        }
        /// <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);
        }
Beispiel #7
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;
            }
        }
Beispiel #8
0
        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;
        }