Пример #1
4
        public static void Init()
        {
            isActive = true;
            snake = new Snake();
            food = new Food();
            wall = new Wall();

            snake.body.Add(new Point { x = 15, y = 14 });
            food.body.Add(new Point { x = 14, y = 15 });

            food.color = ConsoleColor.Blue;
            wall.color = ConsoleColor.Green;
            snake.color = ConsoleColor.Yellow;

            Console.SetWindowSize(48, 19);
        }
Пример #2
0
        static void Main(string[] args)
        {
            Console.SetBufferSize(80, 25);

            Wall wall = new Wall(80, 25, '%');
            wall.Drow();
            /*VerticalLine vert_line_left = new VerticalLine(24, 0, 79, '%');
            vert_line_left.Drow();

            VerticalLine vert_line_right = new VerticalLine(24, 0, 0, '%');
            vert_line_right.Drow();

            HorizontalLine hor_line_bottom = new HorizontalLine(0, 78, 24, '%');
            hor_line_bottom.Drow();

            HorizontalLine hor_line_top = new HorizontalLine(0, 78, 0, '%');
            hor_line_top.Drow();*/

            Point start = new Point(15, 5, '*');
            Snake snake = new Snake(start, 8, Direction.RIGHT);
            snake.Drow();

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

            while(true)
            {
                if(wall.IsHit(snake) || snake.IsHitTail() )
                {
                    break;
                }
                if(snake.Eat(food))
                {
                    food = createfood.CreateFood();
                    food.Drow();
                }
                if(Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.HandleKey(key);
                }
                Thread.Sleep(100);
                snake.Move();
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            Console.SetBufferSize(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(100);

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.HandleKey(key.Key);
                }
            }
            WriteGameOver();
            Console.ReadLine();
        }
Пример #4
0
        void initWall()
        {
            _walls = new List<Wall>();

            Wall wall1 = new Wall(19,15,3,30);
            grid.Children.Add(wall1.Rect);
            Grid.SetColumn(wall1.Rect, wall1.X);
            Grid.SetRow(wall1.Rect, wall1.Y);
            Grid.SetColumnSpan(wall1.Rect, wall1.Width);
            Grid.SetRowSpan(wall1.Rect, wall1.Height);
            _walls.Add(wall1);

            Wall wall2 = new Wall(39, 15, 3, 30);
            grid.Children.Add(wall2.Rect);
            Grid.SetColumn(wall2.Rect, wall2.X);
            Grid.SetRow(wall2.Rect, wall2.Y);
            Grid.SetColumnSpan(wall2.Rect, wall2.Width);
            Grid.SetRowSpan(wall2.Rect, wall2.Height);
            _walls.Add(wall2);

            Wall wall3 = new Wall(59, 15, 3, 30);
            grid.Children.Add(wall3.Rect);
            Grid.SetColumn(wall3.Rect, wall3.X);
            Grid.SetRow(wall3.Rect, wall3.Y);
            Grid.SetColumnSpan(wall3.Rect, wall3.Width);
            Grid.SetRowSpan(wall3.Rect, wall3.Height);
            _walls.Add(wall3);
        }
Пример #5
0
        public static void Main(string[] args)
        {
            string       username  = Console.ReadLine();
            StreamWriter save      = new StreamWriter(@"/Users/macbook/Documents/PP2/Savage.txt");
            int          level     = 1;
            int          nextlevel = 0;
            int          score     = 1;

            Snake snake = new Snake();
            Wall  wall  = new Wall(level);
            Food  food  = new Food();

            Console.BackgroundColor = ConsoleColor.Black;
            Console.CursorVisible   = false;
            while (true)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey();
                if (keyInfo.Key == ConsoleKey.UpArrow)
                {
                    snake.Move(0, -1);
                }
                if (keyInfo.Key == ConsoleKey.DownArrow)
                {
                    snake.Move(0, 1);
                }
                if (keyInfo.Key == ConsoleKey.RightArrow)
                {
                    snake.Move(1, 0);
                }
                if (keyInfo.Key == ConsoleKey.LeftArrow)
                {
                    snake.Move(-1, 0);
                }



                if (snake.CollisionWithWall(wall))
                {
                    Console.Clear();
                    Console.SetCursorPosition(10, 10);
                    Console.WriteLine("GAME OVER!!!!");
                    Console.ReadKey();
                    snake = new Snake();
                    wall  = new Wall(level);
                }



                if (snake.CollisionWithFood(food))
                {
                    food.RandomFood();
                    nextlevel++;
                }
                if (nextlevel == 3)
                {
                    score++;
                    level++;
                    wall      = new Wall(level);
                    nextlevel = 0;
                    snake     = new Snake();
                    food      = new Food();
                }
                if (snake.CollisionWithWall(wall))
                {
                    save.WriteLine(username + " " + score * nextlevel);
                    save.Close();
                }

                Console.Clear();
                snake.Draw();
                wall.Draw();
                food.Setup();
                //Console.WriteLine(score*nextlevel);
            }
        }
