Пример #1
0
        public static void func()
        {
            while (true)
            {
                if (direction == 3)
                {
                    snake.Move(0, 1);
                }
                if (direction == 4)
                {
                    snake.Move(0, -1);
                }
                if (direction == 2)
                {
                    snake.Move(-1, 0);
                }
                if (direction == 1)
                {
                    snake.Move(1, 0);
                }


                if (snake.CollisionWithWall(wall) || snake.Collision())
                {
                    Console.Clear();
                    Console.SetCursorPosition(53, 10);
                    Console.WriteLine("GAME OVER!!!!");
                    Console.ReadKey();
                    Console.Clear();
                    snake = new Snake();
                    level = 1;
                    wall  = new Wall(level);
                    Food food1 = new Food(level);
                    speed        = 400;
                    food1.result = 0;
                }

                if (snake.golovaX == food1.tempX && snake.golovaY == food1.tempY)
                {
                    food1.result += 10;
                    food1.food(level);
                }

                snake.Draw();
                wall.Draw();
                food1.Draw();

                if (snake.cnt % 50 == 0)
                {
                    speed = Math.Max(1, speed - 100);
                }
                Thread.Sleep(speed);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            snake.body.Add(new Point(10, 10));
            Console.CursorVisible = false;
            Console.SetWindowSize(80, 30);
            wall.Draw();
            Thread t = new Thread(MoveSnake);

            t.Start();
            while (true)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey();

                if (keyInfo.Key == ConsoleKey.DownArrow && last != 3)
                {
                    last      = 4;
                    direction = 1;
                }

                if (keyInfo.Key == ConsoleKey.UpArrow && last != 4)
                {
                    last      = 3;
                    direction = 2;
                }
                if (keyInfo.Key == ConsoleKey.LeftArrow && last != 1)
                {
                    last      = 2;
                    direction = 3;
                }
                if (keyInfo.Key == ConsoleKey.RightArrow && last != 2)
                {
                    last      = 1;
                    direction = 4;
                }
                if (keyInfo.Key == ConsoleKey.S)
                {
                    t.Suspend();
                    snake.Serialization();
                    food.Serialization();
                    wall.Serialization();
                }
                if (keyInfo.Key == ConsoleKey.D)
                {
                    t.Resume();
                    snake = snake.Deserialization();
                    wall  = wall.Deserialization();
                    food  = food.Deserialization();
                    wall.Draw();
                }
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            Console.SetWindowSize(80, 25);

            Wall walls = new Wall(80, 25);

            walls.Draw();

            Point p     = new Point(4, 5, '*');
            Snake snake = new Snake(p, 4, Direction.RIGHT);

            snake.Draw();

            FoodCreator foodCreator = new FoodCreator(80, 25, '$');
            Point       food        = foodCreator.CreateFood();

            food.Draw();


            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    break;
                }
                if (snake.Eat(food))
                {
                    food = foodCreator.CreateFood();
                    food.Draw();
                }
                else
                {
                    snake.Move();
                }
                Thread.Sleep(150);

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

            WriteGameOver();
            Console.ReadKey();
        }
Пример #4
0
        public void MoveSnake(object data)
        {
            wall.Draw();
            while (IsAlive)
            {
                ConsoleKeyInfo key = (ConsoleKeyInfo)data;
                if (key.Key == ConsoleKey.Escape)
                {
                    return;
                }
                snake.Clear();
                snake.Move();
                if (snake.Collision(food))
                {
                    snake.cors.Add(new Coordinate(0, 0));

                    score = score + point;
                    while (food.Collision(snake) || food.Collision(wall))
                    {
                        food.Clear();
                        food.Create();
                    }
                    if (snake.cors.Count % 5 == 0)
                    {
                        wall.LevelUp();
                        //wall.LoadLevel();
                        point += point;
                    }
                }
                if (snake.Collision(wall))
                {
                    IsAlive = false;
                }
                if (snake.IsSuicide(snake))
                {
                    IsAlive = false;
                }

                Draw();
                Console.SetCursorPosition(1, 26);
                Console.Write("Score:    " + score);
                Thread.Sleep(100);
            }
            GameOver(score);
        }
Пример #5
0
        public static void MoveSnake()
        {
            while (true)
            {
                switch (direction)
                {
                case 1:
                    snake.Move(0, 1);
                    break;

                case 2:
                    snake.Move(0, -1);
                    break;

                case 3:
                    snake.Move(-1, 0);
                    break;

                case 4:
                    snake.Move(1, 0);
                    break;
                }


                if (snake.Collisionwithwall(wall) == true || snake.Collision() == true)

                {
                    Console.Clear();

                    Console.SetCursorPosition(5, 5);

                    Console.WriteLine("GAME OVER!!!!");
                    Console.ReadKey();

                    while (true)
                    {
                        ConsoleKeyInfo k = Console.ReadKey();
                        if (k.Key == ConsoleKey.Enter)
                        {
                            Console.Clear();
                            snake = new Snake();
                            wall  = new Wall(level);
                            food  = new Food();
                            food.Draw();
                            wall.Draw();
                            snake.Draw();
                            break;
                        }
                    }
                }
                if (snake.Eat(food))
                {
                    food.Setrandompos(snake, wall);
                    score++;
                    cnt++;
                }
                if (cnt == 3)
                {
                    level++;
                    cnt  = 0;
                    wall = new Wall(level);
                }
                snake.Draw();
                food.Draw();
                wall.Draw();
                Thread.Sleep(speed);
                if (score == level * 5)
                {
                    speed = Math.Max(1, speed - 50);
                }
                Console.SetCursorPosition(30, 20);
                Console.WriteLine("score: " + score);
                Console.SetCursorPosition(39, 20);
                Console.WriteLine("level: " + level);
            }
        }