Ejemplo n.º 1
0
        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
            case Keys.Up: _gameModel.RotateShape(); break;

            case Keys.Left: _gameModel.MoveShapeLeft(); break;

            case Keys.Right: _gameModel.MoveShapeRight(); break;

            case Keys.Down: _gameModel.MoveShapeDown(); break;

            default: break;
            }
        }
Ejemplo n.º 2
0
        public MainForm()
        {
            Text            = "Tetris";
            ClientSize      = new Size(_gameModelWidth * BlockSize + 120, _gameModelHeight * BlockSize);
            StartPosition   = FormStartPosition.CenterScreen;
            FormBorderStyle = FormBorderStyle.FixedDialog;
            MaximizeBox     = false;
            DoubleBuffered  = true;

            _gameModel = new GameModel(_gameModelHeight, _gameModelWidth);

            var nextShapeLabel = new Label
            {
                Text     = $"Next shape:",
                Location = new Point(ClientSize.Width - 100, 20)
            };

            this.Controls.Add(nextShapeLabel);
            this._nextShapePositionX = nextShapeLabel.Location.X - BlockSize;
            this._nextShapePositionY = nextShapeLabel.Location.Y + 30;
            this._nextShape          = _gameModel.NextShape;
            this.PlaceNextShape();

            _gameModel.NextShapeChanged += (sender, e)
                                           =>
            {
                this.ClearNextShapeArea();
                _nextShape = _gameModel.NextShape;
                this.PlaceNextShape();
            };

            var scoreLabel = new Label
            {
                Text     = "Current score:\n\n0",
                Location = new Point(ClientSize.Width - 100, _nextShapePositionY + 60),
                Height   = 40
            };

            this.Controls.Add(scoreLabel);

            _gameModel.GameScoreChanged += (sender, e)
                                           => scoreLabel.Text = $"Current score:\n\n{e?.GameScore ?? -1}";

            _gameModel.GameScoreChanged += ChangeTimerInterval;

            _gameModel.GameEnd += OnGameEnd;

            var getNameForm = new GetNameForm();

            getNameForm.NameChanged += (sender, e)
                                       => _playerName = e.Name;

            getNameForm.Show();

            Timer = new System.Windows.Forms.Timer {
                Interval = 450
            };
            Timer.Tick += (s, a) =>
            {
                _gameModel.MoveShapeDown();
                Invalidate();
            };
        }