Example #1
0
        static void Main(string[] args)
        {
            Feld  feld = new Feld(40, 20);
            Snake snek = new Snake();
            Food  food = new Food();

            Console.CursorVisible = false;

            bool isAlive = true;

            while (isAlive)
            {
                Task.Delay(100).Wait();
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo userInput = Console.ReadKey(true);
                    if (userInput.Key == DIR_LEFT && currentDirection != DIR_RIGHT)
                    {
                        currentDirection = DIR_LEFT;
                    }
                    if (userInput.Key == DIR_RIGHT && currentDirection != DIR_LEFT)
                    {
                        currentDirection = DIR_RIGHT;
                    }
                    if (userInput.Key == DIR_UP && currentDirection != DIR_DOWN) // replace with WASD?
                    {
                        currentDirection = DIR_UP;
                    }
                    if (userInput.Key == DIR_DOWN && currentDirection != DIR_UP)
                    {
                        currentDirection = DIR_DOWN;
                    }
                }

                if (snek.Head().x == Console.WindowWidth - 1 && snek.Head().y == Console.WindowHeight - 1)
                {
                    isAlive = false;
                    Console.SetCursorPosition(50, 50);
                    Console.Write("GAME OVER!");
                }

                food.Spawn();
                snek.Move(Direction(currentDirection));
                snek.Draw();
                food.Draw();

                if (snek.Head().x == food.Position(0).x&& snek.Head().y == food.Position(0).y)
                {
                    snek.Eat(food.Position(0));
                    food.Despawn();
                    Thread.Sleep(150);
                }
            }
            Console.ReadLine();
        }