예제 #1
0
        /// <summary>Main routine that controls game play</summary>
        private static void PlayNibbles(int NumPlayers, int speed, string diff)
        {
            // Initialize Snakes
            SnakeBody[,] sammyBody = new SnakeBody[MAXSNAKELENGTH, 2];
            SnakeType[] sammy = new SnakeType[2];
            sammy[0] = new SnakeType { lives = 5, score = 0, scolor = colorTable[0] };
            sammy[1] = new SnakeType { lives = 5, score = 0, scolor = colorTable[1] };

            Level(STARTOVER, sammy);
            var startRow1 = sammy[0].row;
            var startCol1 = sammy[0].col;
            var startRow2 = sammy[1].row;
            var startCol2 = sammy[1].col;

            var curSpeed = speed;

            // play Nibbles until finished

            SpacePause("     Level " + curLevel + ",  Push Space");

            do
            {
                if (NumPlayers == 1)
                    sammy[1].row = 0;

                var number = 1; // Current number that snakes are trying to run into
                var nonum = true; // nonum = TRUE if a number is not on the screen
                int numberRow = 0, NumberCol = 0, sisterRow = 0;

                var playerDied = false;
                PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives);
                // PLAY "T160O1>L20CDEDCDL10ECC"

                do
                {
                    // Print number if no number exists
                    if (nonum)
                    {
                        do
                        {
                            numberRow = (int)(Rnd.NextDouble() * 47 + 3);
                            NumberCol = (int)(Rnd.NextDouble() * 78 + 2);
                            sisterRow = numberRow + arena[numberRow, NumberCol].sister;
                        }
                        while (PointIsThere(numberRow, NumberCol, colorTable[3]) || PointIsThere(sisterRow, NumberCol, colorTable[3]));
                        numberRow = arena[numberRow, NumberCol].realRow;
                        nonum = false;
                        Console.ForegroundColor = (ConsoleColor)colorTable[0];
                        Console.BackgroundColor = (ConsoleColor)colorTable[3];
                        ConsoleSetCursorPosition(NumberCol, numberRow);
                        Console.Write(number.ToString().Last());
                    }

                    // Delay game
                    Thread.Sleep(curSpeed);

                    // Get keyboard input & Change direction accordingly
                    if (Console.KeyAvailable)
                    {
                        var kbd = Console.ReadKey(true).Key;
                        switch (kbd)
                        {
                            case ConsoleKey.W: if (sammy[1].direction != 2) sammy[1].direction = 1; break;
                            case ConsoleKey.S: if (sammy[1].direction != 1) sammy[1].direction = 2; break;
                            case ConsoleKey.A: if (sammy[1].direction != 4) sammy[1].direction = 3; break;
                            case ConsoleKey.D: if (sammy[1].direction != 3) sammy[1].direction = 4; break;
                            case ConsoleKey.UpArrow: if (sammy[0].direction != 2) sammy[0].direction = 1; break;
                            case ConsoleKey.DownArrow: if (sammy[0].direction != 1) sammy[0].direction = 2; break;
                            case ConsoleKey.LeftArrow: if (sammy[0].direction != 4) sammy[0].direction = 3; break;
                            case ConsoleKey.RightArrow: if (sammy[0].direction != 3) sammy[0].direction = 4; break;
                            case ConsoleKey.Spacebar: case ConsoleKey.P: SpacePause(" Game Paused ... Push Space  "); break;
                            default: break;
                        }
                    }

                    for (int a = 0; a < NumPlayers; a++)
                    {
                        // Move Snake
                        switch (sammy[a].direction)
                        {
                            case 1: sammy[a].row = sammy[a].row - 1; break;
                            case 2: sammy[a].row = sammy[a].row + 1; break;
                            case 3: sammy[a].col = sammy[a].col - 1; break;
                            case 4: sammy[a].col = sammy[a].col + 1; break;
                        }

                        // If snake hits number, respond accordingly
                        if (numberRow == (sammy[a].row + 1) / 2 && NumberCol == sammy[a].col)
                        {
                            // PLAY "MBO0L16>CCCE"
                            if (sammy[a].length < MAXSNAKELENGTH - 30)
                                sammy[a].length = sammy[a].length + number * 4;

                            sammy[a].score = sammy[a].score + number;
                            PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives);
                            number = number + 1;
                            if (number == 10)
                            {
                                EraseSnake(sammy, sammyBody, 0);
                                EraseSnake(sammy, sammyBody, 1);
                                ConsoleSetCursorPosition(NumberCol, numberRow);
                                Console.Write(" ");
                                Level(NEXTLEVEL, sammy);
                                PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives);
                                SpacePause("     Level " + curLevel + ",  Push Space");
                                if (NumPlayers == 1)
                                    sammy[1].row = 0;
                                number = 1;
                                if (diff == "P")
                                {
                                    speed = speed - 10;
                                    curSpeed = speed;
                                }
                            }
                            nonum = true;
                            if (curSpeed < 1)
                                curSpeed = 1;
                        }
                    }

                    for (int a = 0; a < NumPlayers; a++)
                    {
                        // If player runs into any point, or the head of the other snake, it dies.
                        if (PointIsThere(sammy[a].row, sammy[a].col, colorTable[3]) || (sammy[0].row == sammy[1].row && sammy[0].col == sammy[1].col))
                        {
                            // PLAY "MBO0L32EFGEFDC"
                            Console.BackgroundColor = (ConsoleColor)colorTable[3];
                            ConsoleSetCursorPosition(NumberCol, numberRow);
                            Console.Write(" ");
                            playerDied = true;
                            sammy[a].alive = false;
                            sammy[a].lives = sammy[a].lives - 1;
                        }
                        // Otherwise, move the snake, and erase the tail
                        else
                        {
                            sammy[a].head = (sammy[a].head + 1) % MAXSNAKELENGTH;
                            sammyBody[sammy[a].head, a].row = sammy[a].row;
                            sammyBody[sammy[a].head, a].col = sammy[a].col;
                            var tail = (sammy[a].head + MAXSNAKELENGTH - sammy[a].length) % MAXSNAKELENGTH;
                            Set(sammyBody[tail, a].row, sammyBody[tail, a].col, colorTable[3]);
                            sammyBody[tail, a].row = 0;
                            Set(sammy[a].row, sammy[a].col, sammy[a].scolor);
                        }
                    }
                }
                while (!playerDied);

                // reset speed to initial value
                curSpeed = speed;

                for (int a = 0; a < NumPlayers; a++)
                {
                    EraseSnake(sammy, sammyBody, a);

                    // If dead, then erase snake in really cool way
                    if (!sammy[a].alive)
                    {
                        // Update score
                        sammy[a].score = sammy[a].score - 10;
                        PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives);

                        if (a == 0)
                            SpacePause(" Sammy Dies! Push Space! --->");
                        else
                            SpacePause(" <---- Jake Dies! Push Space ");
                    }
                }

                Level(SAMELEVEL, sammy);
                PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives);

                // Play next round, until either of snake's lives have run out.
            }
            while (sammy[0].lives != 0 && sammy[1].lives != 0);
        }
