Ejemplo n.º 1
0
 public GameState()
 {
     Console.CursorVisible = false;
     Console.SetWindowSize(40, 40);
     Console.SetBufferSize(40, 40);
     food.Generate(wall.body, worm.body);
 }
Ejemplo n.º 2
0
        public Game()
        {
            g_objects = new List <GameObject>();

            isAlive = true;

            snake = new Snake(5, 3, "o", ConsoleColor.Blue);

            food = new Food(0, 0, "O", ConsoleColor.Green);
            food.Generate();

            wall = new Wall("+", ConsoleColor.Gray);
            wall.Walls();

            while (food.IsCollisionWithObject(snake) || food.IsCollisionWithObject(wall))
            {
                food.Generate();
            }

            g_objects.Add(snake);
            g_objects.Add(food);
            g_objects.Add(wall);

            Console.CursorVisible = false;

            Console.SetWindowSize(40, 20);
        }
Ejemplo n.º 3
0
 void CheckFood()
 {
     if (w.CheckCollision(f.body[0]))
     {
         eating = true;
         w.Eat(f.body[0]);
         Bally.Score += 10;
         f.Generate();
         foreach (Point p in b.body)
         {
             if (f.body[0].X == p.X && f.body[0].Y == p.Y)
             {
                 f.Generate();
                 f.Draw();
             }
         }
         foreach (Point p in w.body)
         {
             if (p.X == f.body[0].X && p.Y == f.body[0].Y && !eating)
             {
                 f.Generate();
                 f.Draw();
             }
         }
         eating = false;
     }
 }
Ejemplo n.º 4
0
 private void GameTimer_Elapsed(object sender, ElapsedEventArgs e)
 {
     Console.Title = DateTime.Now.ToLongTimeString();
     if (CheckCollisionFoodWithWorm())
     {
         w.Increase(w.body[0]);
         f.Generate();
         cnt++;
     }
 }
Ejemplo n.º 5
0
        public void PressedKey(ConsoleKeyInfo consoleKeyInfo)
        {
            switch (consoleKeyInfo.Key)
            {
            case ConsoleKey.UpArrow:
                worm.Move(0, -1);
                break;

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

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

            case ConsoleKey.RightArrow:
                worm.Move(1, 0);
                break;

            case ConsoleKey.Escape:
                Console.Clear();
                Console.SetCursorPosition(20, 30);
                Console.WriteLine("Exit?");
                Environment.Exit(0);
                break;

            case ConsoleKey.F2:
                Console.Clear();
                worm.body.Clear();
                worm.body.Add(new Point {
                    X = 20, Y = 20
                });
                wall.LoadLevel(2);
                food.Generate(wall.body, worm.body);
                level = 2;
                break;

            case ConsoleKey.F3:
                Console.Clear();
                worm.body.Clear();
                worm.body.Add(new Point {
                    X = 20, Y = 20
                });
                wall.LoadLevel(3);
                food.Generate(wall.body, worm.body);
                level = 3;
                break;
            }

            CheckColision();
            CheckWallColision();
            CheckSelfCollision();
        }
Ejemplo n.º 6
0
 private void Update(object sender, EventArgs e)
 {
     this.Text = string.Format("Snake- Score: {0}", score);
     snake.Move(direction);
     for (int i = 1; i < snake.Body.Length; i++)
     {
         if (snake.Body[0].IntersectsWith(snake.Body[i]))
         {
             Restart();
         }
     }
     if (snake.Body[0].X < 0 || snake.Body[0].X > 290)
     {
         Restart();
     }
     if (snake.Body[0].Y < 0 || snake.Body[0].Y > 190)
     {
         Restart();
     }
     if (snake.Body[0].IntersectsWith(food.Piece))
     {
         score++;
         snake.Grow();
         food.Generate(rand);
     }
     Invalidate();
 }
