Ejemplo n.º 1
0
        protected override void Update(GameTime gameTime)
        {
            // Q to quit
            if (Keyboard.GetState().IsKeyDown(Keys.Q))
            {
                Exit();
            }

            // Get key presses
            ks = Keyboard.GetState();

            // Newly pressed keys
            List <Keys> newKeys = new List <Keys>();

            Keys[] pressed = ks.GetPressedKeys();

            // Account for new keys
            foreach (Keys k in pressed)
            {
                if (!accountedKeys.Contains(k))
                {
                    newKeys.Add(k);
                    accountedKeys.Add(k);
                }
            }

            // Remove accounted no longer pressed
            for (int i = 0; i < accountedKeys.Count; i++)
            {
                bool remove = true;
                foreach (Keys pK in pressed)
                {
                    if (accountedKeys[i] == pK)
                    {
                        remove = false;
                        break;
                    }
                }
                if (remove)
                {
                    accountedKeys.RemoveAt(i);
                    i--;
                }
            }

            if (currState == GameState.playing)
            {
                /*      Movement        */

                // Down
                if (accountedKeys.Contains(Keys.S))
                {
                    // Stop fall clock
                    fallCount = 0;

                    // Start hesitation
                    downHes++;

                    // Initial down movement when pressed
                    if (downHes == 1)
                    {
                        if (board.Move(new int[] { 0, -1 }, true))
                        {
                            land.Play();
                        }
                    }
                    // Test if past hesitation threshold and at specific values
                    else if (downHes >= 24 && downHes % 4 == 0)
                    {
                        if (board.Move(new int[] { 0, -1 }, false))
                        {
                            land.Play();
                        }
                    }
                }
                // Otherwise move down by clock
                else
                {
                    // Reset downwards hesitation
                    downHes = 0;

                    // Update fall values
                    fallCount++;
                    fallCount %= 60;

                    // Move down once at a specific value
                    if (fallCount == 59)
                    {
                        if (board.Move(new int[] { 0, -1 }, true))
                        {
                            land.Play();
                        }
                    }
                }

                // Left/Right
                if (newKeys.Contains(Keys.A))
                {
                    if (board.Move(new int[] { -1, 0 }, false))
                    {
                        land.Play();
                    }
                }
                if (newKeys.Contains(Keys.D))
                {
                    if (board.Move(new int[] { 1, 0 }, false))
                    {
                        land.Play();
                    }
                }

                // Instant Place
                if (newKeys.Contains(Keys.W))
                {
                    if (board.Fall())
                    {
                        newPieceCooldown = 25;
                        fall.Play();
                    }
                }

                // Hold Piece
                if (newKeys.Contains(Keys.Space))
                {
                    if (canHold && board.Hold() != null)
                    {
                        canHold = false;
                    }
                }

                // Rotate
                if (newKeys.Contains(Keys.N))
                {
                    int[][] counterClockRot = new int[][] { new int[] { 0, 1 }, new int[] { -1, 0 } };
                    board.Rotate(counterClockRot);
                }
                if (newKeys.Contains(Keys.M))
                {
                    int[][] clockRot = new int[][] { new int[] { 0, -1 }, new int[] { 1, 0 } };
                    board.Rotate(clockRot);
                }

                /*      Update Game     */

                // Clean board of finished lines
                board.Clean();

                // Test if all pieces are below position
                if (!board.IsValid())
                {
                    Lose();
                }

                // If there are no more valid places
                if (board.currPiece == null)
                {
                    newPieceCooldown++;
                    newPieceCooldown %= 30;
                    if (newPieceCooldown == 1)
                    {
                        int randPiece = (int)(random.NextDouble() * pieces.Count);

                        if (!board.AddPiece(pieces[randPiece].Clone(-1, -1)))
                        {
                            Lose();
                        }
                        else
                        {
                            canHold = true;
                        }
                    }
                }
            }
            else if (currState == GameState.lost)
            {
                if (newKeys.Contains(Keys.N))
                {
                    Exit();
                }
                else if (newKeys.Contains(Keys.Y))
                {
                    currState = GameState.playing;
                    board.Reset();
                    MediaPlayer.Play(ThemeA);
                }
            }

            base.Update(gameTime);
        }