示例#1
0
        //for rotations that go out of bounds
        public void CheckCollision(Tetrimino t)
        {
            if (side != BoundarySide.Top)
            {
                for (int i = 0; i < t.rectangles.Count; i++)
                {
                    while (box.Intersects(t.rectangles[i]))
                    {
                        //move back according to boundary side collided with
                        switch (side)
                        {
                        case BoundarySide.Left:
                            t.Move(MoveDirection.Right);
                            break;

                        case BoundarySide.Right:
                            t.Move(MoveDirection.Left);
                            break;

                        case BoundarySide.Bottom:
                            t.Move(MoveDirection.Up);
                            break;
                        }
                    }
                }
            }
        }
        public void newGame()
        {
            //Resets all necessary variables, then starts a new game.

            gameCanvas.Children.Clear();
            nextBlockCanvas.Children.Clear();

            this.logic = new GameLogic(scoreBox, levelBox, rowsBox);
            this.logic.initializeBoxes();
            gameArray        = new GameArray(gameCanvas, this.logic);
            tetriminoFactory = new TetriminoFactory(gameCanvas, gameArray);

            gameStarted = true;

            CurrentBlock = tetriminoFactory.getNewBlock();
            NextBlock    = tetriminoFactory.getNewBlock();
            drawNext();

            CurrentBlock.Move(CurrentBlock.XCoord, CurrentBlock.YCoord, CurrentBlock.Orientation);
            StartTimer();
        }
        private void drawNext()
        {
            //These are necessary for drawing the next block in the side window.

            if (NextBlock.Type == "L")
            {
                Next = new LBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
            else if (NextBlock.Type == "J")
            {
                Next = new JBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
            else if (NextBlock.Type == "T")
            {
                Next = new TBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
            else if (NextBlock.Type == "S")
            {
                Next = new SBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
            else if (NextBlock.Type == "Z")
            {
                Next = new ZBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
            else if (NextBlock.Type == "Square")
            {
                Next = new SquareBlock(nextBlockCanvas, gameArray);
                Next.Move(30, 70, 0);
            }
            else if (NextBlock.Type == "Line")
            {
                Next = new LineBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
        }
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (!paused)
                {
                    newGame();
                }
            }

            //Check for the game being started
            else if (gameStarted)
            {
                if (e.Key == Key.P)
                {
                    pauseGame();
                }

                if (e.Key == Key.S || e.Key == Key.Down)
                {
                    //Move down if game isn't paused.
                    if (!paused)
                    {
                        if (this.CurrentBlock != null && CurrentBlock.CanMove)
                        {
                            descend();
                        }
                        else
                        {
                            NewBlock();
                        }
                    }
                }

                if (e.Key == Key.A || e.Key == Key.Left)
                {
                    //Move left if game isn't paused.
                    if (!paused)
                    {
                        if (this.CurrentBlock != null && CurrentBlock.CanMove)
                        {
                            CurrentBlock.Move(CurrentBlock.XCoord - 20, CurrentBlock.YCoord, CurrentBlock.Orientation);
                        }
                        else
                        {
                            NewBlock();
                        }
                    }
                }

                if (e.Key == Key.D || e.Key == Key.Right)
                {
                    //Move right if game isn't paused.
                    if (!paused)
                    {
                        if (this.CurrentBlock != null && CurrentBlock.CanMove)
                        {
                            CurrentBlock.Move(CurrentBlock.XCoord + 20, CurrentBlock.YCoord, CurrentBlock.Orientation);
                        }
                        else
                        {
                            NewBlock();
                        }
                    }
                }

                if (e.Key == Key.W || e.Key == Key.Up)
                {
                    //Rotate the current block if game isn't paused.
                    if (!paused)
                    {
                        if (this.CurrentBlock != null && CurrentBlock.CanMove)
                        {
                            if (CurrentBlock.Orientation < 3)
                            {
                                CurrentBlock.Move(CurrentBlock.XCoord, CurrentBlock.YCoord, CurrentBlock.Orientation + 1);
                            }
                            else
                            {
                                CurrentBlock.Move(CurrentBlock.XCoord, CurrentBlock.YCoord, 0);
                            }
                        }
                        else
                        {
                            NewBlock();
                        }
                    }
                }
            }
        }