public void Update()
        {
            var inputKey = new ConsoleKeyInfo();

            var stopwatch = new System.Diagnostics.Stopwatch();

            stopwatch.Start();
            double prevElapsed = 0;

            do
            {
                Input.Instance.Update();

                //Drawer.Clear();
                var currentElapsed = stopwatch.Elapsed.TotalMilliseconds;
                var delta          = currentElapsed - prevElapsed;
                prevElapsed = currentElapsed;

                Field.RotateBlocks        = Input.Instance.IsKeyTrigger(Input.KeyCode.Z);
                Field.ReverseRotateBlocks = Input.Instance.IsKeyTrigger(Input.KeyCode.X);
                Field.MoveLeftBlocks      = Input.Instance.IsKeyPress(Input.KeyCode.Left);
                Field.MoveRightBlocks     = Input.Instance.IsKeyPress(Input.KeyCode.Right);
                Field.MoveDownBlocks      = Input.Instance.IsKeyPress(Input.KeyCode.Down);
                Field.FallenBlocks        = Input.Instance.IsKeyTrigger(Input.KeyCode.Up);

                if (Input.Instance.IsKeyTrigger(Input.KeyCode.A))
                {
                    var swappedBlocks = Field.SwapActiveBlocks(HoldBlocks);

                    if (swappedBlocks != null)
                    {
                        HoldBlocks = swappedBlocks;
                        MergeToScreen(HoldBlocks, 1, 6);
                    }
                }

                int clearLine = Field.Step(delta);
                Score += clearLine * 100;

                MergeToScreen(Field.FieldBlocks, 6, 1, FieldScreenOffsetY);
                if (!Field.HasActiveBlock)
                {
                    var blocks = Blocks.ReplaceBlock(NextBlocks, Defs.Blocks.EmptyBlock, Defs.Blocks.EmptyField);
                    if (Field.SpawnBlock(blocks))
                    {
                        NextBlocks = GetRandomBlocks();
                        MergeToScreen(NextBlocks, 1, 1);
                    }
                    else
                    {
                        break;
                    }
                }
                Drawer.DrawDifference(Screen, PreviousScreen);
                PreviousScreen = (Defs.Blocks[, ])Screen.Clone();

                int x = Screen.GetLength(1), y = 2;
                Drawer.DrawText(x + 1, y++, "SCORE: {0}", Score);

                System.Threading.Thread.Sleep(1000 / 10);
            }while (inputKey.Key != ConsoleKey.Q);

            stopwatch.Stop();

            Drawer.Clear();

            Console.WriteLine("Your Score: {0}", Score);
            Console.ReadLine();
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="deltaTime"></param>
        /// <returns></returns>
        public int Step(double deltaTime)
        {
            ElapsedTime += deltaTime;

            if (ActiveBlocks == null)
            {
                return(0);
            }

            ClearBlock(ActiveBlocks.Blocks, ActiveBlocks.X, ActiveBlocks.Y);

            if (RotateBlocks)
            {
                var rotateBlocks = Blocks.RotateBlock(ActiveBlocks.Blocks);

                if (!IsHitBlock(rotateBlocks, ActiveBlocks.X, ActiveBlocks.Y))
                {
                    ActiveBlocks.Blocks = rotateBlocks;
                }

                RotateBlocks = false;
            }

            if (ReverseRotateBlocks)
            {
                var rotateBlocks = Blocks.ReverseRotateBlock(ActiveBlocks.Blocks);

                if (!IsHitBlock(rotateBlocks, ActiveBlocks.X, ActiveBlocks.Y))
                {
                    ActiveBlocks.Blocks = rotateBlocks;
                }

                ReverseRotateBlocks = false;
            }

            if (MoveLeftBlocks)
            {
                if (!IsHitBlock(ActiveBlocks.Blocks, ActiveBlocks.X - 1, ActiveBlocks.Y))
                {
                    --ActiveBlocks.X;
                }

                MoveLeftBlocks = false;
            }

            if (MoveRightBlocks)
            {
                if (!IsHitBlock(ActiveBlocks.Blocks, ActiveBlocks.X + 1, ActiveBlocks.Y))
                {
                    ++ActiveBlocks.X;
                }

                MoveRightBlocks = false;
            }

            if (MoveDownBlocks)
            {
                if (!IsHitBlock(ActiveBlocks.Blocks, ActiveBlocks.X, ActiveBlocks.Y + 1))
                {
                    ++ActiveBlocks.Y;
                }

                MoveDownBlocks = false;
            }

            bool inactiveBlocks = false;

            if (FallenBlocks)
            {
                while (!IsHitBlock(ActiveBlocks.Blocks, ActiveBlocks.X, ActiveBlocks.Y + 1))
                {
                    ++ActiveBlocks.Y;
                }
                inactiveBlocks = true;

                FallenBlocks = false;
            }
            else if (ElapsedTime > 500)
            {
                ElapsedTime = 0;

                if (!IsHitBlock(ActiveBlocks.Blocks, ActiveBlocks.X, ActiveBlocks.Y + 1))
                {
                    ++ActiveBlocks.Y;
                }
                else
                {
                    inactiveBlocks = true;
                }
            }

            FixBlock(ActiveBlocks.Blocks, ActiveBlocks.X, ActiveBlocks.Y);

            if (inactiveBlocks)
            {
                ActiveBlocks = null;
            }

            int lineCount = ClearLine();

            if (lineCount > 0)
            {
                CloseUpBlocks();
            }

            return(lineCount);
        }