Exemple #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);
            }
        }
Exemple #2
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);
            }
        }