예제 #2
0
 /// <summary>Erases snake to facilitate moving through playing field</summary>
 private static void EraseSnake(SnakeType[] snake, SnakeBody[,] snakeBod, int snakeNum)
 {
     for (int c = 0; c < 9; c++)
         for (int b = snake[snakeNum].length - c; b >= 0; b -= 10)
         {
             var tail = (snake[snakeNum].head + MAXSNAKELENGTH - b) % MAXSNAKELENGTH;
             Set(snakeBod[tail, snakeNum].row, snakeBod[tail, snakeNum].col, colorTable[3]);
             Thread.Sleep(2);
         }
 }
예제 #3
0
        /// <summary>Sets game level</summary>
        private static void Level(int WhatToDO, SnakeType[] sammy)
        {
            switch (WhatToDO)
            {
                case STARTOVER:
                    curLevel = 1;
                    break;

                case NEXTLEVEL:
                    curLevel = curLevel + 1;
                    break;
            }

            // Initialize Snakes
            sammy[0].head = 1;
            sammy[0].length = 2;
            sammy[0].alive = true;
            sammy[1].head = 1;
            sammy[1].length = 2;
            sammy[1].alive = true;

            InitColors();

            switch (curLevel)
            {
                case 1:
                    sammy[0].row = 25;
                    sammy[1].row = 25;
                    sammy[0].col = 50;
                    sammy[1].col = 30;
                    sammy[0].direction = 4;
                    sammy[1].direction = 3;
                    break;

                case 2:
                    for (int i = 20; i <= 60; i++)
                        Set(25, i, colorTable[2]);
                    sammy[0].row = 7;
                    sammy[1].row = 43;
                    sammy[0].col = 60;
                    sammy[1].col = 20;
                    sammy[0].direction = 3;
                    sammy[1].direction = 4;
                    break;

                case 3:
                    for (int i = 10; i <= 40; i++)
                    {
                        Set(i, 20, colorTable[2]);
                        Set(i, 60, colorTable[2]);
                    }
                    sammy[0].row = 25;
                    sammy[1].row = 25;
                    sammy[0].col = 50;
                    sammy[1].col = 30;
                    sammy[0].direction = 1;
                    sammy[1].direction = 2;
                    break;

                case 4:
                    for (int i = 4; i <= 30; i++)
                    {
                        Set(i, 20, colorTable[2]);
                        Set(53 - i, 60, colorTable[2]);
                    }
                    for (int i = 2; i <= 40; i++)
                    {
                        Set(38, i, colorTable[2]);
                        Set(15, 81 - i, colorTable[2]);
                    }
                    sammy[0].row = 7;
                    sammy[1].row = 43;
                    sammy[0].col = 60;
                    sammy[1].col = 20;
                    sammy[0].direction = 3;
                    sammy[1].direction = 4;
                    break;

                case 5:
                    for (int i = 13; i <= 39; i++)
                    {
                        Set(i, 21, colorTable[2]);
                        Set(i, 59, colorTable[2]);
                    }
                    for (int i = 23; i <= 57; i++)
                    {
                        Set(11, i, colorTable[2]);
                        Set(41, i, colorTable[2]);
                    }
                    sammy[0].row = 25;
                    sammy[1].row = 25;
                    sammy[0].col = 50;
                    sammy[1].col = 30;
                    sammy[0].direction = 1;
                    sammy[1].direction = 2;
                    break;

                case 6:
                    for (int i = 4; i <= 49; i++)
                    {
                        if (i > 30 || i < 23)
                        {
                            Set(i, 10, colorTable[2]);
                            Set(i, 20, colorTable[2]);
                            Set(i, 30, colorTable[2]);
                            Set(i, 40, colorTable[2]);
                            Set(i, 50, colorTable[2]);
                            Set(i, 60, colorTable[2]);
                            Set(i, 70, colorTable[2]);
                        }
                    }
                    sammy[0].row = 7;
                    sammy[1].row = 43;
                    sammy[0].col = 65;
                    sammy[1].col = 15;
                    sammy[0].direction = 2;
                    sammy[1].direction = 1;
                    break;

                case 7:
                    for (int i = 4; i <= 49; i += 2)
                        Set(i, 40, colorTable[2]);
                    sammy[0].row = 7;
                    sammy[1].row = 43;
                    sammy[0].col = 65;
                    sammy[1].col = 15;
                    sammy[0].direction = 2;
                    sammy[1].direction = 1;
                    break;

                case 8:
                    for (int i = 4; i <= 40; i++)
                    {
                        Set(i, 10, colorTable[2]);
                        Set(53 - i, 20, colorTable[2]);
                        Set(i, 30, colorTable[2]);
                        Set(53 - i, 40, colorTable[2]);
                        Set(i, 50, colorTable[2]);
                        Set(53 - i, 60, colorTable[2]);
                        Set(i, 70, colorTable[2]);
                    }
                    sammy[0].row = 7;
                    sammy[1].row = 43;
                    sammy[0].col = 65;
                    sammy[1].col = 15;
                    sammy[0].direction = 2;
                    sammy[1].direction = 1;
                    break;

                case 9:
                    for (int i = 6; i <= 47; i++)
                    {
                        Set(i, i, colorTable[2]);
                        Set(i, i + 28, colorTable[2]);
                    }
                    sammy[0].row = 40;
                    sammy[1].row = 15;
                    sammy[0].col = 75;
                    sammy[1].col = 5;
                    sammy[0].direction = 1;
                    sammy[1].direction = 2;
                    break;

                default:
                    for (int i = 4; i <= 49; i += 2)
                    {
                        Set(i, 10, colorTable[2]);
                        Set(i + 1, 20, colorTable[2]);
                        Set(i, 30, colorTable[2]);
                        Set(i + 1, 40, colorTable[2]);
                        Set(i, 50, colorTable[2]);
                        Set(i + 1, 60, colorTable[2]);
                        Set(i, 70, colorTable[2]);
                    }
                    sammy[0].row = 7;
                    sammy[1].row = 43;
                    sammy[0].col = 65;
                    sammy[1].col = 15;
                    sammy[0].direction = 2;
                    sammy[1].direction = 1;
                    break;
            }
        }
