Exemple #1
0
        static void main(string [] args)
        {
            int   level = 1;
            Snake snake = new Snake();
            Wall  wall  = new wall;

            while (true)
            {
                ConsoleKeyInfo keyinfo = Console.ReadKey();
                if (keyinfo.Key == ConsoleKey.DownArrow)
                {
                    snake.move(0, 1);
                }
                if (keyinfo.Key == ConsoleKey.UpArrow)
                {
                    snake.move(0, -1);
                }
                if (keyinfo.Key == ConsoleKey.RightArrow)
                {
                    snake.move(1, 0);
                }
                if (keyinfo.Key == ConsoleKey.LeftArrow)
                {
                    snake.move(-1, 0);
                }
                if (keyinfo.Key = ConsoleKey.R)
                {
                    level = 1;
                    snake = new Snake;
                    wall  = new Wall(level);
                }
                if (snake.CollisionWithWall(wall) || snake.Collision())
                {
                    Console.Clear();
                    Console.SetCursorPosition(5, 5);
                    Console.WriteLine("game over!!!!");
                    Console.ReadKey();
                    snake = new Snake;
                    level = 1;
                    wall  = new Wall(level);
                }
                if (snake.cnt % 400 == 0)
                {
                    level++;
                    wall = new Wall(level);
                }
                Console.Clear();
                snake.draw();
                wall.draw();
            }
        }
Exemple #2
0
        public static void Func()
        {
            while (true)
            {
                if (direction == 8)
                {
                    snake.Move(0, -1);
                }
                if (direction == 2)
                {
                    snake.Move(0, 1);
                }
                if (direction == 4)
                {
                    snake.Move(-1, 0);
                }
                if (direction == 6)
                {
                    snake.Move(1, 0);
                }

                if (snake.Eating(food))
                {
                    food.SetRandomPosition();
                }
                if (snake.Collision() || snake.WallCollision(wall))
                {
                    Console.Clear();
                    Console.SetCursorPosition(10, 10);
                    Console.WriteLine("GAME OVER!!!");
                    Console.ReadKey();
                    Console.Clear();
                    snake = new Snake();
                    wall  = new Wall(level);
                }
                snake.Draw();
                wall.Draw();
                food.Draw();
                Thread.Sleep(speed);
            }
        }
Exemple #3
0
        public void Move(ConsoleKeyInfo key)
        {
            Position currentPos;

            if (snake.points.Count != 0)
            {
                currentPos = new Position()
                {
                    left = snake.points.Last().left, top = snake.points.Last().top
                }
            }
            ;
            else
            {
                currentPos = StartPosition();
            }

            switch (key.Key)
            {
            case ConsoleKey.LeftArrow:
                currentPos.left--;
                break;

            case ConsoleKey.RightArrow:
                currentPos.left++;
                break;

            case ConsoleKey.UpArrow:
                currentPos.top--;
                break;

            case ConsoleKey.DownArrow:
                currentPos.top++;
                break;
            }

            snake.Collision(currentPos);

            snake.points.Add(currentPos);
            snake.RemoveTail();
        }
Exemple #4
0
        public static void PlayGame()
        {
            while (true)
            {
                if (direction == 1)
                {
                    snake.Move(-1, 0);
                }
                if (direction == 2)
                {
                    snake.Move(1, 0);
                }
                if (direction == 3)
                {
                    snake.Move(0, -1);
                }
                if (direction == 4)
                {
                    snake.Move(0, 1);
                }

                Console.SetCursorPosition(0, 0);
                Console.WriteLine("High score : " + " " + points + "   Score: " + score + " " + "Level" + " " + level);
                Console.SetCursorPosition(0, 1);
                Console.WriteLine("If you want to save current score, enter [Probel]");
                Console.SetCursorPosition(0, 2);
                Console.WriteLine("If you want to quit, enter [Escape] ");


                if (snake.CollisionWithWall(wall) || snake.Collision())
                {
                    Console.Clear();
                    Console.SetCursorPosition(7, 7);
                    StreamReader sr = new StreamReader(@"C:\Users\Compag\Desktop\PP-2\week5\SnakeGame\Snake\gameover.txt");
                    string       s  = sr.ReadToEnd();
                    Console.WriteLine(s);
                    Console.WriteLine("High Score : " + " " + points + "Your Score is " + score);

                    Console.ReadKey();

                    Console.Clear();
                    speed = 400;
                    snake = new Snake();
                    level = 1;
                    wall  = new Wall(level);
                    score = 0;

                    while (!food.foodonwall(wall) || !food.foononsnake(snake))
                    {
                        food.SetRandomPos();
                        Console.SetCursorPosition(food.x, food.y);
                        Console.Write("$");
                    }
                }

                if (snake.Eat(food))
                {
                    snake.AddBody();
                    food.SetRandomPos();
                    score += 10;
                    points = Math.Max(points, score);
                    Console.SetCursorPosition(food.x, food.y);
                    Console.Write("$");

                    while (!food.foodonwall(wall) || !food.foononsnake(snake))
                    {
                        food.SetRandomPos();
                        Console.SetCursorPosition(food.x, food.y);
                        Console.Write("$");
                    }
                }
                if (score == level * 50)
                {
                    Console.Clear();
                    level++;
                    wall = new Wall(level);

                    speed = Math.Max(1, speed - 100);

                    while (!food.foodonwall(wall) || !food.foononsnake(snake))
                    {
                        food.SetRandomPos();
                        Console.SetCursorPosition(food.x, food.y);
                        Console.Write("$");
                    }
                }
                // Console.Clear();
                snake.Draw();
                food.ShowFood();
                wall.WallDraw();
                Thread.Sleep(speed);
            }
        }
