Пример #1
4
        //when snake eat food and generate a new one
        public void foodCollision(Snake snake)
        {
            bool flag = snake.Collision(food.Position.X, food.Position.Y, snake.Head);
            if (flag)
            {
                snake.BodyGrow();
                score += 1;

                foodCount++;
                do
                {
                    food = new Food();

                } while (snake.Collision(food.Position.X, food.Position.Y, snake.Head));
            }
        }
Пример #2
0
      //static int record = 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 = 100;
                  //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);
          }
      }
Пример #3
0
 //snake eat specialFood
 public void SpecialfoodCollision(Snake snake)
 {
     if (specialFood!=null&&snake.Collision(specialFood.Position.X, specialFood.Position.Y, snake.Head))
     {
         snake.BodyGrow();
         snake.BodyGrow();
         score += 2;
         specialFood = null;
     }
 }
Пример #4
0
        public static void PlayGame()
        {
            while (true)
            {
                if (direction == 1)
                {
                    snake.Move(-1, 0);
                }
                if (direction == 2)
                {
                    snake.Move(1, 0);
                }
                if (direction == 3)
                {
                    snake.Move(0, -1);
                }
                if (direction == 4)
                {
                    snake.Move(0, 1);
                }

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


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

                    Console.ReadKey();

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

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

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

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

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

                    while (!food.foodonwall(wall) || !food.foononsnake(snake))
                    {
                        food.SetRandomPos();
                        Console.SetCursorPosition(food.x, food.y);
                        Console.Write("$");
                    }
                }
                // Console.Clear();
                snake.Draw();
                food.ShowFood();
                wall.WallDraw();
                Thread.Sleep(speed);
            }
        }
Пример #5
0
        //static int qm, ql, qcnt, qs, qd, qspeed, qsp;


        static void Game()
        {
            while (!gameover)
            {
                if (cnt == 4)
                {
                    level++;
                    map++;
                    if (map > 4)
                    {
                        map = 1;
                    }
                    cnt = 0;
                    Console.Clear();
                    wall = new Wall(map);
                }

                if (score % 4 == 0 && score != 0)
                {
                    if (sp == 0)
                    {
                        speed = speed - 10;
                        sp++;
                    }
                }
                if (score % 4 == 1)
                {
                    sp = 0;
                }

                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);
                }

                while (food.OnSnake(food.location.x, food.location.y, snake) || food.OnWall(food.location.x, food.location.y, wall))
                {
                    food.SetRandomPosition();
                }

                if (snake.Eat(food))
                {
                    cnt++;
                    score++;
                }

                if (snake.Collision(wall))
                {
                    Console.Clear();
                    Console.SetCursorPosition(30, 10);
                    Console.WriteLine("GAME OVER");
                    Console.SetCursorPosition(30, 11);
                    Console.WriteLine("press SPACE to start over");
                    Console.SetCursorPosition(30, 12);
                    Console.WriteLine("press ESC to exit");
                    Console.SetCursorPosition(30, 13);
                    Console.WriteLine("press them twice, 'cause i have no idea how to fix that");
                    ConsoleKeyInfo k = Console.ReadKey();
                    if (k.Key == ConsoleKey.Escape)
                    {
                        gameover = true;
                        break;
                    }
                    if (k.Key == ConsoleKey.Spacebar)
                    {
                        Console.Clear();
                        snake     = new Snake();
                        direction = 0;
                        cnt       = 0;
                        map       = 1;
                        level     = 1;
                        score     = 0;
                        speed     = 150;
                        sp        = 0;
                        wall      = new Wall(map);
                    }
                }

                //Console.Clear();
                snake.Draw();
                food.Draw();
                wall.Draw();
                Console.SetCursorPosition(10, 25);
                Console.WriteLine("level " + level);
                Console.SetCursorPosition(10, 26);
                Console.WriteLine("score " + score);
                Console.SetCursorPosition(75, 4);
                Console.WriteLine("use DIRECTIONAL KEYS to move");
                Console.SetCursorPosition(75, 6);
                Console.WriteLine("press Q to save");
                Console.SetCursorPosition(75, 8);
                Console.WriteLine("press W to load (works wierdly)");
                Console.SetCursorPosition(75, 10);
                Console.WriteLine("press ESC to exit");

                Thread.Sleep(speed);
            }
        }
Пример #6
0
        static void game(object all1)
        {
           int a = 0, b = 40, c = 18, da = 1, lll = 3;
           int g = snake.Random1(a, b);
           int g1 = snake.Random2(a, c);
            while (true )
            {
                if (direction == 3)
                {
                    snake.Move(0, -1);
         
                }
                if (direction == 1)
                {
                    snake.Move(-1, 0);
                   
                    
                }
                if (direction == 2)
                {
                    snake.Move(1, 0);
                   
                }
                if (direction == 4)
                {
                    snake.Move(0, 1);
                    
                }
            
                if (snake.CollisionWithWall(wall) || snake.Collision())
                {
                    Console.Clear();
                    Console.SetCursorPosition(5, 5);
                    Console.WriteLine("GAME OVER!!!!");
                    Console.ReadKey();
                    snake = new Snake();
                    level = 1;
                    wall = new Wall(level);
                }
                 Console.Clear();
                if (da % lll == 0)
                {
                    lll *= 2;
                    level++;
                    wall = new Wall(level);
                }

                if (snake.CollisionWithFood(g, g1))
                {
                    da++;
                    g = snake.Random1(a, b);
                    g1 = snake.Random2(a, c);
                   
                }
                if (wall.FoodInWall(g, g1) == true && snake.FoodInMe(g, g1) == true )
                {
                    snake.DrawFood(g, g1);

                }
                else
                {




                    g = snake.Random1(a, b);
                    g1 = snake.Random2(a, c);
                    snake.DrawFood(g, g1);
                }
                
                snake.Draw(direction);
                wall.Draw();
                    
                Thread.Sleep(100);
            }
        }