Пример #6
0
        public void OpenMenu()
        {
            int           cursor = 0;
            List <string> menu   = new List <string>();

            menu.Add("Start game");
            menu.Add("Exit");
            while (true)
            {
                for (int i = 0; i < 2; i++)
                {
                    if (i == cursor)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    Console.WriteLine(menu[i]);
                }
                ConsoleKeyInfo key = Console.ReadKey();
                if (key.Key == ConsoleKey.UpArrow)
                {
                    cursor--;
                }
                if (key.Key == ConsoleKey.DownArrow)
                {
                    cursor++;
                }
                if (cursor == -1)
                {
                    cursor = 1;
                }
                if (cursor == 2)
                {
                    cursor = 0;
                }
                Console.Clear();
                for (int i = 0; i < 2; i++)
                {
                    if (i == cursor)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    Console.WriteLine(menu[i]);
                }
                Console.Clear();
                if (key.Key == ConsoleKey.Enter)
                {
                    if (cursor == 1)
                    {
                        break;
                    }
                    if (cursor == 0)
                    {
                        x        = 1; y = 0; sc = 0; levelCount = 1; score = 0;
                        wall     = new Wall();
                        snake    = new Snake();
                        playGame = true; moovable = false;
                        food     = new Food();
                        Thread t = new Thread(MoveSnakeThread);
                        t.Start();
                        while (true)
                        {
                            ConsoleKeyInfo k = new ConsoleKeyInfo();
                            k = Console.ReadKey();
                            if (k.Key == ConsoleKey.Escape)
                            {
                                break;
                            }
                            if ((k.Key == ConsoleKey.UpArrow || k.Key == ConsoleKey.W))
                            {
                                y = -1;
                                x = 0;
                            }
                            if ((k.Key == ConsoleKey.DownArrow || k.Key == ConsoleKey.S))
                            {
                                y = 1;
                                x = 0;
                            }
                            if ((k.Key == ConsoleKey.LeftArrow || k.Key == ConsoleKey.A))
                            {
                                x = -1;
                                y = 0;
                            }
                            if (k.Key == ConsoleKey.RightArrow || k.Key == ConsoleKey.D)
                            {
                                x = 1;
                                y = 0;
                            }
                        }
                        Console.Clear();
                        playGame = false;
                    }
                }
            }
        }
