Example #1
0
 static void Main(string[] args)
 {
     // рисую рамочку по краям
     HorizontalLine line = new HorizontalLine(0,78,0,'%');
     HorizontalLine line2 = new HorizontalLine(0, 78, 24, '%');
     VerticalLine line3 = new VerticalLine(0, 24, 0, '%');
     VerticalLine line4 = new VerticalLine(0, 24, 78, '%');
     line.Draw();
     line2.Draw();
     line3.Draw();
     line4.Draw();
     //рисую змейку
     Point p = new Point(4, 5, '*');
     Snake snake = new Snake(p, 4, Direction.RIGHT   );
     snake.Draw();
     //moving
     while (true)
     {
         if (Console.KeyAvailable)
         {
             ConsoleKeyInfo key = Console.ReadKey();
             snake.HandleKey(key.Key);
         }
         Thread.Sleep(100);
         snake.Move();
     }
 }
Example #2
0
        static void Main(string[] args)
        {
            Point p1 = new Point(1, 2, '*');
            p1.Draw();

            Point p2 = new Point(4, 5, '#');
            p2.Draw();

            Console.SetBufferSize(80, 25);

            HorizontalLine upLine = new HorizontalLine(0, 78, 0, '+');
            HorizontalLine downLine = new HorizontalLine(0, 78, 24, '+');
            upLine.Draw();
            downLine.Draw();

            VerticalLine leftLine = new VerticalLine(0, 24, 0, '+');
            VerticalLine rightLine = new VerticalLine(78, 24, 0, '+');
            leftLine.Draw();
            rightLine.Draw();

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

            while(true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.HandleKey(key.Key);
                }
                Thread.Sleep(100);
                snake.Move();
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            Console.SetBufferSize(80,25); //установить размер окна и убрать возможность перемотки

            Walls walls = new Walls(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())
                {
                    Console.Clear();
                    Console.SetCursorPosition(37, 7);
                    Console.Write("YOU LOSE");
                    break;
                }
                if(snake.Eat(food))
                {
                    food = foodCreator.CreateFood();
                    food.Draw();
                }
                else
                {
                    snake.Move();
                }

                Thread.Sleep(100);

                if (Console.KeyAvailable) //нажата ли какая-либо клавиша
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.HandleKey(key.Key);
                }
            }

            Console.ReadKey();
        }
Example #4
0
        static void Main(string[] args)
        {
            Console.SetBufferSize(80, 25);

            HorizontalLine top = new HorizontalLine(1, 78, 1, '+');
            top.Draw();
            HorizontalLine bottom = new HorizontalLine(1, 78, 24, '+');
            bottom.Draw();
            VerticalLine left = new VerticalLine(1, 1, 24, '+');
            left.Draw();
            VerticalLine right = new VerticalLine(78, 1, 24, '+');
            right.Draw();

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

            for (int i = 0; i < 10; i++)
            {
                snake.Move();
                Thread.Sleep(300);
            }

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    if (key.Key == ConsoleKey.LeftArrow)
                        snake.direction = Direction.LEFT;
                    else if (key.Key == ConsoleKey.RightArrow)
                        snake.direction = Direction.RIGHT;
                    else if (key.Key == ConsoleKey.UpArrow)
                        snake.direction = Direction.UP;
                    else if (key.Key == ConsoleKey.DownArrow)
                        snake.direction = Direction.DOWN;
                    else if (key.Key == ConsoleKey.Escape)
                        break;

                }
                Thread.Sleep(100);
                snake.Move();

            }
        }
Example #5
0
        static void Main(string[] args)
        {
            Console.SetBufferSize(80, 25);
            HorizontalLine upLine = new HorizontalLine(0,78,0,'+');
            HorizontalLine downLine = new HorizontalLine(0, 78, 24, '+');
            VerticalLine leftLine = new VerticalLine(0, 24, 0, '+');
            VerticalLine rightLine = new VerticalLine(0, 24, 78, '+');

            upLine.Draw();
            downLine.Draw();
            leftLine.Draw();
            rightLine.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 (snake.Eat(food))
                {
                    food = foodCreator.CreateFood();
                    food.Draw();
                }
                else
                {
                    snake.Move();
                }

                //if (Console.KeyAvailable)
                //{
                //    ConsoleKeyInfo key = Console.ReadKey();
                //    snake.HandleKey(key.Key);
                //}
                Thread.Sleep(300);
                //snake.Move();
            }
        }
Example #6
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);
            }
        }
