예제 #1
0
        private static void RunGame()
        {
            frame = new ConsoleBuffer();
            frame.SetFontSize(16);
            game = new TetrisGame(20, 30);

            var border         = frame.Window(0, 0, game.Width + PADDING_SIZE, game.Height + PADDING_SIZE);
            var board          = frame.Window(PADDING, PADDING, game.Width, game.Height);
            var nextPiecePanel = frame.Window(border.AbsoluteRight + 1, 2, 7, 5);
            var scorePanel     = frame.Window(nextPiecePanel.AbsoluteLeft, nextPiecePanel.AbsoluteBottom + 1, 7, 2);
            var lastCommand    = "";

            using var canceller = new CancellationTokenSource();
            var keys = new Win32KeyEventSource(canceller.Token);

            keys.AddKeyAlias("reverse flip", Control | Up);
            keys.AddKeyAlias("flip", Up);
            keys.AddKeyAlias("drop", Down);
            keys.AddKeyAlias("left", Left);
            keys.AddKeyAlias("right", Right);

            var keyActions = new Dictionary <string, Action <bool> >()
            {
                ["flip"]         = game.SetFlip,
                ["reverse flip"] = game.SetReverseFlip,
                ["drop"]         = game.SetDrop,
                ["left"]         = game.SetLeft,
                ["right"]        = game.SetRight
            };

            keys.Changed += delegate(object sender, KeyChangeEventArgs args)
            {
                lastCommand = args.Name;
                keyActions[args.Name](args.State);
            };

            keys.Start();

            var last = DateTime.Now;

            while (!game.GameOver)
            {
                var now   = DateTime.Now;
                var delta = now - last;
                if (delta > TimeSpan.Zero)
                {
                    last = now;
                    game.Update(delta);
                    Draw(border, board, nextPiecePanel, scorePanel, lastCommand);
                }
            }

            canceller.Cancel();
            keys.Quit();
        }