コード例 #1
0
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here
            frameElapsed = (int)(gameTime.TotalGameTime.TotalMilliseconds / (timePerFrame)); //alter the # in front of "timePerFrame" to change the speed of the animation
            frame        = frameElapsed % numFrames + 1;

            timer += gameTime.ElapsedGameTime.TotalMilliseconds;

            switch (gameState)
            {
            case GameState.Prologue:
                msState = Mouse.GetState();
                if (previousMsState.LeftButton == ButtonState.Pressed && msState.LeftButton == ButtonState.Released)
                {
                    MouseClick(msState.X, msState.Y);
                }
                previousMsState = msState;
                break;

            case GameState.Credit:
                msState = Mouse.GetState();
                if (previousMsState.LeftButton == ButtonState.Pressed && msState.LeftButton == ButtonState.Released)
                {
                    MouseClick(msState.X, msState.Y);
                }
                previousMsState = msState;
                break;

            case GameState.Menu:
                character.Level = 0;
                msState         = Mouse.GetState();
                if (previousMsState.LeftButton == ButtonState.Pressed && msState.LeftButton == ButtonState.Released)
                {
                    MouseClick(msState.X, msState.Y);
                }
                previousMsState = msState;
                break;

            case GameState.CharacterSelection:

                msState = Mouse.GetState();
                if (previousMsState.LeftButton == ButtonState.Pressed && msState.LeftButton == ButtonState.Released)
                {
                    MouseClick(msState.X, msState.Y);
                }
                previousMsState = msState;
                break;

            case GameState.Playing:
                if (character.Health > 0)    //game is only running when player is alive
                {
                    //MediaPlayer.Play(song);
                    msState = Mouse.GetState();
                    if (previousMsState.LeftButton == ButtonState.Pressed && msState.LeftButton == ButtonState.Released)
                    {
                        MouseClick(msState.X, msState.Y);
                    }
                    previousMsState = msState;
                    // Scrolling backgrounds
                    if (tunnelWall1.rectangle.X + tunnelWall1.tunnel.Width <= 0)
                    {
                        tunnelWall1.rectangle.X = tunnelWall2.rectangle.X + tunnelWall2.tunnel.Width;
                    }
                    if (tunnelWall2.rectangle.X + tunnelWall2.tunnel.Width <= 0)
                    {
                        tunnelWall2.rectangle.X = tunnelWall1.rectangle.X + tunnelWall1.tunnel.Width;
                    }
                    tunnelWall1.Update();
                    tunnelWall2.Update();

                    kbState = Keyboard.GetState();
                    if (kbState.IsKeyDown(Keys.Up))
                    {
                        character.Position = new Rectangle(character.Position.X, character.Position.Y - 3, PLAYER_W, PLAYER_H);
                    }
                    if (kbState.IsKeyDown(Keys.Down))
                    {
                        character.Position = new Rectangle(character.Position.X, character.Position.Y + 3, PLAYER_W, PLAYER_H);
                    }
                    KeepOnScreen(character);
                    //character.Level = 1;
                    //moveing all collctibles on the screen with the same speed as the background
                    for (int i = 0; i < chairList.Count; i++)
                    {
                        chairList[i].Speed = tunnelWall1.movingSpeed;
                        chairList[i].Moving();
                    }
                    for (int i = 0; i < collectibleList.Count; i++)     //making collectibles
                    {
                        collectibleList[i].Speed = tunnelWall1.movingSpeed;
                        collectibleList[i].Moving();
                    }
                    for (int i = 0; i < idList.Count; i++)     //making ids move
                    {
                        idList[i].Speed = tunnelWall1.movingSpeed;
                        idList[i].Moving();
                    }
                    // Check for collisions between character and chairList
                    foreach (Obstacles obstacle in chairList)
                    {
                        if (character.Health >= 1)    //only check for collision if player still have any lives
                        {
                            if (obstacle.CheckCollision(character))
                            {
                                character.Health--;
                                if (score >= 50)     //only removes 50 if there are 50 points to remove
                                {
                                    score = score - 50;
                                }
                                else     //otherwise sets score to 0. prevents score from going negative
                                {
                                    score = 0;
                                }
                            }
                        }
                    }
                    //milk adds health
                    foreach (Collectibles collectible in collectibleList)
                    {
                        if (character.Health >= 1 && character.Health < 3)     //only check if the character has 1 or 2 hearts
                        {
                            if (collectible.CheckCollision(character))
                            {
                                character.Health++;
                            }
                        }
                    }
                    //id adds 50 to score
                    foreach (Collectibles ids in idList)
                    {
                        if (ids.CheckCollision(character))
                        {
                            score = score + 50;
                        }
                    }

                    //click 'Enter' to pause the game
                    if (SingleKeyPress(Keys.Enter))
                    {
                        gameState = GameState.Pause;
                    }
                    if (timer >= 2000)     //if 1 second has passed
                    {
                        score += 2;
                        timer -= 1000;
                    }
                    if (chairList[chairList.Count - 1].Active == false)
                    {
                        NextLevel();
                    }
                }
                else
                {
                    if (snowPos.X < 0)
                    {
                        snowPos.X += 5;
                    }
                    else
                    {
                        gameState = GameState.GameOver;
                    }
                }
                break;

            case GameState.Options:
                msState = Mouse.GetState();
                if (previousMsState.LeftButton == ButtonState.Pressed && msState.LeftButton == ButtonState.Released)
                {
                    MouseClick(msState.X, msState.Y);
                }
                previousMsState = msState;
                break;

            case GameState.GameOver:
                msState = Mouse.GetState();
                if (previousMsState.LeftButton == ButtonState.Pressed && msState.LeftButton == ButtonState.Released)
                {
                    MouseClick(msState.X, msState.Y);
                }
                previousMsState = msState;
                break;

            case GameState.Pause:
                frame   = 0;
                msState = Mouse.GetState();
                if (previousMsState.LeftButton == ButtonState.Pressed && msState.LeftButton == ButtonState.Released)
                {
                    MouseClick(msState.X, msState.Y);
                }
                previousMsState = msState;
                previousKbState = kbState;
                kbState         = Keyboard.GetState();
                if (SingleKeyPress(Keys.Space))     // Press 'Space' to resume
                {
                    Pause(tunnelWall1);
                    Pause(tunnelWall2);
                    gameState = GameState.Resume;
                }
                if (timer >= 2000)     //if 1 second has passed
                {
                    timer -= 2000;
                }
                break;

            case GameState.Resume:
                timePerFrame = 100;
                msState      = Mouse.GetState();
                if (previousMsState.LeftButton == ButtonState.Pressed && msState.LeftButton == ButtonState.Released)
                {
                    MouseClick(msState.X, msState.Y);
                }
                previousMsState = msState;
                kbState         = Keyboard.GetState();
                if (SingleKeyPress(Keys.Space))
                {
                    gameState = GameState.Playing;
                    Resume(tunnelWall1);
                    Resume(tunnelWall2);
                    msState = Mouse.GetState();
                }
                previousMsState = msState;
                break;

            default:
                break;
            }

            base.Update(gameTime);
        }