Ejemplo n.º 1
0
        //Sets lines, score, level and displays what the next shape will be.

        public void setStats()
        {
            Console.SetCursorPosition(board.getScore().GetCursorPosition().X, board.getScore().GetCursorPosition().Y);
            Console.ForegroundColor = board.getScore().GetTextColor();
            Console.Write(this.score);
            Console.SetCursorPosition(board.getLines().GetCursorPosition().X, board.getLines().GetCursorPosition().Y);
            Console.ForegroundColor = board.getLines().GetTextColor();
            Console.Write(this.lines);
            Console.SetCursorPosition(board.getLevel().GetCursorPosition().X, board.getLevel().GetCursorPosition().Y);
            Console.ForegroundColor = board.getLevel().GetTextColor();
            Console.Write(this.level);
            Console.ResetColor();
            Shape next;

            //if (nextRando == 5) { next = new Shape(board.getNext().GetCursorPosition().X - Block.Width, board.getNext().GetCursorPosition().Y, nextRando); }
            //else { next = new Shape(board.getNext().GetCursorPosition().X, board.getNext().GetCursorPosition().Y, nextRando); }
            switch (nextRando)
            {
            case 0:
                next = new Shape(board.getNext().GetCursorPosition().X + Block.Height, board.getNext().GetCursorPosition().Y - 1, nextRando);
                break;

            case 1:
                next = new Shape(board.getNext().GetCursorPosition().X - Block.Height, board.getNext().GetCursorPosition().Y - 1, nextRando);
                break;

            case 4:
                next = new Shape(board.getNext().GetCursorPosition().X - Block.Height, board.getNext().GetCursorPosition().Y, nextRando);
                break;

            case 5:
                next = new Shape(board.getNext().GetCursorPosition().X - Block.Height, board.getNext().GetCursorPosition().Y + 1, nextRando);
                break;

            default:
                next = new Shape(board.getNext().GetCursorPosition().X, board.getNext().GetCursorPosition().Y, nextRando);
                break;
            }
            next.Arrange();
            next.render();
        }
