示例#1
0
        static void Main(string[] args)
        {
            Console.SetCursorPosition(Console.WindowWidth / 2 - 3, Console.WindowHeight / 2 - 5);
            Console.WriteLine("Snake");

            //create users
            UserManagement userlist = new UserManagement();

            Console.SetCursorPosition(Console.WindowWidth / 2 - 15, Console.WindowHeight - 10);
            Console.WriteLine("Please enter your username: "******"Please enter your username: "******"Please select difficulty: [1]Easy ; [2]Normal ; [3]Hard");
            Console.SetCursorPosition(Console.WindowWidth / 2 - 27, Console.WindowHeight - 14);
            string difficulty = Console.ReadLine();

            while (difficulty != "1" && difficulty != "2" && difficulty != "3")
            {
                Console.Clear();
                Console.SetCursorPosition(Console.WindowWidth / 2 - 27, Console.WindowHeight - 15);
                Console.WriteLine("Please select difficulty: [1]Easy ; [2]Normal ; [3]Hard");
                Console.SetCursorPosition(Console.WindowWidth / 2 - 27, Console.WindowHeight - 14);
                difficulty = Console.ReadLine();
            }
            Console.Clear();

            //help
            var path = "help.txt";

            using (StreamReader file = new StreamReader(path))
            {
                string ln;
                while ((ln = file.ReadLine()) != null)
                {
                    Console.WriteLine(ln);
                }
            }

            //press enter to continue
            Console.WriteLine("\n Press ENTER to continue");
            ConsoleKeyInfo conKey = Console.ReadKey();

            while (conKey.Key != ConsoleKey.Enter)
            {
                conKey = Console.ReadKey();
            }
            Console.Clear();

            // initialize background music
            System.Media.SoundPlayer bgm = new System.Media.SoundPlayer();
            if (difficulty == "1")
            {
                bgm.SoundLocation = "../../sound/first.wav";
                bgm.Play();
            }
            else if (difficulty == "2")
            {
                bgm.SoundLocation = "../../sound/second.wav";
                bgm.Play();
            }
            else
            {
                bgm.SoundLocation = "../../sound/third.wav";
                bgm.Play();
            }

            byte right = 0;
            byte left  = 1;
            byte down  = 2;
            byte up    = 3;

            int lastFoodTime      = 0;
            int foodDissapearTime = 16000;
            //bool isGameOver = false;
            Stopwatch stopwatch = new Stopwatch();

            Position[] directions = new Position[]
            {
                new Position(0, 1),  // right
                new Position(0, -1), // left
                new Position(1, 0),  // down
                new Position(-1, 0), // up
            };

            //change sleepTime parameter based on difficulty
            Console.BufferHeight = Console.WindowHeight;
            double sleepTime;

            if (difficulty == "1")
            {
                sleepTime = 100;
            }
            else if (difficulty == "2")
            {
                sleepTime = 70;
            }
            else
            {
                sleepTime = 30;
            }
            lastFoodTime = Environment.TickCount;

            // Initialize obstacle and draw obstacles
            int obsNum;

            if (difficulty == "1")
            {
                obsNum = 5;
            }
            else if (difficulty == "2")
            {
                obsNum = 10;
            }
            else
            {
                obsNum = 30;
            }
            Obstacle obs = new Obstacle(obsNum);


            // Iniatitlize food and draw food
            Food food = new Food();

            food.Generate_random_food();

            // Initialize snake and draw snake
            Snake snake = new Snake();

            snake.DrawSnake();
            int direct = right;

            // Begin timing.
            stopwatch.Start();

            // looping
            while (true)
            {
                // Set the score at the top right
                Console.SetCursorPosition(Console.WindowWidth - 10, Console.WindowHeight - 30);
                Console.WriteLine("Score: " + user.getScore);
                // check for key pressed
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo userInput = Console.ReadKey(true);   //hides users input

                    if (userInput.Key == ConsoleKey.LeftArrow)
                    {
                        if (direct != right)
                        {
                            direct = left;
                        }
                    }
                    if (userInput.Key == ConsoleKey.RightArrow)
                    {
                        if (direct != left)
                        {
                            direct = right;
                        }
                    }
                    if (userInput.Key == ConsoleKey.UpArrow)
                    {
                        if (direct != down)
                        {
                            direct = up;
                        }
                    }
                    if (userInput.Key == ConsoleKey.DownArrow)
                    {
                        if (direct != up)
                        {
                            direct = down;
                        }
                    }
                }

                // update snake position
                Position snakeHead     = snake.GetPos.Last();
                Position nextDirection = directions[direct];
                Position snakeNewHead  = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col);

                // check for snake if exceed the width or height
                // boundary is WindowWidth+11 to WindowWidth-11
                if (snakeNewHead.col < 11)
                {
                    snakeNewHead.col = Console.WindowWidth - 11;
                }
                else if (snakeNewHead.row < 0)
                {
                    snakeNewHead.row = Console.WindowHeight - 1;
                }
                else if (snakeNewHead.row >= Console.WindowHeight)
                {
                    snakeNewHead.row = 0;
                }
                else if (snakeNewHead.col >= Console.WindowWidth - 11)
                {
                    snakeNewHead.col = 11;
                }


                // check for snake collison with self or obstacles
                if (snake.GetPos.Contains(snakeNewHead) || obs.GetObsPos.Contains(snakeHead))
                {
                    bgm.Stop();
                    System.Media.SoundPlayer crash = new System.Media.SoundPlayer();
                    crash.SoundLocation = "../../sound/crash.wav";
                    crash.Play();
                    Console.Clear();
                    Console.SetCursorPosition(Console.WindowWidth / 2 - 5, Console.WindowHeight / 2);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Game over!");
                    Console.WriteLine("\n Press ENTER to quit");
                    ConsoleKeyInfo keyInfo = Console.ReadKey();
                    while (keyInfo.Key != ConsoleKey.Enter)
                    {
                        keyInfo = Console.ReadKey();
                    }
                    return;
                }

                // check for collision with the food
                if ((snakeNewHead.col == food.x && snakeNewHead.row == food.y) || (snakeNewHead.col == food.x + 1 && snakeNewHead.row == food.y))
                {
                    System.Media.SoundPlayer eat = new System.Media.SoundPlayer();
                    eat.SoundLocation = "../../sound/coin.wav";
                    eat.Play();
                    user.ScoreIncrement(1);
                    Console.SetCursorPosition(Console.WindowWidth - 10, Console.WindowHeight - 30);
                    Console.WriteLine("Score: " + user.getScore);

                    //spawn new food while increasing the length of the snake
                    Console.SetCursorPosition(food.x, food.y);
                    Console.Write("  ");
                    food = new Food();
                    food.Generate_random_food();
                    snake.AddSnake();
                    bgm.Play();
                }

                // draw the snake
                Console.SetCursorPosition(snakeHead.col, snakeHead.row);
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.Write("*");

                // moving
                snake.GetPos.Enqueue(snakeNewHead);
                Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
                Console.ForegroundColor = ConsoleColor.Gray;

                // draw the snake head
                if (direct == right)
                {
                    Console.Write(">");
                }
                if (direct == left)
                {
                    Console.Write("<");
                }
                if (direct == up)
                {
                    Console.Write("^");
                }
                if (direct == down)
                {
                    Console.Write("v");
                }

                // moving...
                Position last = snake.GetPos.Dequeue();
                Console.SetCursorPosition(last.col, last.row);
                Console.Write(" ");

                //change score needed to clear the game based on the difficulty
                int goal;
                if (difficulty == "1")
                {
                    goal = 5;
                }
                else if (difficulty == "2")
                {
                    goal = 10;
                }
                else
                {
                    goal = 20;
                }

                // set winning condition score
                if (user.getScore == goal)
                {
                    Console.Clear();
                    Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Stage Clear!");
                    userlist.AddUser(user);

                    // Stop timing.
                    stopwatch.Stop();
                    double ClearTime = stopwatch.Elapsed.TotalSeconds;
                    user.getTime = ClearTime;

                    //Record and display users data
                    var x = userlist.getUsers;
                    userlist.recordUser();
                    userlist.sortRecord();
                    Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2 - 12);
                    Console.WriteLine("High Score: ");
                    Console.SetCursorPosition(Console.WindowWidth / 2 - 10, Console.WindowHeight / 2 - 11);
                    Console.WriteLine("Time          Name    Score");
                    userlist.readRecord();

                    //Press enter to quit
                    Console.WriteLine("\n Press ENTER to quit");
                    ConsoleKeyInfo keyInfo = Console.ReadKey();
                    while (keyInfo.Key != ConsoleKey.Enter)
                    {
                        keyInfo = Console.ReadKey();
                    }
                    System.Environment.Exit(0);
                    return;
                }

                // food timer
                if (Environment.TickCount - lastFoodTime >= foodDissapearTime)
                {
                    Console.SetCursorPosition(food.x, food.y);
                    Console.Write("  ");
                    food = new Food();
                    food.Generate_random_food();

                    lastFoodTime = Environment.TickCount;
                }

                sleepTime -= 0.01;
                Thread.Sleep((int)sleepTime);
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            bool events    = false;
            int  countdown = 9;
            int  goal;

            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.SoundLocation = "../../music/backgroundmusic.wav";
            player.Play();

            Menu menu = new Menu();

            menu.DrawMenu();
            Console.CursorVisible = false;
            ConsoleKeyInfo userinput = Console.ReadKey();

            while (userinput.Key != ConsoleKey.Enter)
            {
                menu.SelectDiff(userinput);
                menu.DrawMenu();

                userinput = Console.ReadKey();
            }
            Console.Clear();
            Console.SetWindowSize(120, 30);

            if (menu.GetDiff() == 0)
            {
                goal = 1000;
            }
            else if (menu.GetDiff() == 1)
            {
                goal = 2000;
            }
            else
            {
                goal = 4000;
            }

            //Create a snake object
            Snake snake = new Snake();

            //Create obstacles object
            Obstacle obstacle = new Obstacle();

            //Create a Food Object
            Food food = new Food();

            //Extra Food
            int      z     = 1;
            Position extra = food.GetExFood();
            bool     spawn = false;

            int lastFoodTime = 0;

            int foodDissapearTime = 14000; //extended the time

            if (menu.GetDiff() == 1)
            {
                foodDissapearTime = 10000;
            }
            else if (menu.GetDiff() == 2)
            {
                foodDissapearTime = 8000;
            }

            int negativePoints = 0;
            int userPoints     = 0;

            double sleepTime = 100;
            Random randomNumbersGenerator  = new Random();
            Random randomNumbersGenerator2 = new Random();
            int    x;
            int    y;

            int SnakeColor;

            //Snake Initial Colour
            SnakeColor           = randomNumbersGenerator.Next(0, 5);
            Console.BufferHeight = Console.WindowHeight;
            lastFoodTime         = Environment.TickCount;

            foreach (Position ob in obstacle.GetPositions())
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.SetCursorPosition(ob.col, ob.row);
                Console.Write("\u2592");
            }

            x = randomNumbersGenerator.Next(2, 30);
            y = randomNumbersGenerator.Next(0, 120);

            Position fd  = food.GetFood();
            Position fd1 = food.Generate(x, y + 1);

            while (snake.GetSnakeElements().Contains(fd) || obstacle.GetPositions().Contains(fd) || snake.GetSnakeElements().Contains(fd1) || obstacle.GetPositions().Contains(fd1))
            {
                x   = randomNumbersGenerator.Next(2, 30);
                y   = randomNumbersGenerator.Next(0, 120);
                fd  = food.Generate(x, y);
                fd1 = food.Generate(x, y + 1);
            }

            Console.SetCursorPosition(fd.col, fd.row);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("\u2665\u2665");

            snake.SnakeBody();


            while (true)
            {
                Position snakeHead;
                Position snakeNewHead;
                Console.SetCursorPosition(0, 0);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Score: " + (userPoints).ToString());
                Console.WriteLine("Goal: " + goal);
                Console.ForegroundColor = ConsoleColor.Black;
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo userInput = Console.ReadKey();
                    if (events)
                    {
                        if (userInput.Key == ConsoleKey.LeftArrow)
                        {
                            if (snake.GetDirection() != Direction.left)
                            {
                                snake.SetDirection(Direction.right);
                            }
                        }
                        else if (userInput.Key == ConsoleKey.RightArrow)
                        {
                            if (snake.GetDirection() != Direction.right)
                            {
                                snake.SetDirection(Direction.left);
                            }
                        }
                        else if (userInput.Key == ConsoleKey.UpArrow)
                        {
                            if (snake.GetDirection() != Direction.up)
                            {
                                snake.SetDirection(Direction.down);
                            }
                        }
                        else if (userInput.Key == ConsoleKey.DownArrow)
                        {
                            if (snake.GetDirection() != Direction.down)
                            {
                                snake.SetDirection(Direction.up);
                            }
                        }
                    }
                    else
                    {
                        if (userInput.Key == ConsoleKey.LeftArrow)
                        {
                            if (snake.GetDirection() != Direction.right)
                            {
                                snake.SetDirection(Direction.left);
                            }
                        }
                        else if (userInput.Key == ConsoleKey.RightArrow)
                        {
                            if (snake.GetDirection() != Direction.left)
                            {
                                snake.SetDirection(Direction.right);
                            }
                        }
                        else if (userInput.Key == ConsoleKey.UpArrow)
                        {
                            if (snake.GetDirection() != Direction.down)
                            {
                                snake.SetDirection(Direction.up);
                            }
                        }
                        else if (userInput.Key == ConsoleKey.DownArrow)
                        {
                            if (snake.GetDirection() != Direction.up)
                            {
                                snake.SetDirection(Direction.down);
                            }
                        }
                    }
                    if (userInput.Key == ConsoleKey.R)
                    {
                        Console.Clear();
                        menu.DrawMenu();
                        Console.CursorVisible = false;
                        userinput             = Console.ReadKey();
                        while (userinput.Key != ConsoleKey.Enter)
                        {
                            menu.SelectDiff(userinput);
                            menu.DrawMenu();

                            userinput = Console.ReadKey();
                        }
                        Console.Clear();
                        if (menu.GetDiff() == 0)
                        {
                            goal = 1000;
                        }
                        else if (menu.GetDiff() == 1)
                        {
                            goal = 2000;
                        }
                        else
                        {
                            goal = 4000;
                        }
                        snake = new Snake();

                        //Create obstacles object
                        obstacle = new Obstacle();

                        //Create a Food Object
                        food = new Food();

                        //Extra Food
                        z     = 1;
                        extra = food.GetExFood();
                        spawn = false;

                        foodDissapearTime = 14000; //extended the time
                        if (menu.GetDiff() == 1)
                        {
                            foodDissapearTime = 10000;
                        }
                        else if (menu.GetDiff() == 2)
                        {
                            foodDissapearTime = 8000;
                        }

                        negativePoints = 0;
                        userPoints     = 0;

                        sleepTime = 100;
                        randomNumbersGenerator  = new Random();
                        randomNumbersGenerator2 = new Random();

                        Console.BufferHeight = Console.WindowHeight;
                        lastFoodTime         = Environment.TickCount;

                        foreach (Position ob in obstacle.GetPositions())
                        {
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.SetCursorPosition(ob.col, ob.row);
                            Console.Write("\u2592");
                        }

                        x = randomNumbersGenerator.Next(2, 30);
                        y = randomNumbersGenerator.Next(0, 120);

                        fd  = food.GetFood();
                        fd1 = food.Generate(x, y + 1);
                        while (snake.GetSnakeElements().Contains(fd) || obstacle.GetPositions().Contains(fd) || snake.GetSnakeElements().Contains(fd1) || obstacle.GetPositions().Contains(fd1))
                        {
                            x   = randomNumbersGenerator.Next(2, 30);
                            y   = randomNumbersGenerator.Next(0, 120);
                            fd  = food.Generate(x, y);
                            fd1 = food.Generate(x, y + 1);
                        }

                        Console.SetCursorPosition(fd.col, fd.row);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("\u2665\u2665");

                        snake.SnakeBody();

                        snakeHead    = snake.GetSnakeElements().Last();
                        snakeNewHead = snake.Move_Snake(snakeHead);
                    }
                }

                snakeHead    = snake.GetSnakeElements().Last();
                snakeNewHead = snake.Move_Snake(snakeHead);
                //Winning Requirement
                if (userPoints == goal)
                {
                    Console.SetCursorPosition(Console.WindowWidth - 30, 0);
                    Console.Write(new string(' ', 30));
                    string path = "../../Score.txt";
                    Console.SetCursorPosition((Console.WindowWidth / 2) - 14, (Console.WindowHeight / 2) - 2);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Successfull Clear Stage!!!");
                    Console.ForegroundColor = ConsoleColor.Red;
                    string exit = "\t\t\t\t\t\tPress Enter to Exit";
                    Console.WriteLine(exit);
                    Console.ForegroundColor = ConsoleColor.Black;
                    if (!File.Exists(path))
                    {
                        // Create a file to write to.
                        Console.WriteLine("Can't Open the File, Please try again");
                    }
                    else
                    {
                        using (StreamWriter sw = File.AppendText(path))
                        {
                            sw.WriteLine("Clear Stage");
                        }
                    }
                    ConsoleKeyInfo userInput = Console.ReadKey();
                    while (true)
                    {
                        if (userInput.Key == ConsoleKey.R)
                        {
                            Console.Clear();
                            menu.DrawMenu();
                            Console.CursorVisible = false;
                            userinput             = Console.ReadKey();
                            while (userinput.Key != ConsoleKey.Enter)
                            {
                                menu.SelectDiff(userinput);
                                menu.DrawMenu();

                                userinput = Console.ReadKey();
                            }
                            Console.Clear();
                            if (menu.GetDiff() == 0)
                            {
                                goal = 1000;
                            }
                            else if (menu.GetDiff() == 1)
                            {
                                goal = 2000;
                            }
                            else
                            {
                                goal = 4000;
                            }
                            snake = new Snake();

                            //Create obstacles object
                            obstacle = new Obstacle();

                            //Create a Food Object
                            food = new Food();

                            //Extra Food
                            z     = 1;
                            extra = food.GetExFood();
                            spawn = false;

                            foodDissapearTime = 14000; //extended the time
                            if (menu.GetDiff() == 1)
                            {
                                foodDissapearTime = 10000;
                            }
                            else if (menu.GetDiff() == 2)
                            {
                                foodDissapearTime = 8000;
                            }

                            negativePoints = 0;
                            userPoints     = 0;

                            sleepTime = 100;
                            randomNumbersGenerator = new Random();

                            Console.BufferHeight = Console.WindowHeight;
                            lastFoodTime         = Environment.TickCount;

                            foreach (Position ob in obstacle.GetPositions())
                            {
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.SetCursorPosition(ob.col, ob.row);
                                Console.Write("=");
                            }

                            fd  = food.GetFood();
                            fd1 = food.Generate(x, y + 1);
                            while (snake.GetSnakeElements().Contains(fd) || obstacle.GetPositions().Contains(fd) || snake.GetSnakeElements().Contains(fd1) || obstacle.GetPositions().Contains(fd1))
                            {
                                x   = randomNumbersGenerator.Next(2, 30);
                                y   = randomNumbersGenerator.Next(0, 120);
                                fd  = food.Generate(x, y);
                                fd1 = food.Generate(x, y + 1);
                            }

                            Console.SetCursorPosition(fd.col, fd.row);
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write("\u2665\u2665");

                            snake.SnakeBody();
                            snakeHead    = snake.GetSnakeElements().Last();
                            snakeNewHead = snake.Move_Snake(snakeHead);

                            break;
                        }

                        if (userInput.Key == ConsoleKey.Enter)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            System.Environment.Exit(1);
                        }

                        userInput = Console.ReadKey();
                    }
                }

                if (snake.GetSnakeElements().Contains(snakeNewHead) || obstacle.GetPositions().Contains(snakeNewHead))
                {
                    string path = "../../Score.txt";
                    Console.SetCursorPosition((Console.WindowWidth / 2) - 8, (Console.WindowHeight / 2) - 2);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Game over!");
                    // userPoints = (snakeElements.Count - 6) * 100; //removed negativePoints because it's decreasing too fast.
                    //if (userPoints < 0) userPoints = 0;
                    userPoints = Math.Max(userPoints, 0);
                    Console.WriteLine("\t\t\t\t\t\tYour points are: {0}", userPoints);
                    string exit = "\t\t\t\t\t\tPress Enter to Exit";
                    Console.WriteLine(exit);
                    Console.ForegroundColor = ConsoleColor.Black;
                    if (!File.Exists(path))
                    {
                        // Create a file to write to.
                        Console.WriteLine("Can't Open the File, Please try again");
                    }
                    else
                    {
                        using (StreamWriter sw = File.AppendText(path))
                        {
                            sw.WriteLine(userPoints);
                        }
                    }
                    ConsoleKeyInfo userInput = Console.ReadKey();
                    while (true)
                    {
                        if (userInput.Key == ConsoleKey.R)
                        {
                            Console.Clear();
                            menu.DrawMenu();
                            Console.CursorVisible = false;
                            userinput             = Console.ReadKey();
                            while (userinput.Key != ConsoleKey.Enter)
                            {
                                menu.SelectDiff(userinput);
                                menu.DrawMenu();

                                userinput = Console.ReadKey();
                            }
                            Console.Clear();
                            if (menu.GetDiff() == 0)
                            {
                                goal = 1000;
                            }
                            else if (menu.GetDiff() == 1)
                            {
                                goal = 2000;
                            }
                            else
                            {
                                goal = 4000;
                            }

                            snake = new Snake();
                            //Create obstacles object
                            obstacle = new Obstacle();

                            //Create a Food Object
                            food = new Food();

                            //Extra Food
                            z     = 1;
                            extra = food.GetExFood();
                            spawn = false;

                            foodDissapearTime = 14000; //extended the time
                            if (menu.GetDiff() == 1)
                            {
                                foodDissapearTime = 10000;
                            }
                            else if (menu.GetDiff() == 2)
                            {
                                foodDissapearTime = 8000;
                            }

                            negativePoints = 0;
                            userPoints     = 0;

                            sleepTime = 100;
                            randomNumbersGenerator = new Random();

                            Console.BufferHeight = Console.WindowHeight;
                            lastFoodTime         = Environment.TickCount;

                            foreach (Position ob in obstacle.GetPositions())
                            {
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.SetCursorPosition(ob.col, ob.row);
                                Console.Write("\u2592");
                            }


                            fd  = food.GetFood();
                            fd1 = food.Generate(x, y + 1);
                            while (snake.GetSnakeElements().Contains(fd) || obstacle.GetPositions().Contains(fd) || snake.GetSnakeElements().Contains(fd1) || obstacle.GetPositions().Contains(fd1))
                            {
                                x   = randomNumbersGenerator.Next(0, 30);
                                y   = randomNumbersGenerator.Next(0, 120);
                                fd  = food.Generate(x, y);
                                fd1 = food.Generate(x, y + 1);
                            }

                            Console.SetCursorPosition(fd.col, fd.row);
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write("\u2665\u2665");

                            snake.SnakeBody();
                            snakeHead    = snake.GetSnakeElements().Last();
                            snakeNewHead = snake.Move_Snake(snakeHead);
                            break;
                        }
                        if (userInput.Key == ConsoleKey.Enter)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            System.Environment.Exit(1);
                        }

                        userInput = Console.ReadKey();
                    }
                }

                Console.SetCursorPosition(snakeHead.col, snakeHead.row);
                if (SnakeColor == 0)
                {
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                }
                else if (SnakeColor == 1)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                }
                else if (SnakeColor == 2)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                }
                else if (SnakeColor == 3)
                {
                    Console.ForegroundColor = ConsoleColor.Magenta;
                }
                else if (SnakeColor == 4)
                {
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                }
                else if (SnakeColor == 5)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                }

                Console.Write("*");

                snake.GetSnakeElements().Enqueue(snakeNewHead);
                Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
                Console.ForegroundColor = ConsoleColor.Gray;
                string head = snake.SnakeHead();
                Console.Write(head);


                // feeding the snake
                if ((snakeNewHead.col == fd.col && snakeNewHead.row == fd.row) || (snakeNewHead.col == fd1.col && snakeNewHead.row == fd1.row))
                {
                    System.Media.SoundPlayer player2 = new System.Media.SoundPlayer();
                    player2.SoundLocation = "../../music/soundeffect.wav";
                    player2.Play();
                    Console.SetCursorPosition(fd.col, fd.row);
                    Console.Write("  ");
                    SnakeColor  = randomNumbersGenerator.Next(0, 5);
                    userPoints += 100;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.SetCursorPosition(0, 0);
                    Console.Write("Score :" + (userPoints).ToString());


                    while (snake.GetSnakeElements().Contains(fd) || obstacle.GetPositions().Contains(fd) || snake.GetSnakeElements().Contains(fd1) || obstacle.GetPositions().Contains(fd1))
                    {
                        x   = randomNumbersGenerator.Next(2, 30);
                        y   = randomNumbersGenerator.Next(0, 120);
                        fd  = food.Generate(x, y);
                        fd1 = food.Generate(x, y + 1);
                    }
                    z            = randomNumbersGenerator.Next(0, 1);
                    lastFoodTime = Environment.TickCount;
                    Console.SetCursorPosition(fd.col, fd.row);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write("\u2665\u2665");
                    if (menu.GetDiff() == 0)
                    {
                        sleepTime -= 0.01;
                    }
                    else if (menu.GetDiff() == 1)
                    {
                        sleepTime -= 0.02;
                    }
                    else
                    {
                        sleepTime -= 0.04;
                    }


                    //Position obstacles = new Position();
                    Position ob = new Position();
                    while (snake.GetSnakeElements().Contains(ob) ||
                           obstacle.GetPositions().Contains(ob) ||
                           (fd.row != ob.row && fd.col != ob.row))
                    {
                        x  = randomNumbersGenerator.Next(2, Console.WindowHeight);
                        y  = randomNumbersGenerator.Next(0, Console.WindowWidth);
                        ob = obstacle.Generate(x, y);
                    }

                    obstacle.GetPositions().Add(ob);
                    Console.SetCursorPosition(ob.col, ob.row);
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.Write("\u2592");

                    if (menu.GetDiff() == 2)
                    {
                        while (snake.GetSnakeElements().Contains(ob) ||
                               obstacle.GetPositions().Contains(ob) ||
                               (fd.row != ob.row && fd.col != ob.row))
                        {
                            x  = randomNumbersGenerator.Next(2, Console.WindowHeight);
                            y  = randomNumbersGenerator.Next(0, Console.WindowWidth);
                            ob = obstacle.Generate(x, y);
                        }

                        obstacle.GetPositions().Add(ob);
                        Console.SetCursorPosition(ob.col, ob.row);
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.Write("\u2592");
                    }
                }
                else
                {
                    // moving...
                    Position last = snake.Moving();
                    Console.SetCursorPosition(last.col, last.row);
                    Console.Write(" ");
                }

                if (Environment.TickCount - lastFoodTime >= foodDissapearTime)
                {
                    Console.SetCursorPosition(fd.col, fd.row);

                    do
                    {
                        x   = randomNumbersGenerator.Next(2, Console.WindowHeight);
                        y   = randomNumbersGenerator.Next(0, Console.WindowWidth);
                        fd  = food.Generate(x, y);
                        fd1 = food.Generate(x, y + 1);
                        Console.WriteLine("  ");
                    }while (snake.GetSnakeElements().Contains(fd) || obstacle.GetPositions().Contains(fd) || snake.GetSnakeElements().Contains(fd1) || obstacle.GetPositions().Contains(fd1));
                    lastFoodTime = Environment.TickCount;

                    lastFoodTime = Environment.TickCount;
                    Console.SetCursorPosition(fd.col, fd.row);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write("\u2665\u2665");
                }

                if (menu.GetDiff() == 2)
                {
                    if (z == 0)
                    {
                        do
                        {
                            x     = randomNumbersGenerator.Next(2, Console.WindowHeight);
                            y     = randomNumbersGenerator.Next(0, Console.WindowWidth);
                            extra = food.Generate(x, y);
                        }while (snake.GetSnakeElements().Contains(extra) || obstacle.GetPositions().Contains(extra));
                        Console.SetCursorPosition(extra.col, extra.row);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("Q");

                        z    += 1;
                        spawn = true;
                    }

                    if (spawn)
                    {
                        if (Environment.TickCount - lastFoodTime >= 4000)
                        {
                            Console.SetCursorPosition(extra.col, extra.row);
                            Console.WriteLine(" ");
                            lastFoodTime = Environment.TickCount;
                            spawn        = false;
                        }
                        else if (snake.GetSnakeElements().Contains(extra))
                        {
                            userPoints += 1000;
                            Console.SetCursorPosition(extra.col, extra.row);
                            Console.WriteLine(" ");
                            spawn = false;
                        }
                    }
                }
                if (userPoints > 0 && userPoints % 500 == 0 && events == false)
                {
                    events    = true;
                    countdown = 9;
                }

                if (events)
                {
                    Console.SetCursorPosition(Console.WindowWidth - 30, 0);
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("Upside Down Event!!!: ");
                    Console.Write(countdown);
                    if (Environment.TickCount - lastFoodTime >= 1000)
                    {
                        lastFoodTime = Environment.TickCount;
                        countdown   -= 1;
                    }
                    if (countdown == 0)
                    {
                        Console.SetCursorPosition(Console.WindowWidth - 30, 0);
                        Console.Write(new string(' ', 30));
                        events = false;
                    }
                }
                Console.ForegroundColor = ConsoleColor.Black; //hide text if clicked others button
                Console.CursorVisible   = false;

                Thread.Sleep((int)sleepTime);
            }
        }