Ejemplo n.º 7
0
        public void Start()
        {
            ConsoleKeyInfo cnskey = Console.ReadKey();

            while (IsAlive && cnskey.Key != ConsoleKey.Escape)
            {
                Draw();
                cnskey = Console.ReadKey();
                if (snake.IsColl(food))
                {
                    snake.body.Add(new Point(0, 0));
                    points += 50;
                    while (food.IsColl(snake) || food.IsColl(wall))
                    {
                        food.Generate();
                    }
                }
                if (snake.body.Count % 3 == 0)
                {
                    wall.NextLevel();
                }
                if (snake.IsColl(wall))
                {
                    IsAlive = false;
                }
                Console.WriteLine(snake.body[0].x);
                snake.Move(cnskey);
            }
            //DEAD or END
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(20, 10);
            Console.WriteLine("GAME OVER!!! Your score is: {0} ", points);
            Console.ReadKey();
        }
Ejemplo n.º 8
0
        public void SnakeMove()
        {
            while (isAlive)
            {
                snake.Move();
                if (snake.IsCollision(food))
                {
                    score += 10;
                    snake.body.Add(new Point(0, 0));

                    food.Generate(snake, wall);


                    if (snake.body.Count % 3 == 0)
                    {
                        wall.NextLevel();
                        levels -= 50;
                    }
                }
                if (snake.IsCollision(wall))
                {
                    isAlive = false;
                }
                if (snake.IsCollision(snake))
                {
                    isAlive = false;
                }

                Draw();
                Thread.Sleep(levels);
            }
        }
Ejemplo n.º 9
0
        void CheckFood()
        {
            if (w.CheckCollision(b.body))
            {
                gameOver = true;
                Console.Clear();
                Console.SetCursorPosition(10, 20);
                Console.WriteLine("Game over!");
            }
            else if (w.CheckCollision(f.body))
            {
                w.Eat(f.body[0]);
                score++;
                f.Generate();
            }
            else if (w.CheckCollisionwithItself())
            {
                gameOver = true;
                Console.Clear();
                Console.SetCursorPosition(10, 20);
                Console.WriteLine("Game over!");
            }

            if (score > MaxScore)
            {
                b.Clear();
                b.body.Clear();
                level = 3;
                b.LoadLevel(3);
            }
        }
Ejemplo n.º 10
0
        public void MoveSnake()
        {
            while (isAlive)
            {
                snake.Move();
                if (snake.IsCollisionWithObject(food))
                {
                    snake.body.Add(new Point(0, 0));
                    while (food.IsCollisionWithObject(snake) || food.IsCollisionWithObject(wall))
                    {
                        food.Generate();
                    }

                    if (snake.body.Count % 3 == 0)
                    {
                        wall.NextLevel();
                    }
                }
                if (snake.IsCollisionWithObject(wall))
                {
                    isAlive = false;
                }
                Draw();
                Thread.Sleep(100);
            }
        }
Ejemplo n.º 11
0
 private void Update(object sender, EventArgs e)
 {
     if (score > bestScore)
     {
         bestScore = score;
     }
     this.Text   = string.Format("Snake - Score {0} ", score);
     label7.Text = string.Format("{0}", playerName);
     label8.Text = string.Format("{0}", score);
     label9.Text = string.Format("{0}", bestScore);
     snake.Move(direction);
     for (int i = 1; i < snake.Body.Length; i++)
     {
         if (snake.Body[0].IntersectsWith(snake.Body[i]))
         {
             Restart();
         }
     }
     if (snake.Body[0].X < 0 || snake.Body[0].X > 430)
     {
         Restart();
     }
     if (snake.Body[0].Y < 0 || snake.Body[0].Y > 430)
     {
         Restart();
     }
     if (snake.Body[0].IntersectsWith(food.Piece))
     {
         score++;
         snake.Grow();
         food.Generate(rand);
     }
     this.Invalidate();
 }
Ejemplo n.º 12
0
        private void Update(object sender, EventArgs e)
        {
            snake.Move(direction);
            for (int i = 1; i < snake.body.Length; i++)
            {
                if (snake.body[0].IntersectsWith(snake.body[i]))
                {
                    Restart();
                }
            }

            if (snake.body[0].X < 0 || snake.body[0].X > 990)
            {
                Restart();
            }

            if (snake.body[0].Y < 0 || snake.body[0].Y > 690)
            {
                Restart();
            }

            if (snake.body[0].IntersectsWith(food.food))
            {
                score++;
                snake.Grow();
                food.Generate(rand);
            }

            this.Invalidate();
        }
