Пример #1
0
        void DrawSnake()
        {
            SnakeLocation.ForEach((point) => {
                if (SnakeLocation.First().Equals(point))
                {
                    var head = new Head(UIColor.DarkGray,
                                        new SizeF((float)RectSize.Width, (float)RectSize.Height),
                                        (int)point.X,
                                        (int)point.Y);
                    Snake.Add(head);
                }
                else if (SnakeLocation.Last().Equals(point))
                {
                    var tail = new Tail(UIColor.White,
                                        new SizeF((float)RectSize.Width, (float)RectSize.Height),
                                        (int)point.X,
                                        (int)point.Y);
                    Snake.Add(tail);
                }
                else
                {
                    var body = new Body(UIColor.Gray,
                                        new SizeF((float)RectSize.Width, (float)RectSize.Height),
                                        (int)point.X,
                                        (int)point.Y);
                    Snake.Add(body);
                }
            });

            Snake.ForEach((sprite) => AddChild(sprite));
        }
Пример #2
0
        private void _gameTimer_Tick(object sender, EventArgs e)
        {
            Snake.Update();

            if ((_food != null) && (Snake.CanEatFood(_food)))
            {
                Snake.Add(5);
                addFood();
                _score++;
                _lbl.Text = string.Format("Очки: {0}", _score);
            }
            else
            {
                _lbl.Text = string.Format("Очки: {0}", _score);

                if (Snake.HitSelf())
                {
                    _failMessage = "Вы проиграли!";
                    _fail        = true;
                    _gameTimer.Stop();
                    Form2 f2 = new Form2();
                    f2.Show();
                }
                if (Snake.HitWall(this.Width, this.Height))
                {
                    _failMessage = "Вы проиграли";
                    _fail        = true;
                    _gameTimer.Stop();
                    Form2 f2 = new Form2();
                    f2.Show();
                }
            }
            this.Refresh();
        }
Пример #3
0
        static void RefreshField(object x)
        {
            if (snake.Over(w, h))
            {
                game = false; //change continue of game to false
                return;
            }
            if (snake.has(food_place))
            {
                score++;
                Point point = snake.body[snake.body.Count - 1];
                switch (snake.vectors[snake.vectors.Count - 1])
                {
                case MoveVector.BOTTOM:
                    point.y--;
                    break;

                case MoveVector.LEFT:
                    point.x++;
                    break;

                case MoveVector.RIGHT:
                    point.x--;
                    break;

                case MoveVector.TOP:
                    point.y++;
                    break;
                }
                snake.Add(point, snake.vectors[snake.vectors.Count - 1]); //there are bug
                while (snake.has(food_place))
                {
                    Random random = new Random();
                    food_place = new Point(random.Next(1, w - 2), random.Next(1, h - 2));
                } //check that any snake body element on place of food (sry for my English)
            }
            Console.Clear();
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    if (new Point(j, i) == food_place)
                    {
                        Console.Write('&');                                //draw food
                    }
                    else if (snake.has(new Point(j, i)))
                    {
                        Console.Write('@');                                  //draw snake
                    }
                    else
                    {
                        Console.Write(field[j, i]);  //draw default
                    }
                }
                Console.WriteLine();
            } //(re)draw field
            Console.Title = $"Console Snake v {Assembly.GetEntryAssembly().GetName().Version} by snaulX. Your score = {score}"; //refresh title
            snake.Move(vector); //snake move
        }
Пример #4
0
 //--------------------------------------------------------------------------------------------
 #region Update
 public void Update(TimeSpan czas)
 {
     this.czas = czas;
     if (czyMenu == true)
     {
         menu.Update();
     }
     else
     {
         if (isPause == true && isGameOver == false)
         {
             kierunek = SnakeControl();
             if (waz.Kierunek != kierunek)
             {
                 if (czas.Milliseconds % speed == 0)
                 {
                     if (waz.ChangeDirection(kierunek) == true /*|| waz.CzyWSiebie() == true*/)
                     {
                         isGameOver = true;
                     }
                 }
             }
             else
             {
                 if (czas.Milliseconds % speed == 0)
                 {
                     if (waz.Move() == true /*|| waz.CzyWSiebie() == true*/)
                     {
                         isGameOver = true;
                     }
                 }
             }
         }
         if (Keyboard.GetState().IsKeyDown(Keys.Escape))
         {
             isPause = false;
         }
         if (isGameOver == true)
         {
             int LowestScore = HighList.Min(z => z.points);
             if (score > LowestScore)
             {
                 czyMenu = true;
                 menu    = new Menu(this, GetGameOver());
             }
             else
             {
                 czyMenu = true;
                 menu    = new Menu(this, GetOver());
             }
         }
         if (CzyJablko(waz.GlowaPolozenie, jablko.ApplePosition) == true)
         {
             waz.Add();
             score += 10;
             jablko.RandomPositionApple(waz.Poleznia());
         }
     }
 }
Пример #5
0
        public static void func()
        {
            while (!gameOver)
            {
                if (direction == 3)
                {
                    snake.Move(0, 1);
                }
                if (direction == 4)
                {
                    snake.Move(0, -1);
                }
                if (direction == 2)
                {
                    snake.Move(-1, 0);
                }
                if (direction == 1)
                {
                    snake.Move(1, 0);
                }

                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, 30);
                            food = new Food(_x, _y);
                            continue;
                        }
                    }
                    break;
                }
                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);
                    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, 30);
                                food = new Food(_x, _y);
                                continue;
                            }
                        }
                        break;
                    }
                    food = new Food(_x, _y);
                }
                if (k % 5 == 0)
                {
                    k++;
                    lvl++;
                    wall = new Wall(lvl);
                }
                if (gameOver)
                {
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.SetCursorPosition(screenwidth / 2, screenheight / 2);
                    Console.WriteLine("GAME OVER!");
                    Console.SetCursorPosition(screenwidth / 2, screenheight / 2 + 1);
                    Console.WriteLine("score:" + (k - lvl));
                    Console.ReadKey();
                    break;
                }

                Console.Clear();
                snake.Draw();
                wall.Draw();
                food.Draw(_x, _y);
                Thread.Sleep(speed);
            }
        }
Пример #6
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();
            }
        }