Ejemplo n.º 2
0
        public bool playGame()
        {
            //Set up game.  Instantiate current block next block and board

            Console.Clear();
            Console.CursorVisible = false;

            board.createBoard();
            board.drawBoard();
            board.getHighScoreBoard().displayScores();

            current = new Shape(board.SpawnPoint.X - Block.Width, board.SpawnPoint.Y, rando.Next(7));
            current.Arrange();
            current.render();

            nextRando = rando.Next(7);

            foreach (Point p in board.getBorders())
            {
                edges.Add(p);
            }

            setStats();

            current.Move(ConsoleKey.DownArrow);

            timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            timer.Enabled  = true;

            ConsoleKey userInput = ConsoleKey.UpArrow;

            //bool notDead = true;



            //-----------Handles all user input---------------


            while (notDead && (userInput == ConsoleKey.UpArrow || userInput == ConsoleKey.DownArrow || userInput == ConsoleKey.LeftArrow || userInput == ConsoleKey.RightArrow))
            {
                while (Console.KeyAvailable == false) //Without this loop control hangs up on the readkey and creates strange behavior in critical section.
                {
                    if (!notDead)
                    {
                        break;
                    }
                }

                if (!notDead)
                {
                    break;
                }

                userInput = Console.ReadKey().Key;

                notDead = processInput(userInput); // Returns a bool determining whether or not the game is over, which governs the loop.
            }

            //Receives user's decision to play or quit after game and returns to Program class.

            ConsoleKey playAgain = gameOver();

            if (playAgain == ConsoleKey.Q)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Ejemplo n.º 3
0
        //-----------------------------------Primary Logic-------------------------------------

        //Processes input from user and events to control game, move blocks, detect collisions.


        public bool processInput(ConsoleKey action)
        {
            bool alive = true;

            if (action == ConsoleKey.UpArrow)
            {
                current.Mutate(edges, state);
            }
            else
            {
                bool hitEdge  = false;
                bool hitBlock = false;


                if (action == ConsoleKey.LeftArrow)
                {
                    hitEdge  = current.checkCollision(edges, Block.Width, 0);
                    hitBlock = current.checkCollision(state, Block.Width, 0);

                    if (!hitEdge && !hitBlock)
                    {
                        current.Move(action);
                    }
                }

                if (action == ConsoleKey.RightArrow)
                {
                    hitEdge  = current.checkCollision(edges, -Block.Width, 0);
                    hitBlock = current.checkCollision(state, -Block.Width, 0);

                    if (!hitEdge && !hitBlock)
                    {
                        current.Move(action);
                    }
                }



                //-----------------------Critical Section-----------------------
                //Both the timer and user input threads access this code and change state

                lock (threadLock)
                {
                    if (action == ConsoleKey.DownArrow)
                    {
                        hitEdge  = current.checkCollision(edges, 0, -Block.Height);
                        hitBlock = current.checkCollision(state, 0, -Block.Height);

                        if (!hitEdge && !hitBlock)
                        {
                            current.Move(action);
                        }

                        else
                        {
                            if (delay == 1) //Pauses fall for one cycle upon contact with surface below to allow player to slide block under another
                            {
                                foreach (Block blk in current.getBlocks())
                                {
                                    state.Add(blk);
                                }

                                Lines();

                                Console.Clear();

                                foreach (Block b in state)
                                {
                                    b.inflate();
                                    b.draw();
                                }


                                current = new Shape(board.SpawnPoint.X - Block.Width, board.SpawnPoint.Y, nextRando);
                                current.Arrange();
                                current.render();

                                board.drawBoard();

                                nextRando = rando.Next(7);

                                setStats();


                                if (nextLevel >= 2)
                                {
                                    nextLevel = 0;
                                    level++;
                                    levelUp();
                                    board.drawBoard();
                                    setStats();
                                }

                                alive = !current.checkCollision(state, 0, -1);

                                delay = 0;

                                if (!alive)
                                {
                                    notDead = alive;
                                    return(alive);
                                }
                            }

                            else
                            {
                                delay++;
                            }
                        }
                    }
                }

                //-----------------------End Critical Section-----------------------
            }

            return(alive);
        }
Ejemplo n.º 4
0
        public void Start()
        {
            Console.CursorVisible = false;
            Console.Clear();
            Console.BackgroundColor = ConsoleColor.White;
            Console.Clear();

            SplashWriter title = new SplashWriter(5, 5, 1, ConsoleColor.Black);
            SplashWriter label = new SplashWriter(5, 5, 1, ConsoleColor.Black);


            label.WriteWord("TETRIS", ConsoleColor.Black, TextStyles.Calico);

            Console.ReadKey();

            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;

            Console.WriteLine("\n\n     The Presentation");

            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;

            Console.ReadKey();

            currentY = Console.CursorTop;


            string[] ProgramMethods = { "Main()" };
            UML      ProgramUML     = new UML(6, 25, indent, Console.CursorTop + 3, ConsoleColor.DarkGray, ConsoleColor.White, "Program", ProgramMethods);

            ProgramUML.drawScoreboard();
            ProgramUML.displayMethods();

            string[] ShapeMethods = { "Dictionary<int, int[]> configurations", "Block[] blocks", "Block[] blocks", "-----------------------------------", "Render()", "Delete()", "Move()", "Arrange()", "Mutate()", "CheckCollision()", "CheckBlocks()", "CheckPoints()" };
            UML      ShapeUML     = new UML(17, 43, indent, Console.CursorTop + 5, ConsoleColor.DarkGray, ConsoleColor.White, "Shape", ShapeMethods);

            ShapeUML.drawScoreboard();
            ShapeUML.displayMethods();

            string[] blockMethods = { "Inflate()", "Draw()", "Shift()", "Erase()", "CheckEveryPoint()", "CheckEveryBlock()" };
            UML      blockUML     = new UML(11, 25, indent, Console.CursorTop + 5, ConsoleColor.DarkGray, ConsoleColor.White, "Block", blockMethods);

            blockUML.drawScoreboard();
            blockUML.displayMethods();

            string[] GameMethods = { "PlayGame()", "ProcessInput()", "Lines()", "DebrisFall()", "GameOver()", "SetStats()", "LevelUp()", "UponMyDeath()" };
            UML      GameUML     = new UML(13, 25, indent * 3 + ShapeUML.Width, currentY + 3, ConsoleColor.DarkGray, ConsoleColor.White, "Game", GameMethods);

            GameUML.drawScoreboard();
            GameUML.displayMethods();

            string[] BoardMethods = { "CreateBoard()", "DrawBoard()" };
            UML      BoardUML     = new UML(7, 25, indent * 3 + ShapeUML.Width, Console.CursorTop + 5, ConsoleColor.DarkGray, ConsoleColor.White, "Board", BoardMethods);

            BoardUML.drawScoreboard();
            BoardUML.displayMethods();

            string[] DictionaryShapes   = { "Dictionary<int, int[]> zero", "Dictionary<int, int[]> one", "Dictionary<int, int[]> two", "Dictionary<int, int[]> three", "Dictionary<int, int[]> four", "Dictionary<int, int[]> five", "Dictionary<int, int[]> six", };
            UML      ShapeDictionaryUML = new UML(12, 34, indent * 3 + ShapeUML.Width, Console.CursorTop + 5, ConsoleColor.DarkGray, ConsoleColor.White, "ShapeDictionary", DictionaryShapes);

            ShapeDictionaryUML.drawScoreboard();
            ShapeDictionaryUML.displayMethods();

            string[] textFiles = { "HighScores.txt", "HighScorers.txt" };
            UML      TextFiles = new UML(7, 25, indent * 5 + ShapeUML.Width + ShapeDictionaryUML.Width, currentY + 3, ConsoleColor.DarkGray, ConsoleColor.White, "Highscores Text", textFiles);

            TextFiles.drawScoreboard();
            TextFiles.displayMethods();

            string[] SplashMethods = { "WriteWord()", "WriteLetter()", "WriteWord()", "DrawBackground()", "WritePixel()", "DrawR" };
            UML      SplashUML     = new UML(11, 25, indent * 5 + ShapeUML.Width + ShapeDictionaryUML.Width, Console.CursorTop + 5, ConsoleColor.DarkGray, ConsoleColor.White, "SplashWriter", SplashMethods);

            SplashUML.drawScoreboard();
            SplashUML.displayMethods();

            Console.ReadKey();

            Console.BackgroundColor = ConsoleColor.White;
            Console.Clear();

            title.WriteWord("TETRIS", ConsoleColor.Black, TextStyles.Calico);



            //title.Y = 17;
            //title.BackGround = ConsoleColor.White;
            //title.FontWidth = 1;
            //title.WriteWord("The Presentation");
            //Console.ResetColor();
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.CursorVisible   = false;

            Console.ReadKey();


            Shape bullet = new Shape(indent - 2, Console.CursorTop + 4, 5);

            bullet.Arrange();
            bullet.render();

            title.X          = indent * 3;
            title.Y          = Console.CursorTop - 3;
            title.BackGround = ConsoleColor.White;
            title.FontWidth  = 1;
            title.Padding    = -4;
            title.YMargin    = 0;
            title.WriteWord("STATE");


            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;

            Console.SetCursorPosition(indent * 4, Console.CursorTop + 2);
            Console.WriteLine("Shape = Block[]");
            Console.SetCursorPosition(indent * 4, Console.CursorTop);
            Console.WriteLine("Block = Point[]");
            Console.SetCursorPosition(indent * 4, Console.CursorTop);
            Console.WriteLine("Each point represents a character space on the console, and has a unique x and y.");
            Console.ReadKey();

            Console.WriteLine();
            Console.WriteLine();

            Shape bullet2 = new Shape(indent - 2, Console.CursorTop + 1, 4);

            bullet2.Arrange();
            bullet2.render();

            title.X          = indent * 3;
            title.Y          = Console.CursorTop - 4;
            title.BackGround = ConsoleColor.White;
            title.FontWidth  = 1;
            title.Padding    = -4;
            title.YMargin    = 0;
            title.WriteWord("THXEAD_SAFE");

            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;


            Console.SetCursorPosition(indent * 4, Console.CursorTop + 2);
            Console.WriteLine("Game uses a timer to cause the shape to fall and trigger other game events");
            Console.SetCursorPosition(indent * 4, Console.CursorTop);
            Console.WriteLine("User input can also cause the shape to move and trigger events");
            Console.ReadKey();
            Console.SetCursorPosition(indent * 4, Console.CursorTop + 1);
            Console.WriteLine("  -  Unusual behavior.");
            Console.ReadKey();
            Console.SetCursorPosition(indent * 4, Console.CursorTop);
            Console.WriteLine("  -  Difficult to Debug.");
            Console.ReadKey();
            Console.SetCursorPosition(indent * 4, Console.CursorTop + 1);
            Console.WriteLine("Critical Section / Lock");
            Console.ReadKey();


            Shape bullet3 = new Shape(indent - 2, Console.CursorTop + 3, 3);

            bullet3.Arrange();
            bullet3.render();

            title.X          = indent * 3;
            title.Y          = Console.CursorTop - 4;
            title.BackGround = ConsoleColor.White;
            title.FontWidth  = 1;
            title.Padding    = -4;
            title.YMargin    = 0;
            title.WriteWord("MOXPH...?");

            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;


            Console.SetCursorPosition(indent * 4, Console.CursorTop + 2);
            Console.WriteLine("Polymorphism vs Dictionary");
            Console.SetCursorPosition(indent * 4, Console.CursorTop);
            Console.WriteLine("Create abstract shape class and a class for each shape");
            Console.SetCursorPosition(indent * 4, Console.CursorTop);
            Console.WriteLine("Or some other way");
            Console.ReadKey();
            Console.ResetColor();
            Console.Clear();
            Console.ReadKey();
        }