Пример #7
0
        static void Main(string[] args)
        {
            Console.WindowHeight = 40;
            Console.WindowWidth  = 70;
            Snake snake = new Snake();
            int   lvl   = 1;
            Wall  wall  = new Wall(lvl);

            Console.CursorVisible = false;
            Thread thread = new Thread(func);

            thread.Start();

            Random rnd  = new Random();
            int    _x   = rnd.Next(1, 70);
            int    _y   = rnd.Next(1, 20);
            Food   food = new Food(_x, _y);


            while (true)
            {
                foreach (Point p in wall.body)
                {
                    if (p.x == food.body[0].x && p.y == food.body[0].y)
                    {
                        rnd  = new Random();
                        _x   = rnd.Next(1, 70);
                        _y   = rnd.Next(1, 20);
                        food = new Food(_x, _y);
                        continue;
                    }
                }
                for (int p = 1; p <= snake.body_cnt; p++)
                {
                    if (snake.body[p].x == food.body[0].x && snake.body[p].y == food.body[0].y)
                    {
                        rnd  = new Random();
                        _x   = rnd.Next(1, 70);
                        _y   = rnd.Next(1, 20);
                        food = new Food(_x, _y);
                        continue;
                    }
                }
                break;
            }
            int k = 1;

            while (!gameOver)
            {
                while (true)
                {
                    ConsoleKeyInfo keyInfo = Console.ReadKey();
                    if (keyInfo.Key == ConsoleKey.DownArrow && direction != 4)
                    {
                        direction = 3;
                    }
                    if (keyInfo.Key == ConsoleKey.UpArrow && direction != 3)
                    {
                        direction = 4;
                    }
                    if (keyInfo.Key == ConsoleKey.LeftArrow && direction != 1)
                    {
                        direction = 2;
                    }
                    if (keyInfo.Key == ConsoleKey.RightArrow && direction != 2)
                    {
                        direction = 1;
                    }

                    if (keyInfo.Key == ConsoleKey.R)
                    {
                        level = 1;
                        snake = new Snake();
                        wall  = new Wall(level);
                    }
                    if (keyInfo.Key == ConsoleKey.Escape)
                    {
                        gameOver = true;
                    }
                    foreach (Point p in wall.body)
                    {
                        if (p.x == snake.body[0].x && p.y == snake.body[0].y)
                        {
                            gameOver = true;
                        }
                    }
                    for (int p = 1; p <= snake.body_cnt; p++)
                    {
                        if (snake.body[p].x == snake.body[0].x && snake.body[p].y == snake.body[0].y)
                        {
                            gameOver = true;
                        }
                    }


                    if (food.body[0].x == snake.body[0].x && food.body[0].y == snake.body[0].y)
                    {
                        k++;
                        snake.Add();
                        _x   = rnd.Next(1, 70);
                        _y   = rnd.Next(1, 20);
                        food = new Food(_x, _y);
                        bool ok = true;
                        while (ok)
                        {
                            foreach (Point p in wall.body)
                            {
                                if (p.x == food.body[0].x && p.y == food.body[0].y)
                                {
                                    _x   = rnd.Next(1, 70);
                                    _y   = rnd.Next(1, 20);
                                    food = new Food(_x, _y);
                                }
                            }
                            for (int p = 1; p <= snake.body_cnt; p++)
                            {
                                if (snake.body[p].x == food.body[0].x && snake.body[p].y == food.body[0].y)
                                {
                                    _x   = rnd.Next(1, 70);
                                    _y   = rnd.Next(1, 20);
                                    food = new Food(_x, _y);
                                }
                            }
                            ok = false;
                        }
                    }
                    if (k % 5 == 0)
                    {
                        k = 1;
                        lvl++;
                        wall = new Wall(lvl);
                    }
                }
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.White;
                Console.SetCursorPosition(35, 12);
                Console.WriteLine("GAME OVER!");
                Console.ReadKey();
            }
        }
Пример #8
0
        public static void func()
        {
            while (true)
            {
                if ((t == snake.body[0].x && s == snake.body[0].y))
                {
                    snake.body.Add(new Point(t, s));
                    score++;
                    speed = Math.Max(50, speed - 25);
                    // t = rdm.Next(0, 54);
                    //s = rdm.Next(0, 24);

                    CreateFood();
                    if (score % 3 == 0)
                    {
                        level++;
                        Console.Clear();
                        for (int i = 0; i < snake.body.Count; ++i)
                        {
                            snake.body[i].x = i + 10;
                            snake.body[i].y = 15;
                        }
                        wall = new Wall(level);
                    }
                }
                if (direction == 8)
                {
                    snake.Move(0, -1);
                }
                if (direction == 2)
                {
                    snake.Move(0, 1);
                }
                if (direction == 6)
                {
                    snake.Move(1, 0);
                }
                if (direction == 4)
                {
                    snake.Move(-1, 0);
                }
                if (snake.ColllisionWithWall(wall) || snake.Collision())
                {
                    Console.Clear();
                    Console.SetCursorPosition(20, 10);
                    Console.WriteLine("GAME OVER!!!");
                    speed = 300;
                    F1(record);
                    score = 0;
                    Console.ReadKey();
                    Console.Clear();
                    snake = new Snake();
                    level = 1;
                    wall  = new Wall(level);
                }

                Console.SetCursorPosition(t, s);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Q");
                snake.Draw();
                wall.Draw();
                Thread.Sleep(speed);
            }
        }