Example #7
0
        static void Main(string[] args)
        {
            Walls walls = new Walls(80, 25);

            walls.Draw();

            //рисуем точки
            Point p     = new Point(4, 5, '*');
            Snake snake = new Snake(p, 4, Direction.RIGHT);

            snake.Draw();
            FoodCreator foodcreat = new FoodCreator(80, 25, '&');
            Point       food      = foodcreat.CreateFood();

            food.Draw();

            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    break;
                }
                if (snake.Eat(food))
                {
                    food = foodcreat.CreateFood();
                    food.Draw();
                }
                else
                {
                    snake.Move();
                }
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.Go(key.Key);
                }
                Thread.Sleep(100);
                snake.Move();
            }
        }
Example #8
0
        static public void Draw(SpriteBatch spriteBatch)
        {
            Vector2 vector;

            vector.X = 655;
            vector.Y = 215;
            Snake.Draw(spriteBatch);
            Apple.Draw(spriteBatch);
            if ((Mouse.GetState().X > 431) && (Mouse.GetState().X < 785) && (Mouse.GetState().Y > 434) && (Mouse.GetState().Y < 486))
            {
                spriteBatch.Draw(ripnew, Vector2.Zero, Color.White);
            }
            else
            if ((Mouse.GetState().X > 431) && (Mouse.GetState().X < 637) && (Mouse.GetState().Y > 524) && (Mouse.GetState().Y < 568))
            {
                spriteBatch.Draw(ripexit, Vector2.Zero, Color.White);
            }
            else
            {
                spriteBatch.Draw(rip, Vector2.Zero, Color.White);
            }
            spriteBatch.DrawString(font, Upmenu.Score.ToString(), vector, Color.White);
        }
Example #9
0
        static void Main(string[] args)
        {
            Console.SetWindowSize(80, 25);
            Console.SetBufferSize(80, 25);

            HorizontalLine topLine    = new HorizontalLine(0, 78, 0, '-');
            HorizontalLine bottomLine = new HorizontalLine(0, 78, 24, '-');

            VerticalLine leftLine  = new VerticalLine(0, 24, 0, '|');
            VerticalLine rightLine = new VerticalLine(0, 24, 78, '|');

            topLine.Draw();
            bottomLine.Draw();
            leftLine.Draw();
            rightLine.Draw();

            Point p = new Point(2, 3, '*');

            Snake snake = new Snake(p, 5, Direction.RIGHT);

            snake.Draw();

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();

                    snake.HandleKey(key.Key);
                }

                Thread.Sleep(100);
                snake.Move();
            }

            Console.ReadLine();
        }
Example #10
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);
            }
        }
Example #11
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);
            }
        }
Example #12
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);
            }
        }
Example #13
0
        static void Main(string[] args)
        {
            StreamWriter sw = new StreamWriter(@"C:\Users\local\Desktop\PP\week 5\players.txt", true);

            Console.CursorVisible = false;
            Console.SetWindowSize(70, 20);
            Snake snake = new Snake();
            Food  food  = new Food();
            Wall  wall  = new Wall();

            food.SetRandomPosition(wall, snake);
            int cnt = 0;

            bool res = true;

            while (res)
            {
                wall.LoadLevel();
                Console.Clear();
                snake.Draw();
                food.Draw();
                wall.Draw();



                ConsoleKeyInfo btn = Console.ReadKey();
                switch (btn.Key)
                {
                case ConsoleKey.UpArrow:
                    snake.Move(0, -1);
                    break;

                case ConsoleKey.DownArrow:
                    snake.Move(0, 1);
                    break;

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

                case ConsoleKey.RightArrow:
                    snake.Move(1, 0);
                    break;
                }
                cnt++;
                snake.Borders();
                if (snake.CanEat(food))
                {
                    food.Eat(snake, wall);
                }

                if (snake.GameOver(wall))
                {
                    Snake.EndofGame();
                    ConsoleKeyInfo bttn = Console.ReadKey();
                    switch (bttn.Key)
                    {
                    case ConsoleKey.Enter:
                        snake.body.Clear();
                        food.score = 0;
                        snake      = new Snake();
                        Console.Clear();
                        break;

                    case ConsoleKey.Q:
                        res = false;
                        Console.SetCursorPosition(49, 9);
                        Console.WriteLine("User name:");
                        Console.SetCursorPosition(49, 10);
                        string s = Console.ReadLine();
                        sw.WriteLine("User:"******"score:" + food.score);
                        sw.Close();
                        break;
                    }
                }
            }
        }
