Example #1
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)
        {
            if (startup && KeystateHelper.IsKeyReleased(Keys.Enter))
            {
                startup = false;

                gameBoard = new GameBoard();
            }
            if (!startup && KeystateHelper.IsKeyReleased(Keys.Escape))
            {
                paused = !paused;
            }

            KeystateHelper.Update();
            if (!paused)
            {
                gameBoard?.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
            }

            base.Update(gameTime);
        }
Example #2
0
        public void Update(float deltaTime)
        {
            //This can happen every frame
            //Check for user input left or right
            //Determine if move is legal (make sure every block can move)
            //If move is legal, perform it

            if (KeystateHelper.IsKeyReleased(Keys.LeftShift) || KeystateHelper.IsKeyReleased(Keys.RightShift))
            {
                Hold();
            }

            bool validMove = true;

            if (KeystateHelper.IsKeyReleased(Keys.Left))
            {
                //Move Left
                foreach (Block b in activeBlocks)
                {
                    Point blockPos  = b.GetPosition();
                    Block testBlock = GetBlock(blockPos.X - 1, blockPos.Y);
                    if ((blockPos.X - 1 < 0) || (testBlock != null && !testBlock.active))
                    {
                        validMove = false;
                    }
                }

                if (validMove)
                {
                    foreach (Block b in activeBlocks)
                    {
                        Point blockPos = b.GetPosition();
                        blocks[blockPos.X, blockPos.Y] = null;
                    }

                    foreach (Block b in activeBlocks)
                    {
                        Point blockPos = b.GetPosition();
                        b.SetPosition(new Point(blockPos.X - 1, blockPos.Y));
                        blocks[blockPos.X - 1, blockPos.Y] = b;
                    }
                    activeOrigin.X -= 1;
                }
            }
            else if (KeystateHelper.IsKeyReleased(Keys.Right))
            {
                //Move Right
                foreach (Block b in activeBlocks)
                {
                    Point blockPos  = b.GetPosition();
                    Block testBlock = GetBlock(blockPos.X + 1, blockPos.Y);
                    if ((blockPos.X + 1 >= TClone.WIDTH) || (testBlock != null && !testBlock.active))
                    {
                        validMove = false;
                    }
                }

                if (validMove)
                {
                    foreach (Block b in activeBlocks)
                    {
                        Point blockPos = b.GetPosition();
                        blocks[blockPos.X, blockPos.Y] = null;
                    }

                    foreach (Block b in activeBlocks)
                    {
                        Point blockPos = b.GetPosition();
                        b.SetPosition(new Point(blockPos.X + 1, blockPos.Y));
                        blocks[blockPos.X + 1, blockPos.Y] = b;
                    }
                    activeOrigin.X += 1;
                }
            }
            validMove = true;

            if (KeystateHelper.IsKeyReleased(Keys.LeftControl) || KeystateHelper.IsKeyReleased(Keys.RightControl))
            {
                //Rotate clockwise

                //Determine centerpoint
                //Determine relative coordinates for each block
                //new relative coordinates = (x,y) -> (y,-x)

                //Check if rotation is possible
                bool    validRotation = true;
                Point[] newPositions  = new Point[activeBlocks.Count];
                for (int i = 0; i < newPositions.Length; i++)
                {
                    Point pos    = activeBlocks[i].GetPosition() - activeOrigin;
                    Point newPos = new Point(-pos.Y, pos.X) + activeOrigin;
                    newPositions[i] = newPos;

                    Block testBlock = GetBlock(newPos.X, newPos.Y);
                    if ((newPos.X < 0 || newPos.X >= TClone.WIDTH || newPos.Y < 0 || newPos.Y >= TClone.HEIGHT) ||
                        testBlock != null && !testBlock.active)
                    {
                        validRotation = false;
                    }
                }

                if (validRotation)
                {
                    //Remove the blocks from their current positon
                    foreach (Block b in activeBlocks)
                    {
                        Point pos = b.GetPosition();
                        blocks[pos.X, pos.Y] = null;
                    }

                    for (int i = 0; i < newPositions.Length; i++)
                    {
                        Block b = activeBlocks[i];
                        blocks[newPositions[i].X, newPositions[i].Y] = b;
                        b.SetPosition(newPositions[i]);
                    }
                }
            }

            //Accelerate the timer to drop the block faster
            if (KeystateHelper.state.IsKeyDown(Keys.Down))
            {
                timer += deltaTime * 5;
            }

            timer += deltaTime;
            if (timer < timePerDrop)
            {
                return;
            }
            timer = timer - timePerDrop;

            //This happens on a timer
            //Determine if the active blocks can fall again
            //If move is legal, drop the blocks
            //Else spawn a new block

            foreach (Block b in activeBlocks)
            {
                Point blockPos  = b.GetPosition();
                Block testBlock = GetBlock(blockPos.X, blockPos.Y + 1);
                if ((blockPos.Y + 1 >= TClone.HEIGHT) || (testBlock != null && !testBlock.active))
                {
                    validMove = false;
                }
            }

            if (validMove)
            {
                foreach (Block b in activeBlocks)
                {
                    Point blockPos = b.GetPosition();
                    blocks[blockPos.X, blockPos.Y] = null;
                }

                foreach (Block b in activeBlocks)
                {
                    Point blockPos = b.GetPosition();
                    b.SetPosition(new Point(blockPos.X, blockPos.Y + 1));
                    blocks[blockPos.X, blockPos.Y + 1] = b;
                }
                activeOrigin.Y += 1;
            }
            else
            {
                //Check for and clear complete lines
                int multiplier = 0;
                for (int y = 0; y < TClone.HEIGHT; y++)
                {
                    bool completeLine = true;
                    for (int x = 0; x < TClone.WIDTH; x++)
                    {
                        if (blocks[x, y] == null)
                        {
                            completeLine = false;
                        }
                    }

                    if (completeLine)
                    {
                        if (multiplier == 0)
                        {
                            multiplier = 1;
                        }
                        else
                        {
                            multiplier *= 2;
                        }

                        for (int x = 0; x < TClone.WIDTH; x++)
                        {
                            blocks[x, y] = null;
                        }

                        //Drop above blocks by one
                        for (int j = y - 1; j >= 0; j--)
                        {
                            for (int i = 0; i < TClone.WIDTH; i++)
                            {
                                Block b = blocks[i, j];
                                blocks[i, j] = null;
                                if (b == null)
                                {
                                    continue;
                                }

                                b.SetPosition(new Point(i, j + 1));
                                blocks[i, j + 1] = b;
                            }
                        }
                    }
                }
                score += multiplier * 100;

                PlacePrefab(nextPrefab);
                nextPrefab = Block.prefabs[rand.Next(Block.prefabs.Count)];
            }
        }