Пример #9
0
        static void Main(string[] args)
        {
            record = F2();
            Console.CursorVisible = false;
            Console.SetWindowSize(60, 30);

            Console.SetCursorPosition(5, 10);
            Console.WriteLine("Enter your name : ");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.SetCursorPosition(25, 10);
            string name = Console.ReadLine();

            Console.Clear();
            Console.WriteLine("If you want to continue type 2");
            ConsoleKeyInfo ki = Console.ReadKey();

            Console.Clear();
            if (ki.Key == ConsoleKey.NumPad2)
            {
                snake = F4();
                wall  = F6();
            }

            Thread thread = new Thread(func);

            thread.Start();

            Console.SetCursorPosition(t, s);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Q");

            while (true)
            {
                Console.SetCursorPosition(1, 27);
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine(name);
                Console.SetCursorPosition(name.Length + 2, 27);
                record = Math.Max(record, score);
                Console.WriteLine("Score : " + score);
                Console.WriteLine("Record: " + record);
                ConsoleKeyInfo k = Console.ReadKey();

                if (k.Key == ConsoleKey.UpArrow && direction != 2)
                {
                    direction = 8;
                }
                if (k.Key == ConsoleKey.DownArrow && direction != 8)
                {
                    direction = 2;
                }
                if (k.Key == ConsoleKey.RightArrow && direction != 4)
                {
                    direction = 6;
                }
                if (k.Key == ConsoleKey.LeftArrow && direction != 6)
                {
                    direction = 4;
                }
                if (k.Key == ConsoleKey.S)
                {
                    F3(snake);
                    F5(wall);
                }
            }
        }
Пример #10
0
        public void Start()
        {
            Load();


            Thread t = new Thread(new ThreadStart(worm.Move));

            t.Start();

            while (true)
            {
                wall.Save();
                if (score == 500 && nextLevel == true)
                {
                    level = level + 1;
                    wall.Clear();
                    wall.Generate(level);
                    wall.Draw();
                    nextLevel = false;
                    score     = 0;

                    worm.Clear();
                    Point p = new Point();
                    p = worm.points[0];
                    worm.points.Clear();
                    worm.points.Add(p);
                }

                ConsoleKeyInfo pressedKey = Console.ReadKey();
                switch (pressedKey.Key)
                {
                case ConsoleKey.F2:
                    this.Save();
                    break;

                case ConsoleKey.F3:
                    wall = wall.Load() as Wall;
                    wall.Draw();
                    worm.Clear();
                    worm = worm.Load() as Worm;
                    worm.AttachGameLink(this);
                    t.Abort();

                    t = new Thread(new ThreadStart(worm.Move));
                    t.Start();
                    break;

                case ConsoleKey.UpArrow:
                    worm.dx = 0;
                    worm.dy = -1;
                    break;

                case ConsoleKey.DownArrow:
                    worm.dx = 0;
                    worm.dy = 1;
                    break;

                case ConsoleKey.LeftArrow:
                    worm.dx = -1;
                    worm.dy = 0;
                    break;

                case ConsoleKey.RightArrow:
                    worm.dx = 1;
                    worm.dy = 0;
                    break;

                case ConsoleKey.Escape:
                    break;
                }
            }
        }
Пример #11
0
 public void Start()
 {
     snake = new Snake();
     food  = new Food();
     wall  = new Wall(level);
 }
Пример #12
0
 public Fruit(Wall w)
 {
     this.w = w;
     FoodMaker(w);
 }