예제 #4
0
        /// <summary>Main routine that controls game play</summary>
        private static void PlayNibbles(int NumPlayers, int speed, string diff)
        {
            // Initialize Snakes
            SnakeBody[,] sammyBody = new SnakeBody[MAXSNAKELENGTH, 2];
            SnakeType[] sammy = new SnakeType[2];
            sammy[0] = new SnakeType {
                lives = 5, score = 0, scolor = colorTable[0]
            };
            sammy[1] = new SnakeType {
                lives = 5, score = 0, scolor = colorTable[1]
            };

            Level(STARTOVER, sammy);
            var startRow1 = sammy[0].row;
            var startCol1 = sammy[0].col;
            var startRow2 = sammy[1].row;
            var startCol2 = sammy[1].col;

            var curSpeed = speed;

            // play Nibbles until finished

            SpacePause("     Level " + curLevel + ",  Push Space");

            do
            {
                if (NumPlayers == 1)
                {
                    sammy[1].row = 0;
                }

                var number = 1;   // Current number that snakes are trying to run into
                var nonum = true; // nonum = TRUE if a number is not on the screen
                int numberRow = 0, NumberCol = 0, sisterRow = 0;

                var playerDied = false;
                PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives);
                // PLAY "T160O1>L20CDEDCDL10ECC"

                do
                {
                    // Print number if no number exists
                    if (nonum)
                    {
                        do
                        {
                            numberRow = (int)(Rnd.NextDouble() * 47 + 3);
                            NumberCol = (int)(Rnd.NextDouble() * 78 + 2);
                            sisterRow = numberRow + arena[numberRow, NumberCol].sister;
                        }while (PointIsThere(numberRow, NumberCol, colorTable[3]) || PointIsThere(sisterRow, NumberCol, colorTable[3]));
                        numberRow = arena[numberRow, NumberCol].realRow;
                        nonum     = false;
                        Console.ForegroundColor = (ConsoleColor)colorTable[0];
                        Console.BackgroundColor = (ConsoleColor)colorTable[3];
                        ConsoleSetCursorPosition(NumberCol, numberRow);
                        Console.Write(number.ToString().Last());
                    }

                    // Delay game
                    Thread.Sleep(curSpeed);

                    // Get keyboard input & Change direction accordingly
                    if (Console.KeyAvailable)
                    {
                        var kbd = Console.ReadKey(true).Key;
                        switch (kbd)
                        {
                        case ConsoleKey.W: if (sammy[1].direction != 2)
                            {
                                sammy[1].direction = 1;
                            }
                            break;

                        case ConsoleKey.S: if (sammy[1].direction != 1)
                            {
                                sammy[1].direction = 2;
                            }
                            break;

                        case ConsoleKey.A: if (sammy[1].direction != 4)
                            {
                                sammy[1].direction = 3;
                            }
                            break;

                        case ConsoleKey.D: if (sammy[1].direction != 3)
                            {
                                sammy[1].direction = 4;
                            }
                            break;

                        case ConsoleKey.UpArrow: if (sammy[0].direction != 2)
                            {
                                sammy[0].direction = 1;
                            }
                            break;

                        case ConsoleKey.DownArrow: if (sammy[0].direction != 1)
                            {
                                sammy[0].direction = 2;
                            }
                            break;

                        case ConsoleKey.LeftArrow: if (sammy[0].direction != 4)
                            {
                                sammy[0].direction = 3;
                            }
                            break;

                        case ConsoleKey.RightArrow: if (sammy[0].direction != 3)
                            {
                                sammy[0].direction = 4;
                            }
                            break;

                        case ConsoleKey.Spacebar:
                        case ConsoleKey.P: SpacePause(" Game Paused ... Push Space  "); break;

                        default: break;
                        }
                    }

                    for (int a = 0; a < NumPlayers; a++)
                    {
                        // Move Snake
                        switch (sammy[a].direction)
                        {
                        case 1: sammy[a].row = sammy[a].row - 1; break;

                        case 2: sammy[a].row = sammy[a].row + 1; break;

                        case 3: sammy[a].col = sammy[a].col - 1; break;

                        case 4: sammy[a].col = sammy[a].col + 1; break;
                        }

                        // If snake hits number, respond accordingly
                        if (numberRow == (sammy[a].row + 1) / 2 && NumberCol == sammy[a].col)
                        {
                            // PLAY "MBO0L16>CCCE"
                            if (sammy[a].length < MAXSNAKELENGTH - 30)
                            {
                                sammy[a].length = sammy[a].length + number * 4;
                            }

                            sammy[a].score = sammy[a].score + number;
                            PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives);
                            number = number + 1;
                            if (number == 10)
                            {
                                EraseSnake(sammy, sammyBody, 0);
                                EraseSnake(sammy, sammyBody, 1);
                                ConsoleSetCursorPosition(NumberCol, numberRow);
                                Console.Write(" ");
                                Level(NEXTLEVEL, sammy);
                                PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives);
                                SpacePause("     Level " + curLevel + ",  Push Space");
                                if (NumPlayers == 1)
                                {
                                    sammy[1].row = 0;
                                }
                                number = 1;
                                if (diff == "P")
                                {
                                    speed    = speed - 10;
                                    curSpeed = speed;
                                }
                            }
                            nonum = true;
                            if (curSpeed < 1)
                            {
                                curSpeed = 1;
                            }
                        }
                    }

                    for (int a = 0; a < NumPlayers; a++)
                    {
                        // If player runs into any point, or the head of the other snake, it dies.
                        if (PointIsThere(sammy[a].row, sammy[a].col, colorTable[3]) || (sammy[0].row == sammy[1].row && sammy[0].col == sammy[1].col))
                        {
                            // PLAY "MBO0L32EFGEFDC"
                            Console.BackgroundColor = (ConsoleColor)colorTable[3];
                            ConsoleSetCursorPosition(NumberCol, numberRow);
                            Console.Write(" ");
                            playerDied     = true;
                            sammy[a].alive = false;
                            sammy[a].lives = sammy[a].lives - 1;
                        }
                        // Otherwise, move the snake, and erase the tail
                        else
                        {
                            sammy[a].head = (sammy[a].head + 1) % MAXSNAKELENGTH;
                            sammyBody[sammy[a].head, a].row = sammy[a].row;
                            sammyBody[sammy[a].head, a].col = sammy[a].col;
                            var tail = (sammy[a].head + MAXSNAKELENGTH - sammy[a].length) % MAXSNAKELENGTH;
                            Set(sammyBody[tail, a].row, sammyBody[tail, a].col, colorTable[3]);
                            sammyBody[tail, a].row = 0;
                            Set(sammy[a].row, sammy[a].col, sammy[a].scolor);
                        }
                    }
                }while (!playerDied);

                // reset speed to initial value
                curSpeed = speed;

                for (int a = 0; a < NumPlayers; a++)
                {
                    EraseSnake(sammy, sammyBody, a);

                    // If dead, then erase snake in really cool way
                    if (!sammy[a].alive)
                    {
                        // Update score
                        sammy[a].score = sammy[a].score - 10;
                        PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives);

                        if (a == 0)
                        {
                            SpacePause(" Sammy Dies! Push Space! --->");
                        }
                        else
                        {
                            SpacePause(" <---- Jake Dies! Push Space ");
                        }
                    }
                }

                Level(SAMELEVEL, sammy);
                PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives);

                // Play next round, until either of snake's lives have run out.
            }while (sammy[0].lives != 0 && sammy[1].lives != 0);
        }