Example #1
0
        public static Figure GetRotatedFigure(Figure figure, double angleOfRotation)
        {
            var absoluteCosine = Math.Abs(Math.Cos(angleOfRotation));
            var absoluteSine = Math.Abs(Math.Sin(angleOfRotation));

            var cosineTimesWidth = absoluteCosine * figure.width;
            var sineTimesHeight = absoluteSine * figure.height;
            var sineTimesWidth = absoluteSine * figure.width;
            var cosineTimesHeight = absoluteCosine * figure.height;

            var figureWidth = cosineTimesWidth + sineTimesHeight;
            var figureHeight = sineTimesWidth + cosineTimesHeight;

            var rotatedFigure = new Figure(figureWidth, figureHeight);

            return rotatedFigure;
        }
Example #2
0
        private static void PrintNextFigure(Figure nextFigure)
        {
            int x = 2;
            char symbol = '\u2B1B';

            for (int i = 0; i < 4; i++)
            {
                int y = 14;
                for (int j = 0; j < 4; j++)
                {
                    if (nextFigure.Form[i, j])
                    {
                        Console.SetCursorPosition(y, x);
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write(symbol);
                    }
                    y++;
                }
                x++;
            }

            Console.SetCursorPosition(14, 1);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("NEXT");
        }
Example #3
0
        private static void PrintFigure(Figure figure)
        {
            int x = figure.UperLeftCorner.X;
            char symbol = '\u2B1B';

            for (int i = 0; i < 4; i++)
            {
                int y = figure.UperLeftCorner.Y + 1;
                for (int j = 0; j < 4; j++)
                {
                    if (figure.Form[i, j])
                    {
                        Console.SetCursorPosition(y, x);
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(symbol);
                    }
                    y++;
                }
                x++;
            }
        }
Example #4
0
        public void Run()
        {
            Print();

            while (isRuning)
            {
                int removedLines = 0;

                int newLevel = UpdateLevel(score);
                if (newLevel > level)
                {
                    sleepTime -= 20;
                }

                while (Console.KeyAvailable)
                {
                    ConsoleKeyInfo keyPressed = Console.ReadKey();

                    switch (keyPressed.Key)
                    {
                        case ConsoleKey.Spacebar:
                            ConsoleKeyInfo pausePressed = Console.ReadKey();
                            if (pausePressed.Key == ConsoleKey.Spacebar)
                            {
                                continue;
                            }
                            break;
                        case ConsoleKey.LeftArrow:
                            currentFigure.Left();
                            if (CheckCollision(board, currentFigure))
                            {
                                currentFigure.Right();
                                continue;
                            }
                            Print();
                            break;
                        case ConsoleKey.RightArrow:
                            currentFigure.Right();
                            if (CheckCollision(board, currentFigure))
                            {
                                currentFigure.Left();
                                continue;
                            }
                            Print();
                            break;
                        case ConsoleKey.UpArrow:
                            currentFigure.Rotate();
                            if (CheckCollision(board, currentFigure))
                            {
                                currentFigure.UnRotate();
                            }
                            Print();
                            break;
                        case ConsoleKey.DownArrow:
                            currentFigure.Down();
                            if (CheckCollision(board, currentFigure))
                            {
                                currentFigure.Up();
                                AddFigureToBoard(board, currentFigure);
                                removedLines = RemoveLines(board);
                                score += UpdateScore(removedLines);
                                Print();

                                currentFigure = nextFigure;
                                nextFigure = GenerateFigure();
                                if (CheckCollision(board, currentFigure))
                                {
                                    currentFigure.Exist = false;
                                    isRuning = false;
                                }
                                continue;

                            }
                            Print();
                            break;
                    }
                }

                Thread.Sleep(sleepTime);
                currentFigure.Down();
                if (CheckCollision(board, currentFigure))
                {
                    currentFigure.Up();
                    AddFigureToBoard(board, currentFigure);
                    removedLines = RemoveLines(board);
                    score += UpdateScore(removedLines);
                    Print();

                    currentFigure = nextFigure;
                    nextFigure = GenerateFigure();

                    if (CheckCollision(board, currentFigure))
                    {
                        isRuning = false;
                    }

                    continue;
                }

                Print();
            }

            Console.SetCursorPosition(1, 4);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("GAME OVER");
            Console.SetCursorPosition(1, 6);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("press space");
        }
Example #5
0
        private static bool CheckCollision(Board board, Figure figure)
        {
            bool haveCollision = false;
            int startRow = figure.UperLeftCorner.X;

            for (int i = 0; i < 4; i++)
            {
                int startCol = figure.UperLeftCorner.Y;
                for (int j = 0; j < 4; j++)
                {
                    if (figure.Form[i, j])
                    {
                        if (startRow < 0 ||
                        startRow > 19 ||
                        startCol < 0 ||
                        startCol > 9 ||
                        board.Form[startRow, startCol]
                        )
                        {
                            haveCollision = true;
                            return haveCollision;
                        }
                    }
                    startCol++;
                }
                startRow++;
            }

            return haveCollision;
        }
Example #6
0
        private static void AddFigureToBoard(Board board, Figure figure)
        {
            int startRow = figure.UperLeftCorner.X;

            for (int i = 0; i < 4; i++)
            {
                int startCol = figure.UperLeftCorner.Y;
                for (int j = 0; j < 4; j++)
                {
                    if (figure.Form[i, j])
                    {
                        board.Form[startRow, startCol] = true;
                    }
                    startCol++;
                }
                startRow++;
            }
        }
Example #7
0
 public EditForm(Figure figure)
 {
     InitializeComponent();
     SetupControls(figure);
 }