Ejemplo n.º 13
0
 private void CheckFoodCatch()
 {
     if (serpent.HasCollided(food.GetFood()))
     {
         serpent.Eat(food.GetFood());
         food.Generate();
     }
 }
Ejemplo n.º 14
0
 public void CheckCollision()
 {
     if (worm.CheckIntersection(food.body[0]))
     {
         worm.Eat(food.body[0]);
         food.Generate();
     }
 }
Ejemplo n.º 15
0
 void CheckColision()
 {
     if (worm.CheckIntersection(food.body) == true)
     {
         worm.Eat(food.body);
         food.Generate();
     }
 }
Ejemplo n.º 16
0
 void CheckFood()
 {
     if (w.CheckCollision(f.body[0]))
     {
         w.Eat(f.body[0]);
         f.Generate();
     }
 }
Ejemplo n.º 17
0
 private void GenerateFood()
 {
     if (!IsFoodThere)
     {
         food.Generate();
         IsFoodThere = true;
     }
 }
Ejemplo n.º 18
0
        private void CheckFoodCatch()
        {
            //if (FoodAndWallOrSerpent())
            //{

            if (serpent.HasCollided(food.GetFood()))
            {
                serpent.Eat(food.GetFood());
                score++;
                Score();
                food.Generate();
            }
            //}
            //else
            //{
            //    food.Generate();
            //    CheckFoodCatch();
            //}
        }
Ejemplo n.º 19
0
 public void CheckCollision()
 {
     if (worm.CheckIntersection(food.body))
     {
         worm.Eat(food.body);
         food.Generate();
     }
     if (worm.CheckIntersection(wall.body))
     {
         //GameOver = true;
     }
 }
Ejemplo n.º 20
0
        public void Load()
        {
            worm = new Worm();
            worm.AttachGameLink(this);

            wall = new Wall();
            food = new Food();
            food.Generate(wall, worm);
            worm.Generate(wall);
            wall.Generate(level);
            worm.Draw();
            wall.Draw();
            food.Draw();
        }
Ejemplo n.º 21
0
        public void CanEat()
        {
            if (worm.points[0].Equals(food.points[0]))
            {
                worm.points.Add(food.points[0]);
                food = new Food();
                food.Generate(wall, worm);
                food.Draw();

                score = score + 100;
                if (score == 500)
                {
                    nextLevel = true;
                }
            }
        }
Ejemplo n.º 22
0
        public Game()
        {
            isAlive = true;
            snake   = new Snake(10, 10, '*', ConsoleColor.White);
            wall    = new Wall('#', ConsoleColor.Yellow);
            wall.LoadLevel();
            food = new Food(0, 0, 'o', ConsoleColor.Cyan);
            food.Generate(snake, wall);


            g_objects = new List <GameObject>();
            g_objects.Add(snake);
            g_objects.Add(food);
            g_objects.Add(wall);

            Console.CursorVisible = false;
        }
Ejemplo n.º 23
0
 void CheckFood()
 {
     if (w.CheckCollision(f.body[0]))
     {
         score++;
         if (score == 2)
         {
             degree++;
         }
         if (score == 5)
         {
             degree++;
         }
         w.Eat(f.body[0]);
         f.Generate();
     }
 }
Ejemplo n.º 24
0
        public Game()
        {
            g_objects = new List <GameObject>();
            snake     = new Snake(20, 10, '*', ConsoleColor.White);
            food      = new Food(0, 0, 'o', ConsoleColor.Cyan);
            wall      = new Wall('#', ConsoleColor.Green);
            wall.LoadLevel();
            while (food.IsCollisionWithObject(snake) || food.IsCollisionWithObject(wall))
            {
                food.Generate();
            }

            g_objects.Add(snake);
            g_objects.Add(food);
            g_objects.Add(wall);
            isAlive = true;
        }
Ejemplo n.º 25
0
        public Game()
        {
            g_objects = new List <GObject>();

            snake = new Snake(1, 1, '*', ConsoleColor.White);
            wall  = new Wall('#', ConsoleColor.Green);
            wall.LoadLevel();
            food = new Food(0, 0, 'o', ConsoleColor.Red);

            food.Generate(snake, wall);


            g_objects.Add(snake);
            g_objects.Add(food);
            g_objects.Add(wall);

            isAlive = true;
        }