示例#3
0
        //Generating obstacles
        private void GenerateObstacles(int count)
        {
            Random random = new Random();
            Point newPosition;
            Size obstacleSize;
            int obstacleLength;

            //0 for vertical, false for horizontal
            for (int i = 0; i < count; i++)
            {
                int orientationTemp = random.Next(0,2);
                if (orientationTemp == 0)
                {
                    obstacleLength = GenerateObstacleSize(Orientation.VERTICAL);
                    obstacleSize = new Size(snake.Body.StrokeThickness, obstacleLength);
                }
                else
                {
                    obstacleLength = GenerateObstacleSize(Orientation.HORIZONTAL);
                    obstacleSize = new Size(obstacleLength, snake.Body.StrokeThickness);
                }
                //generating new obstacle position
                while (!FindObstaclePlace(out newPosition, obstacleSize)) { }
                //setting obstacle's properties

                UIElement shape = CreateObstacle(obstacleSize, newPosition);

                Canvas.SetLeft(shape, newPosition.X);
                Canvas.SetTop(shape, newPosition.Y);
                playField.Children.Add(shape);

                Obstacle obstacle = new Obstacle(shape, (int)newPosition.X,
                    (int)newPosition.Y, obstacleSize);
                obstacles.Add(obstacle);
            }
        }