Example #14
0
        static void Main(string[] args)
        {
            int choise;
            int mode;
            int speed;

menu:
            Console.Clear();
            Console.WriteLine("\t$ ЗМЕЙКА ВОЛКОВА $\t telegram автора: serega_vlk\n\n");
            Console.WriteLine("1. играть");
            Console.WriteLine("2. выход");
            choise = Convert.ToInt32(Console.ReadLine());
            if (choise == 1)
            {
menu1:
                Console.Clear();
                Console.WriteLine("\t$ ЗМЕЙКА ВОЛКОВА $\t telegram автора: serega_vlk\n\n");
                Console.WriteLine("Выберите сложность\n");
                Console.WriteLine("1. Тренировка");
                Console.WriteLine("2. Арена");
                Console.WriteLine("3. Назад");
                choise = Convert.ToInt32(Console.ReadLine());
                if (choise == 1)
                {
                    Console.Clear();
                    mode = 1;
                }
                else if (choise == 2)
                {
                    Console.Clear();
                    mode = 2;
                }
                else if (choise == 3)
                {
                    Console.Clear();
                    goto menu;
                }
                else
                {
                    Console.Clear();
                    goto menu1;
                }
menu2:
                Console.Clear();
                Console.WriteLine("\t$ ЗМЕЙКА ВОЛКОВА $\t telegram автора: serega_vlk\n\n");
                Console.WriteLine("Выберите скорость\n");
                Console.WriteLine("1. Легкая - 130");
                Console.WriteLine("2. Средняя - 100");
                Console.WriteLine("3. Сложная - 70");
                Console.WriteLine("4. Хардкор - 40");
                Console.WriteLine("5. Назад");
                choise = Convert.ToInt32(Console.ReadLine());
                if (choise == 1)
                {
                    Console.Clear();
                    speed = 130;
                    goto start;
                }
                else if (choise == 2)
                {
                    Console.Clear();
                    speed = 100;
                    goto start;
                }
                else if (choise == 3)
                {
                    Console.Clear();
                    speed = 70;
                    goto start;
                }
                else if (choise == 4)
                {
                    Console.Clear();
                    speed = 40;
                    goto start;
                }
                else if (choise == 5)
                {
                    Console.Clear();
                    goto menu1;
                }
                else
                {
                    Console.Clear();
                    goto menu2;
                }
            }
            else if (choise == 2)
            {
                return;
            }
            else
            {
                Console.Clear();
                goto menu;
            }
start:
            int weight = 80;
            int  height = 25;
            Wall frames = new Wall(weight, height);

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

            snake.Draw();

            FoodCreator foodcreator = new FoodCreator(weight, height, '%');
            Point       food        = foodcreator.CreateFood();

            food.Draw();

            Console.SetCursorPosition(90, 0);
            Console.Write("Для выхода нажмите esc");
            Console.SetCursorPosition(90, 2);
            Console.Write("Сложность: " + mode);
            Console.SetCursorPosition(90, 3);
            Console.Write("Скорость: " + speed);

            int  counter = 0;
            bool check   = true;

            while (true)
            {
                if (snake.SnakeLenght() && check && mode == 2)
                {
                    weight = weight - 4;
                    height = height - 1;
                    Console.Clear();
                    foodcreator = new FoodCreator(weight - 5, height - 2, '%');
                    frames      = new Wall(weight, height);
                    frames.Draw();
                    food.Draw();
                    snake.Draw();
                    Console.SetCursorPosition(90, 0);
                    Console.Write("Для выхода нажмите esc");
                    Console.SetCursorPosition(90, 2);
                    Console.Write("Сложность: " + mode);
                    Console.SetCursorPosition(90, 3);
                    Console.Write("Скорость: " + speed);
                    check = false;
                }
                if (frames.IsHit(snake) || snake.IsHitTail())
                {
                    Console.SetCursorPosition(90, 15);
                    Console.Write("Вы проиграли(((");
                    Console.SetCursorPosition(90, 17);
                    Console.Write("Ваш счет: " + Convert.ToString(counter));
                    Console.SetCursorPosition(90, 19);
                    Console.Write("Нажмите esc для выхода в меню");
                    while (true)
                    {
                        ConsoleKeyInfo key1 = Console.ReadKey();
                        if (key1.Key == ConsoleKey.Escape)
                        {
                            break;
                        }
                    }
                    goto menu;
                }
                if (snake.Eat(food))
                {
                    counter++;
                    while (true)
                    {
                        food = foodcreator.CreateFood();
                        if (snake.IsHitPoint(food))
                        {
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }
                    food.Draw();
                    check = true;
                }
                else
                {
                    snake.Move();
                }
                Thread.Sleep(speed);
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    if (key.Key == ConsoleKey.LeftArrow)
                    {
                        snake.direction = Direction.LEFT;
                    }
                    else if (key.Key == ConsoleKey.RightArrow)
                    {
                        snake.direction = Direction.RIGHT;
                    }
                    else if (key.Key == ConsoleKey.UpArrow)
                    {
                        snake.direction = Direction.UP;
                    }
                    else if (key.Key == ConsoleKey.DownArrow)
                    {
                        snake.direction = Direction.DOWN;
                    }
                    else if (key.Key == ConsoleKey.Escape)
                    {
                        Console.Clear();
                        Console.SetCursorPosition(0, 0);
                        goto menu;
                    }
                }
            }
        }