Ejemplo n.º 26
0
        public void Start()
        {
            Console.Clear();
            int k = 0;

            Console.WriteLine("Write your username");
            string         s       = Console.ReadLine();
            ConsoleKeyInfo keyInfo = Console.ReadKey();

            while (isAlive && keyInfo.Key != ConsoleKey.Escape)
            {
                Draw();
                keyInfo = Console.ReadKey();
                if (snake.IsCollisionWithObject(food))
                {
                    snake.body.Add(new Point(0, 0));
                    k++;
                    while (food.IsCollisionWithObject(snake) || food.IsCollisionWithObject(wall))
                    {
                        food.Generate();
                    }

                    if (snake.body.Count % 3 == 0)
                    {
                        wall.NextLevel();
                    }
                }
                if (snake.IsCollisionWithObject(wall) || snake.IsCollisionWithSnake(snake))
                {
                    isAlive = false;
                }
                snake.ChangeDirection(keyInfo);
            }
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(20, 10);
            Console.WriteLine("GAME OVER!!!");
            Console.ReadKey();
            Console.Clear();
            Console.WriteLine(s);
            Console.Write("Your score:" + k * 100);
            Console.ReadKey();
        }
Ejemplo n.º 27
0
 void CheckFood()
 {
     if (w.CheckCollision(f.body[0]))
     {
         point += 100 + level * 10;
         w.Eat(f.body[0]);
         if (point >= levels[level])
         {
             level++;
             speed -= 50;
             b.LoadLevel(level);
             Console.BackgroundColor = ConsoleColor.Gray;
             b.Draw();
             Console.BackgroundColor = ConsoleColor.Black;
         }
         f.Generate(new object[] { w, b });
         Console.ForegroundColor = ConsoleColor.Red;
         f.Draw();
         Console.ForegroundColor = ConsoleColor.Green;
     }
 }
Ejemplo n.º 28
0
        public void Generate()
        {
            bool res = false;

            while (!res)
            {
                Random random = new Random(DateTime.Now.Second);
                x = random.Next(1, 79);
                y = random.Next(3, 39);
                for (int i = 0; i < b.body.Count; ++i)
                {
                    if (x == b.body[i].X && y == b.body[i].Y)
                    {
                        res = true;
                        break;
                    }
                }
                res = !res;
            }
            f.Generate(x, y);
        }
Ejemplo n.º 29
0
        public void Start()
        {
            //int k = 0;
            ConsoleKeyInfo keyInfo = Console.ReadKey();

            while (keyInfo.Key != ConsoleKey.Escape && isAlive)
            {
                snake.Move(keyInfo);
                Draw();
                keyInfo = Console.ReadKey();
                //k++;
                //if (k % 10 == 0)
                //{
                //    snake.body.Add(new Point(0, 0));
                //}
                if (snake.IsCollisionWithFood(food))
                {
                    snake.body.Add(new Point(0, 0));
                    food.Generate(snake, wall);
                    if (snake.body.Count % 3 == 0)
                    {
                        wall.NextLevel();
                    }
                }
                if (snake.IsCollistionWithWall(wall))
                {
                    isAlive = false;
                }
                //if (snake.IsCollisionWithSnake())
                //  isAlive = false;
            }
            Console.Clear();
            Console.SetCursorPosition(10, 10);
            Console.Write("GAME OVER");
            Console.ReadKey();
        }
Ejemplo n.º 30
0
 public void MoveSnake()
 {
     while (isAlive)
     {
         snake.Move();
         Draw();
         if (snake.IsCollisionWithObject(food))
         {
             score += 10;
             while (food.IsCollisionWithObject(snake) || food.IsCollisionWithObject(wall))
             {
                 food.Generate();
             }
             snake.body.Add(new Point(0, 0));
         }
         if (snake.IsCollisionWithObject(wall))
         {
             Console.Write("Press any button.");
             isAlive = false;
         }
         if (snake.IsCollisionWithItself())
         {
             isAlive = false;
         }
         //Draw();
         if (snake.body.Count == 3)
         {
             speed = 50;
         }
         else if (snake.body.Count == 6)
         {
             speed = 30;
         }
         Thread.Sleep(speed);
     }
 }