コード例 #1
0
ファイル: Game1.cs プロジェクト: gdd-shobhit/CokeEmUpGame
        /// <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

            gameState      = GameState.Menu;                                                        // starting game state
            powerTrigger   = SpecialPowerTrigger.off;                                               // initial state of specialPower
            RNG            = new Random();
            levelDisplayer = 1;                                                                     // starting level
            timer          = 10;                                                                    // starting timer
            widthOfScreen  = GraphicsDevice.Viewport.Width;                                         // stores the width of screen
            heightOfScreen = GraphicsDevice.Viewport.Height;                                        // stores the height of screen
            counter        = 0;                                                                     // to check number of collectables collected
            collectables   = new List <Collectible>();                                              // initializing the list of collectables
            player         = Content.Load <Texture2D>("player");                                    // loads player's content
            collectable    = Content.Load <Texture2D>("coke");                                      // loads collectable texture
            playerObject   = new Player(player, new Rectangle(GraphicsDevice.Viewport.Width / 2,    // creates the player
                                                              GraphicsDevice.Viewport.Height / 2, 50, 100));

            for (int i = 0; i < 10; i++)
            {
                collectables.Add(new Collectible(collectable, new Rectangle(RNG.Next(1, 786), RNG.Next(1, 426), 30, 70)));      // adding initial collectables
            }

            base.Initialize();
        }
コード例 #2
0
ファイル: Game1.cs プロジェクト: gdd-shobhit/CokeEmUpGame
        /// <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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                gameState = GameState.Menu;                     // gets the menu
            }
            // TODO: Add your update logic here
            previousKbState = kbState;                          // stores the previous state after a frame
            kbState         = Keyboard.GetState();              // stores tbhe state in the current frame

            switch (gameState)
            {
            case GameState.Menu:
                if (SingleKeyPress(Keys.Enter))                 // single press used to start the game
                {
                    this.ResetGame();                           // reset method to set things from the initial value
                    gameState = GameState.Game;                 // changing the state to game as Enter was pressed
                }
                break;

            case GameState.Game:                                                        // finite state is Game

                //kbState = Keyboard.GetState();
                timer -= gameTime.ElapsedGameTime.TotalSeconds;                         // decrease time for 1 sec when 1 sec is actually passed
                this.ScreenWrap(playerObject);                                          // wraps the player
                switch (powerTrigger)
                {
                case SpecialPowerTrigger.on:
                    if (kbState.IsKeyDown(Keys.Right))
                    {
                        playerObject.PositionX += 3;
                    }                                                                           // handles the movement of the player in special power mode
                    if (kbState.IsKeyDown(Keys.Left))                                           // handles the movement of the player in special power mode
                    {                                                                           // handles the movement of the player in special power mode
                        playerObject.PositionX -= 3;                                            // handles the movement of the player in special power mode
                    }                                                                           // handles the movement of the player in special power mode
                    if (kbState.IsKeyDown(Keys.Up))
                    {
                        playerObject.PositionY -= 3;
                    }
                    if (kbState.IsKeyDown(Keys.Down))
                    {
                        playerObject.PositionY += 3;
                    }
                    for (int i = 0; i < collectables.Count; i++)
                    {
                        if (collectables[i].CheckCollision(playerObject))                       // checks Collision and increase things collected
                        {                                                                       // checks Collision and increase things collected
                            counter++;                                                          // increase the number of items collected to keep track
                            playerObject.LevelScore += 100;                                     // increase the level score
                            if (specialPowerMeter <= 15 && specialPowerMeter > 0)
                            {
                                specialPowerMeter -= 3;                                            // decreases special power by 3 for every bottle collected
                            }
                            else
                            {
                                powerTrigger = SpecialPowerTrigger.off;                          // trigger off the special power when special power meter is 0 or <0
                            }
                        }
                    }
                    break;

                case SpecialPowerTrigger.off:
                    if (specialPowerMeter == 15 && SingleKeyPress(Keys.X))                      // if special power meter is 15 and press X trigger special power
                    {
                        powerTrigger = SpecialPowerTrigger.on;                                  // triggering special power
                        break;
                    }
                    else
                    {
                        if (kbState.IsKeyDown(Keys.Right))
                        {
                            playerObject.PositionX += 2;
                        }                                                                           // handles the movement of the player without special power
                        if (kbState.IsKeyDown(Keys.Left))                                           // handles the movement of the player without special power
                        {                                                                           // handles the movement of the player without special power
                            playerObject.PositionX -= 2;                                            // handles the movement of the player without special power
                        }                                                                           // handles the movement of the player without special power
                        if (kbState.IsKeyDown(Keys.Up))
                        {
                            playerObject.PositionY -= 2;
                        }
                        if (kbState.IsKeyDown(Keys.Down))
                        {
                            playerObject.PositionY += 2;
                        }
                        for (int i = 0; i < collectables.Count; i++)
                        {
                            if (collectables[i].CheckCollision(playerObject))                       // checks Collision and increase things collected
                            {                                                                       // checks Collision and increase things collected
                                counter++;                                                          // increase the number of items collected to keep track
                                playerObject.LevelScore += 100;                                     // increase  the level score
                                if (specialPowerMeter < 15)
                                {
                                    specialPowerMeter++;                                            // increases special power for every bottle collected
                                }
                            }
                        }
                        break;
                    }
                }
                if (counter < collectables.Count && timer < 0)                                      // time runs out and not enough bottle collected
                {
                    gameState = GameState.GameOver;                                                 // leads to game over
                }
                if (timer < 0)                                                                      // timer runs out alone
                {
                    gameState = GameState.GameOver;                                                 // leads to game over
                }
                if (counter == collectables.Count)                                                  // all collectables collected leads to increase a  level
                {
                    this.NextLevel();                                                               // method for next level
                }

                break;

            case GameState.GameOver:                                                     // game state is GameOver
                if (SingleKeyPress(Keys.Enter))
                {
                    gameState = GameState.Menu;                                         // press enter to get main menu
                }
                break;
            }
            base.Update(gameTime);
        }