Example #1
0
        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.Cyan;
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Black;
            int score = 0;
            //drawing a game field frame
            Walls walls = new Walls(80, 25);

            walls.Draw();

            Point snakeTail = new Point(15, 15, 's');
            Snake snake     = new Snake(snakeTail, 5, Direction.RIGHT);

            snake.Draw();


            FoodGenerator foodGenerator = new FoodGenerator(80, 25, '$');
            Point         food          = foodGenerator.GenerateFood();

            food.Draw();

            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    break;
                }

                if (snake.Eat(food))
                {
                    food = foodGenerator.GenerateFood();
                    food.Draw();
                    score++;
                }
                else
                {
                    snake.Move();
                }

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.handleKeys(key.Key);
                }

                Thread.Sleep(300);
                //snake.Move();
            }

            string str_score = Convert.ToString(score);

            WriteGameOver(str_score);
            Console.ReadLine();
        }
Example #2
0
        static void Main(string[] args)
        {
            int score = 0;

            Walls walls = new Walls(80, 25);

            walls.Draw();

            Point snaketail = new Point(15, 15, 'ยค');
            Snake snake     = new Snake(snaketail, 7, Direction.RIGHT);

            snake.Draw();
            FoodGenerator foodGenerator = new FoodGenerator(80, 25, '$');
            Point         food          = foodGenerator.Generatefood();

            food.Draw();

            while (true)
            {
                if (walls.IsHit(snake) || snake.ishittail())
                {
                    break;
                }
                if (snake.Eat(food))
                {
                    food = foodGenerator.Generatefood();
                    food.Draw();
                    score++;
                }
                else
                {
                    snake.Move();
                }
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.HandleKeys(key.Key);
                }

                Thread.Sleep(150);
                //snake.Move();
            }
            string str_score = Convert.ToString(score);

            writegameover(str_score);
            Console.ReadLine();
        }