示例#4
0
 public void AddObstacle(Obstacle obs)
 {
     _obstacles.Add(obs);
     _positions.Add(obs.Pos);
 }
示例#5
0
        static void Main(string[] args)
        {
            // initialize objects
            UserManagement userlist = new UserManagement();

            Console.WriteLine("Please enter your username: "******"../../sound/bgm.wav";
            bgm.Play();

            byte right = 0;
            byte left  = 1;
            byte down  = 2;
            byte up    = 3;

            int lastFoodTime      = 0;
            int foodDissapearTime = 16000;

//            bool isGameOver = false;


            Position[] directions = new Position[]
            {
                new Position(0, 1),  // right
                new Position(0, -1), // left
                new Position(1, 0),  // down
                new Position(-1, 0), // up
            };

            Console.BufferHeight = Console.WindowHeight;
            double sleepTime = 100;

            lastFoodTime = Environment.TickCount;

            // Initialize obstacle and draw obstacles
            Obstacle obs = new Obstacle();

            obs.Generate_random_obstacle();

            foreach (Position obstacle in obs.GetObsPos)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.SetCursorPosition(obstacle.col, obstacle.row);
                Console.Write("=");
            }

            // Iniatitlize food and draw food
            Food food = new Food();

            food.Generate_random_food();

            // Initialize snake and draw snake
            Snake snake = new Snake();

            snake.DrawSnake();
            int direct = right;

            // looping
            while (true)
            {
                // Set the score at the top right
                Console.SetCursorPosition(Console.WindowWidth - 10, Console.WindowHeight - 30);
                Console.WriteLine("Score: " + user.getScore);

                // check for key pressed
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo userInput = Console.ReadKey();
                    if (userInput.Key == ConsoleKey.LeftArrow)
                    {
                        if (direct != right)
                        {
                            direct = left;
                        }
                    }
                    if (userInput.Key == ConsoleKey.RightArrow)
                    {
                        if (direct != left)
                        {
                            direct = right;
                        }
                    }
                    if (userInput.Key == ConsoleKey.UpArrow)
                    {
                        if (direct != down)
                        {
                            direct = up;
                        }
                    }
                    if (userInput.Key == ConsoleKey.DownArrow)
                    {
                        if (direct != up)
                        {
                            direct = down;
                        }
                    }
                }

                // update snake position
                Position snakeHead     = snake.GetPos.Last();
                Position nextDirection = directions[direct];

                Position snakeNewHead = new Position(snakeHead.row + nextDirection.row,
                                                     snakeHead.col + nextDirection.col);

                // check for snake if exceed the width or height
                if (snakeNewHead.col < 0)
                {
                    snakeNewHead.col = Console.WindowWidth - 1;
                }
                if (snakeNewHead.row < 0)
                {
                    snakeNewHead.row = Console.WindowHeight - 1;
                }
                if (snakeNewHead.row >= Console.WindowHeight)
                {
                    snakeNewHead.row = 0;
                }
                if (snakeNewHead.col >= Console.WindowWidth)
                {
                    snakeNewHead.col = 0;
                }

                // check for snake collison with self or obstacles
                if (snake.GetPos.Contains(snakeNewHead) || obs.GetObsPos.Contains(snakeNewHead))
                {
                    Console.SetCursorPosition(0, 0);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Game over!");
                    return;
                }

                // check for collision with the food
                if (snakeNewHead.col == food.x && snakeNewHead.row == food.y)
                {
                    System.Media.SoundPlayer eat = new System.Media.SoundPlayer();
                    eat.SoundLocation = "../../sound/coin.wav";
                    eat.Play();
                    user.ScoreIncrement(1);
                    Console.SetCursorPosition(Console.WindowWidth - 10, Console.WindowHeight - 30);
                    Console.WriteLine("Score: " + user.getScore);
                    //Console.WriteLine("Eaten");
                    bgm.Play();
                    food = new Food();
                    food.Generate_random_food();
                }
                // draw the snake
                Console.SetCursorPosition(snakeHead.col, snakeHead.row);
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.Write("*");

                // moving
                snake.GetPos.Enqueue(snakeNewHead);
                Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
                Console.ForegroundColor = ConsoleColor.Gray;

                // draw the snake head
                if (direct == right)
                {
                    Console.Write(">");
                }
                if (direct == left)
                {
                    Console.Write("<");
                }
                if (direct == up)
                {
                    Console.Write("^");
                }
                if (direct == down)
                {
                    Console.Write("v");
                }

                // moving...
                Position last = snake.GetPos.Dequeue();
                Console.SetCursorPosition(last.col, last.row);
                Console.Write(" ");

                // set winning condition score = 5
                if (user.getScore == 5)
                {
                    Console.Clear();
                    Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Stage Clear!");
                    userlist.AddUser(user);

                    var x = userlist.getUsers;
                    userlist.recordUser();
                    //userlist.readRecord();
                    return;
                }

                // food timer
                if (Environment.TickCount - lastFoodTime >= foodDissapearTime)
                {
                    Console.SetCursorPosition(food.x, food.y);
                    Console.Write(" ");
                    food = new Food();
                    food.Generate_random_food();

                    lastFoodTime = Environment.TickCount;
                }

                sleepTime -= 0.01;
                Thread.Sleep((int)sleepTime);
            }
        }