Exemple #5
0
        public static void Thread_func()
        {
            while (true)
            {
                if (direction == 1)
                {
                    snake.Motion(1, 0);
                }
                if (direction == 2)
                {
                    snake.Motion(-1, 0);
                }
                if (direction == 3)
                {
                    snake.Motion(0, 1);
                }
                if (direction == 4)
                {
                    snake.Motion(0, -1);
                }

                if (a.Key == ConsoleKey.R)
                {
                    level = 1;
                    snake = new Snake();
                    wall  = new Wall(level);
                    eat   = new food();
                    while (!eat.testsnake(snake) || (!eat.testwall(wall)))
                    {
                        n   = rnd.Next(2, 75);
                        m   = rnd.Next(2, 24);
                        eat = new food(n, m);
                    }
                }
                if (a.Key == ConsoleKey.S)
                {
                    FileStream fs   = new FileStream("data.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    FileStream fs_1 = new FileStream("data1.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    List <int> list = new List <int>();
                    list.Add(level);
                    list.Add(n);
                    list.Add(m);
                    XmlSerializer xml   = new XmlSerializer(typeof(Snake));
                    XmlSerializer xml_1 = new XmlSerializer(typeof(List <int>));
                    xml.Serialize(fs, snake);
                    xml_1.Serialize(fs_1, list);
                    fs.Close();
                    fs_1.Close();
                }
                if (a.Key == ConsoleKey.E)
                {
                    FileStream    fs    = new FileStream("data.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    FileStream    fs_1  = new FileStream("data1.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    XmlSerializer xml   = new XmlSerializer(typeof(Snake));
                    XmlSerializer xml_1 = new XmlSerializer(typeof(List <int>));

                    snake = xml.Deserialize(fs) as Snake;
                    List <int> list = xml_1.Deserialize(fs_1) as List <int>;
                    fs.Close();
                    fs_1.Close();
                    level = list[0];
                    n     = list[1];
                    m     = list[2];
                    wall  = new Wall(level);
                    eat   = new food(n, m);
                }
                if (snake.CollisionWithFood(eat))
                {
                    snake.AddMember();
                    eat = new food();
                    while (!eat.testsnake(snake) || (!eat.testwall(wall)))
                    {
                        n   = rnd.Next(2, 75);
                        m   = rnd.Next(2, 24);
                        eat = new food(n, m);
                    }
                }
                if (snake.CollisionWithWall(wall) || snake.Collision())
                {
                    Console.Clear();
                    Console.SetCursorPosition(5, 5);
                    Console.WriteLine("GAME OVER!!!!");
                    Console.ReadKey();
                    snake = new Snake();
                    level = 1;
                    eat   = new food();
                    while (!eat.testsnake(snake) || (!eat.testwall(wall)))
                    {
                        n   = rnd.Next(2, 75);
                        m   = rnd.Next(2, 24);
                        eat = new food(n, m);
                    }


                    wall = new Wall(level);
                }
                if (snake.UpLevel(level))

                {
                    direction = 1;
                    level++;
                    snake = new Snake();
                    wall  = new Wall(level);
                    eat   = new food();
                    while (!eat.testsnake(snake) || (!eat.testwall(wall)))
                    {
                        n   = rnd.Next(2, 75);
                        m   = rnd.Next(2, 24);
                        eat = new food(n, m);
                    }
                }

                Console.Clear();
                wall.Draw();
                snake.Draw();
                eat.Showfood(n, m);

                Console.SetCursorPosition(0, 27);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write("Your Level: " + level + " You must dial " + level * 5);
                Console.SetCursorPosition(0, 28);
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("Score: " + (snake.a.Count - 1));
                if (snake.UpLevel(level))
                {
                    speed = Math.Max(100, speed - 50);
                }
                Thread.Sleep(speed);
            }
        }
Exemple #6
0
        static void Main(string[] args)
        {
            //Установим размер окна консоли
            Console.SetWindowSize(105, 30);

            //Ограждение игрового поля
            HorizontalLine horizontalLine = new HorizontalLine(0, 100, 0, '*');

            horizontalLine.Draw();

            HorizontalLine horizontalLine2 = new HorizontalLine(0, 100, 24, '*');

            horizontalLine2.Draw();


            VerticalLine verticalLine = new VerticalLine(0, 24, 0, '*');

            verticalLine.Draw();

            VerticalLine verticalLine2 = new VerticalLine(0, 24, 100, '*');

            verticalLine2.Draw();

            //Cоздаём змею
            //точка её хвоста
            Point tail = new Point(2, 2, 'x');
            //змея длиной 3, ползёт вправо
            Snake snake = new Snake(tail, 10, Direction.right);

            snake.Draw();

            Food food = new Food(100, 24, 'o');

            food.MakeFood(ref snake.coordinats);

            //движение и управление змейкой
            while (true)
            {
                //Проверяем не закончилась ли игра если змея столкнулась с самой собой или оградой поля
                if (snake.Collision())
                {
                    break;
                }

                //Проверяем съела ли змея фрукт, если да генерим новый
                if (snake.Eat(food))
                {
                    food.MakeFood(ref snake.coordinats);
                    snake.Draw();
                }

                //Смотрим нажатия клавиш пользователем
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    //Вызываем метод управления направлением змеи из класса Snake
                    snake.SnakeControl(key.Key);
                }
                //Движение змеи
                snake.Move();
                Thread.Sleep(300);
            }
        }