Пример #13
0
        static void Main(string[] args)
        {
VeryBegin:
            int l = 1;

            /*
             * while (true)
             * {
             *   Console.Clear();
             *   Console.WriteLine("Chose level (1-5)");
             *   l = int.Parse(Console.ReadLine());
             *   if (l > 0 && l <= 5)
             *       break;
             * }
             */
Begin:
            int score = 0;

            Wall wall = new Wall(l);
            Worm worm = new Worm();
            Food food = new Food();

            worm.Start();

            string way = "none";

            bool f0 = true;

            while (f0 == true)
            {
                worm = new Worm();
                food = new Food();
                worm.Start();
                f0 = false;
                for (int i = 0; i < wall.bricks.Count; i++)
                {
                    if (wall.bricks[i].Equals(worm.body[0]))
                    {
                        f0 = true;
                    }
                }
            }

            bool s0 = true;
            bool s1 = true;

            while (s0 == true || s1 == true)
            {
                worm = new Worm();
                worm.Start();
                s0 = false;
                s1 = false;
                for (int i = 0; i < wall.bricks.Count; i++)
                {
                    if (wall.bricks[i].Equals(worm.body[0]))
                    {
                        s0 = true;
                    }
                }
                if (worm.body[0].Equals(food.location))
                {
                    s1 = true;
                }
            }
            while (worm.isAlive)
            {
                Console.Clear();
                worm.Draw();
                food.Draw();
                wall.Draw();
                Console.WriteLine("\n\nScore: {0}", score);
                if (score >= 10)
                {
                    Console.WriteLine("\n\n Next level availible");
                }

                ConsoleKeyInfo pressedKey = Console.ReadKey();
                switch (pressedKey.Key)
                {
                case ConsoleKey.UpArrow:
                    if (way != "down" || worm.body.Count == 1)
                    {
                        worm.Move(0, -1);
                        way = "up";
                    }
                    break;

                case ConsoleKey.DownArrow:
                    if (way != "up" || worm.body.Count == 1)
                    {
                        worm.Move(0, 1);
                        way = "down";
                    }
                    break;

                case ConsoleKey.LeftArrow:
                    if (way != "right" || worm.body.Count == 1)
                    {
                        worm.Move(-1, 0);
                        way = "left";
                    }
                    break;

                case ConsoleKey.RightArrow:
                    if (way != "left" || worm.body.Count == 1)
                    {
                        worm.Move(1, 0);
                        way = "right";
                    }
                    break;

                case ConsoleKey.Escape:
                    worm.isAlive = false;
                    Console.Clear();
                    goto End;

                case ConsoleKey.F5:
                    Game game = new Game(food, wall, worm, score, l);
                    Serialize(game);
                    break;

                case ConsoleKey.F9:
                    Game game2 = Deserialize();
                    food  = game2.food;
                    wall  = game2.wall;
                    worm  = game2.worm;
                    score = game2.score;
                    l     = game2.l;
                    break;

                case ConsoleKey.OemPlus:
                    if (score >= 10)
                    {
                        l++;
                        goto Begin;
                    }
                    break;

                case ConsoleKey.OemMinus:
                    if (l > 1)
                    {
                        l--;
                        goto Begin;
                    }
                    break;
                }

                for (int i = 0; i < wall.bricks.Count; i++)
                {
                    if (wall.bricks[i].Equals(worm.body[0]))
                    {
                        worm.isAlive = false;
                    }
                }

                for (int i = 1; i < worm.body.Count; i++)
                {
                    if (worm.body[0].Equals(worm.body[i]))
                    {
                        worm.isAlive = false;
                    }
                }

                if (worm.CanEat(food))
                {
                    score++;
                    bool f1 = true;
                    bool f2 = true;
                    while (f1 == true || f2 == true)
                    {
                        f1   = false;
                        f2   = false;
                        food = new Food();
                        for (int i = 0; i < worm.body.Count; i++)
                        {
                            if (worm.body[i].Equals(food.location))
                            {
                                f1 = true;
                            }
                        }

                        for (int i = 0; i < wall.bricks.Count; i++)
                        {
                            if (wall.bricks[i].Equals(food.location))
                            {
                                f2 = true;
                            }
                        }
                    }
                }
            }

            Console.Clear();
            Console.WriteLine("GAME OVER\n\n Again?");
            Console.ReadKey();
            goto VeryBegin;
End:
            Console.WriteLine("GAME OVER");
        }
