예제 #1
0
        private bool CanMove(Tetromino.Directions d)
        {
            Tetromino tmp = (Tetromino)activeTetromino.Clone();

            tmp.Move(d);

            if (tmp.X / BlockWidth + tmp.Area.Left < 0)
            {
                return(false);
            }
            if (tmp.X / BlockWidth + tmp.Area.Right > GridWidth - 1)
            {
                return(false);
            }
            if (tmp.Y / BlockWidth + tmp.Area.Bottom > GridHeight - 1)
            {
                return(false);
            }

            return(!IsOverlapping(tmp));
        }
예제 #2
0
        public Board(Form parent, int gridWidth, int gridHeight = -1, int blockWidth = 16, int blockHeight = 16)
        {
            this.parent = parent;
            GridWidth   = gridWidth;
            if (gridHeight == -1)
            {
                GridHeight = parent.Height / blockHeight;
                //GridHeight -= 4;
            }
            else
            {
                GridHeight = gridHeight;
            }
            BlockWidth  = blockWidth;
            BlockHeight = blockHeight;

            InitAudioService();

            Cells = new Cell[GridWidth][];
            for (int x = 0; x < GridWidth; x++)
            {
                Cells[x] = new Cell[GridHeight];
            }

            Stats = new Dictionary <Tetromino.TetrominoTypes, int>();

            parent.KeyDown += (object s, KeyEventArgs e) => {
                if (suspendGameLoop || gameOver)
                {
                    return;
                }

                long curTicks = DateTime.Now.Ticks;

                switch (e.KeyCode)
                {
                case Keys.Left:
                    if (CanMove(Tetromino.Directions.Left))
                    {
                        activeTetromino.Move(Tetromino.Directions.Left);
                    }
                    break;

                case Keys.Right:
                    if (CanMove(Tetromino.Directions.Right))
                    {
                        activeTetromino.Move(Tetromino.Directions.Right);
                    }
                    break;

                case Keys.Down:
                    MoveDown();
                    break;

                case Keys.Space:
                    if (curTicks - lastKeyPressedAt < 10000000)
                    {
                        return;
                    }
                    lastKeyPressedAt = curTicks;
                    Task.Run(() => { while (MoveDown())
                                     {
                                         Thread.Sleep(30);
                                     }
                             });
                    lastKeyPressedAt = curTicks + 2000000;
                    break;

                case Keys.Up:
                    activeTetromino.Move(Tetromino.Directions.Rotate);

                    bool isOutOfBounds;
                    int  x;
                    int  y;
                    do
                    {
                        isOutOfBounds = false;
                        x             = activeTetromino.X / BlockWidth;
                        y             = activeTetromino.Y / blockHeight;
                        if (x + activeTetromino.Area.Left < 0)
                        {
                            activeTetromino.Move(Tetromino.Directions.Right);
                            isOutOfBounds = true;
                        }
                        if (x + activeTetromino.Area.Right >= GridWidth)
                        {
                            activeTetromino.Move(Tetromino.Directions.Left);
                            isOutOfBounds = true;
                        }
                        if (y + activeTetromino.Area.Top < 0)
                        {
                            activeTetromino.Move(Tetromino.Directions.Down);
                            isOutOfBounds = true;
                        }
                        if (y + activeTetromino.Area.Bottom >= GridHeight)
                        {
                            activeTetromino.Move(Tetromino.Directions.Up);
                            isOutOfBounds = true;
                        }
                        if (IsOverlapping(activeTetromino))
                        {
                            activeTetromino.Move(Tetromino.Directions.Up);
                            isOutOfBounds = true;
                        }
                    } while(isOutOfBounds);
                    break;
                }
            };

            Thread gameLoop = new Thread(() => {
                ShowBanner($"LEVEL {gameLevel}");
                StartThemeMusic();

                while (!gameOver)
                {
                    Thread.Sleep(gameLoopsDelays[gameLevel - 1]);
                    if (!suspendGameLoop)
                    {
                        MoveDown();
                    }
                }
            })
            {
                IsBackground = true
            };

            gameLoop.Start();

            AddNewRandomTetromino();
        }