Example #15
0
        static void Game()
        {
            snake.body.Add(new Point(20, 20));
            while (!gameOver)
            {
                if (cnt == 3)
                {
                    level++;
                    cnt  = 0;
                    wall = new Wall(level);
                }
                if (direction == 1)
                {
                    snake.Move(0, -1);
                }
                if (direction == 2)
                {
                    snake.Move(0, 1);
                }
                if (direction == 3)
                {
                    snake.Move(1, 0);
                }
                if (direction == 4)
                {
                    snake.Move(-1, 0);
                }

                if (direction1 == 1)
                {
                    snake1.Move(0, -1);
                }
                if (direction1 == 2)
                {
                    snake1.Move(0, 1);
                }
                if (direction1 == 3)
                {
                    snake1.Move(1, 0);
                }
                if (direction1 == 4)
                {
                    snake1.Move(-1, 0);
                }
                while (snake.Inthesnake(fruit.coordinates.x, fruit.coordinates.y) || snake.Inthewall(fruit.coordinates.x, fruit.coordinates.y, wall))
                {
                    fruit.FoodMaker();
                }
                if (snake.Bump() || snake.Collide(wall))
                {
                    Console.Clear();
                    Console.SetCursorPosition(50, 50);
                    Console.WriteLine("GAME OVER");
                    Console.ReadKey();
                    snake = new Snake();
                    snake.body.Add(new Point(20, 20));
                    level = 1;
                    score = 0;
                    cnt   = 0;
                    speed = 120;
                }

                if (snake.Eaten(fruit) == true)
                {
                    cnt++;
                    score++;
                }
                Console.Clear();
                snake.Draw();
                fruit.DrawFood();
                wall.Draw();
                Console.SetCursorPosition(150, 200);
                Console.Write("Score:" + score.ToString());
                Console.Write("Level:" + level);

                if (cnt % 3 == 0 && cnt != 0)
                {
                    speed = Math.Max(speed - 1, 1);
                }

                Thread.Sleep(speed);
            }
        }
Example #16
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter your name: ");
            string name = Console.ReadLine();

            Console.Clear();

            Console.SetWindowSize(80, 30);
            Random rnd   = new Random();
            int    level = 1;
            int    n     = rnd.Next(2, 75);
            int    m     = rnd.Next(2, 24);
            Wall   wall  = new Wall(level);
            Snake  snake = new Snake();
            food   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.Draw();
            snake.Draw();
            eat.Showfood(n, m);



            while (true)
            {
                ConsoleKeyInfo a = Console.ReadKey();

                if (a.Key == ConsoleKey.DownArrow)
                {
                    snake.Motion(0, 1);
                }
                if (a.Key == ConsoleKey.UpArrow)
                {
                    snake.Motion(0, -1);
                }
                if (a.Key == ConsoleKey.LeftArrow)
                {
                    snake.Motion(-1, 0);
                }
                if (a.Key == ConsoleKey.RightArrow)
                {
                    snake.Motion(1, 0);
                }
                if (a.Key == ConsoleKey.Q)
                {
                    int scoreofplayer = 0;
                    for (int i = 1; i < level; i++)
                    {
                        scoreofplayer += level * 5;
                    }
                    scoreofplayer += snake.a.Count - 1;
                    StreamReader sr = new StreamReader(@"D:\snakelevels\highestscore.txt");
                    string       h  = sr.ReadToEnd();

                    string[] b = h.Split('\n');
                    bool     k = false;
                    for (int i = 0; i < b.Length; i++)
                    {
                        int    index        = b[i].IndexOf(' ');
                        string testname     = b[i].Substring(0, index);
                        int    currentscore = Convert.ToInt32(b[i].Substring(index + 1, b[i].Length - index - 1));


                        if (testname == name)
                        {
                            k = true;

                            if (scoreofplayer > currentscore)
                            {
                                sr.Close();
                                StreamWriter sw = new StreamWriter(@"D:\snakelevels\highestscore.txt");
                                sw.WriteLine(name + ' ' + scoreofplayer);
                            }
                        }
                    }
                    if (k == false)
                    {
                        sr.Close();
                        StreamWriter sw = new StreamWriter(@"D:\snakelevels\highestscore.txt");
                        sw.WriteLine(name + ' ' + scoreofplayer);
                    }
                }
                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 (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))
                {
                    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();
                eat.Showfood(n, m);
                snake.Draw();
                wall.Draw();
                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));
            }
        }