Пример #14
0
        public static void Init()
        {
            exit  = Over = false;
            pause = true;

            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("ENTER YOUR USERNAME: "******"Please enter a non-empty username with no more than 16 letters!";

            while (username.Length > 16 || username.Length == 0)
            {
                if (wrongForFirstTime)
                {
                    Console.WriteLine(message);
                    wrongForFirstTime = false;
                }
                Console.SetCursorPosition(21, 0);

                for (int i = 0; i < username.Length; i++)
                {
                    Console.Write(' ');
                }

                Console.SetCursorPosition(21, 0);
                username = Console.ReadLine();
            }

            string        path      = @"C:\snake game\usernames\" + username + "\\saves";
            DirectoryInfo directory = new DirectoryInfo(path);

            Console.ForegroundColor = ConsoleColor.Cyan;

            List <string> options = new List <string>();

            options.Add("New game");
            if (directory.Exists && directory.GetDirectories().Length > 0)
            {
                options.Add("Load game");
            }
            if (new FileInfo(@"C:\snake game\usernames\" + username + "\\highscores.txt").Exists)
            {
                options.Add("List of highscores");
            }
            options.Add("Exit");
            int option = 0;

            while (true)
            {
                bool resumeCycle = false;

                while (true)
                {
                    Console.Clear();
                    Console.WriteLine("MAIN MENU\n");
                    for (int i = 0; i < options.Count; i++)
                    {
                        Console.BackgroundColor = i == option ? ConsoleColor.White : ConsoleColor.Black;
                        Console.WriteLine(options[i]);
                    }

                    ConsoleKey key = Console.ReadKey(true).Key;
                    switch (key)
                    {
                    case ConsoleKey.UpArrow:
                        if (option > 0)
                        {
                            option--;
                        }
                        break;

                    case ConsoleKey.DownArrow:
                        if (option < options.Count - 1)
                        {
                            option++;
                        }
                        break;
                    }

                    if (key == ConsoleKey.Enter)
                    {
                        break;
                    }
                }

                if (options[option] == "Load game")
                {
                    Console.Clear();
                    string saveName;
                    Console.WriteLine("Load game\n");

                    DirectoryInfo   dir  = new DirectoryInfo(path);
                    DirectoryInfo[] dirs = dir.GetDirectories();

                    for (int i = 0; i < dirs.Length; i++)
                    {
                        for (int j = i + 1; j < dirs.Length; j++)
                        {
                            if (dirs[i].CreationTime < dirs[j].CreationTime)
                            {
                                DirectoryInfo d = dirs[j];
                                dirs[j] = dirs[i];
                                dirs[i] = d;
                            }
                        }
                    }

                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    for (int i = 0; i < dirs.Length; i++)
                    {
                        Console.WriteLine(dirs[i].Name);
                    }
                    int saveNum = 0;

                    while (true)
                    {
                        Console.SetCursorPosition(0, 2 + saveNum);
                        Console.BackgroundColor = ConsoleColor.White;
                        Console.WriteLine(dirs[saveNum]);

                        int        prevSaveNum = saveNum;
                        ConsoleKey key         = Console.ReadKey(true).Key;
                        switch (key)
                        {
                        case ConsoleKey.UpArrow:
                            if (saveNum > 0)
                            {
                                saveNum--;
                            }
                            break;

                        case ConsoleKey.DownArrow:
                            if (saveNum < dirs.Length - 1)
                            {
                                saveNum++;
                            }
                            break;
                        }

                        if (key == ConsoleKey.Enter)
                        {
                            break;
                        }

                        Console.SetCursorPosition(0, 2 + prevSaveNum);
                        Console.BackgroundColor = ConsoleColor.Black;
                        Console.WriteLine(dirs[prevSaveNum]);
                    }

                    saveName = dirs[saveNum].Name;

                    Console.Clear();

                    path += "\\" + saveName;

                    string fSnake = path + "\\snake.xml";
                    string fWall  = path + "\\wall.xml";
                    string fFood  = path + "\\food.xml";

                    FileStream fs = new FileStream(fSnake, FileMode.Open);
                    snake = new XmlSerializer(typeof(Snake)).Deserialize(fs) as Snake;
                    fs.Close();

                    fs   = new FileStream(fWall, FileMode.Open);
                    wall = new XmlSerializer(typeof(Wall)).Deserialize(fs) as Wall;
                    fs.Close();

                    fs   = new FileStream(fFood, FileMode.Open);
                    food = new XmlSerializer(typeof(Food)).Deserialize(fs) as Food;
                    fs.Close();

                    Console.BackgroundColor = ConsoleColor.Black;
                }

                else if (options[option] == "New game")
                {
                    DirectoryInfo dir = new DirectoryInfo(@"C:\Snake Game\usernames\" + username);
                    if (!dir.Exists)
                    {
                        dir.Create();
                    }

                    FileStream   highscore = new FileStream(dir.FullName + @"\highscores.txt", FileMode.OpenOrCreate);
                    StreamReader sr        = new StreamReader(highscore);
                    string       line      = sr.ReadLine();
                    sr.Close();

                    if (line == null)
                    {
                        StreamWriter sw = new StreamWriter(dir.FullName + @"\highscores.txt");
                        sw.Write("1)  " + 0);
                        sw.Close();
                    }
                    highscore.Close();

                    snake = new Snake(username);
                    wall  = new Wall();
                    food  = new Food();
                }

                else if (options[option] == "List of highscores")
                {
                    Console.Clear();
                    string       hsPath = @"C:\snake game\usernames\" + username + "\\highscores.txt";
                    StreamReader sr     = new StreamReader(hsPath);
                    Console.Write(sr.ReadToEnd());
                    sr.Close();

                    while (Console.ReadKey(true).Key != ConsoleKey.Escape)
                    {
                    }

                    resumeCycle = true;
                }
                else if (options[option] == "Exit")
                {
                    exit = true;
                }
                if (!resumeCycle)
                {
                    break;
                }
            }
        }
Пример #15
0
        public void OpenMenu()
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Адиль\Desktop\PP2_LABS\Snake\Levels");

            maxLevel = directoryInfo.GetFileSystemInfos().Length;
            int           cursor = 0;
            List <string> menu   = new List <string>();

            menu.Add("Start game");
            menu.Add("Leader board");
            menu.Add("Choose difficulty level");
            menu.Add("Exit");
            while (true)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (i == cursor)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    Console.WriteLine(menu[i]);
                }
                ConsoleKeyInfo key = Console.ReadKey();
                if (key.Key == ConsoleKey.UpArrow)
                {
                    cursor--;
                }
                if (key.Key == ConsoleKey.DownArrow)
                {
                    cursor++;
                }
                if (cursor == -1)
                {
                    cursor = 3;
                }
                if (cursor == 4)
                {
                    cursor = 0;
                }
                Console.Clear();
                for (int i = 0; i < 4; i++)
                {
                    if (i == cursor)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    Console.WriteLine(menu[i]);
                }
                Console.Clear();
                if (key.Key == ConsoleKey.Enter)
                {
                    if (cursor == 1)
                    {
                        highScores = Sort(highScores);
                        for (int i = 0; i < highScores.name.Count; i++)
                        {
                            Console.Write(highScores.name[i]);
                            Console.Write(" - ");
                            Console.WriteLine(highScores.score[i]);
                        }
                        while (true)
                        {
                            ConsoleKeyInfo k = Console.ReadKey();
                            if (k.Key == ConsoleKey.Escape)
                            {
                                Console.Clear();
                                break;
                            }
                        }
                    }
                    if (cursor == 2)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Press button to choose difficulty level");
                        Console.Write("Easy   : ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.Write("OO");
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("O");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("  --->  Q");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("Normal : ");
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.Write("OO");
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.Write("O");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("  --->  W");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("Hard   : ");
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                        Console.Write("OO");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write("O");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("  --->  E");
                        ConsoleKeyInfo k = Console.ReadKey();
                        if (k.Key == ConsoleKey.Q)
                        {
                            snakeBodyColor = ConsoleColor.Gray;
                            snakeHeadColor = ConsoleColor.Yellow;
                            speed          = 200;
                        }
                        if (k.Key == ConsoleKey.W)
                        {
                            snakeBodyColor = ConsoleColor.Cyan;
                            snakeHeadColor = ConsoleColor.Magenta;
                            speed          = 180;
                        }
                        if (k.Key == ConsoleKey.E)
                        {
                            snakeBodyColor = ConsoleColor.DarkCyan;
                            snakeHeadColor = ConsoleColor.Green;
                            speed          = 160;
                        }
                        Console.Clear();
                    }
                    if (cursor == 3)
                    {
                        break;
                    }
                    if (cursor == 0)
                    {
                        x        = 1; y = 0; sc = 0; levelCount = 1; score = 0;
                        wall     = new Wall();
                        snake    = new Snake();
                        playGame = true; moovable = false;
                        food     = new Food();
                        Thread t = new Thread(MoveSnakeThread);
                        t.Start();
                        while (true)
                        {
                            ConsoleKeyInfo k = new ConsoleKeyInfo();
                            k = Console.ReadKey();
                            if (k.Key == ConsoleKey.Escape)
                            {
                                break;
                            }
                            if ((k.Key == ConsoleKey.UpArrow || k.Key == ConsoleKey.W) && y != 1)
                            {
                                y = -1;
                                x = 0;
                            }
                            if ((k.Key == ConsoleKey.DownArrow || k.Key == ConsoleKey.S) && y != -1)
                            {
                                y = 1;
                                x = 0;
                            }
                            if ((k.Key == ConsoleKey.LeftArrow || k.Key == ConsoleKey.A) && x != 1)
                            {
                                x = -1;
                                y = 0;
                            }
                            if ((k.Key == ConsoleKey.RightArrow || k.Key == ConsoleKey.D) && x != -1)
                            {
                                x = 1;
                                y = 0;
                            }
                        }
                        Console.Clear();
                        playGame = false;
                    }
                }
            }
        }