Esempio n. 1
0
        /* void CheckFood()
         * {
         *   if (w.CheckCollision(f.body))
         *   {
         *       w.Eat(f.body[0]);
         *       score++;
         *       do
         *       {
         *           f.Generate();
         *       }
         *       while (!CheckFoodSnake());
         *       Console.SetCursorPosition(5, 32);
         *       Console.Write("Score:" + score);
         *   }
         *  else  if (w.CheckCollisionwithItself())
         *   {
         *       gameOver = true;
         *       Console.Clear();
         *       Console.SetCursorPosition(10, 20);
         *
         *       Console.WriteLine("Game over!");
         *   }
         * }
         *
         * bool CheckFoodSnake()
         * {
         *   for (int i = 0; i < w.body.Count; i++)
         *   {
         *       if (w.body[i].X == f.body[0].X && w.body[i].Y == f.body[0].Y)
         *       {
         *           return false;
         *       }
         *   }
         *   return true;
         * }
         * void CheckWall()
         * {
         *
         *   if (w.CheckCollision(b.body))
         *   {
         *       gameOver = true;
         *       Console.Clear();
         *       Console.SetCursorPosition(5, 33);
         *
         *       Console.WriteLine("Game over!");
         *   }
         *
         *   if (score > MaxScore)
         *   {
         *      // b.LoadLevel(3);
         *   }
         * }
         */

        public void PressedKey(ConsoleKeyInfo consoleKeyInfo)
        {
            switch (consoleKeyInfo.Key)
            {
            case ConsoleKey.UpArrow:
                w.Move(0, -1);
                break;

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

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

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

            case ConsoleKey.F2:
                w.Save();
                f.Save();
                break;

            case ConsoleKey.F3:
                w = w.Load() as Worm;
                f = f.Load() as Food;
                break;
            }
            CheckFood();
        }
Esempio n. 2
0
        public void Move()
        {
            while (!GameOver)
            {
                switch (dir)
                {
                case Dir.UP:
                    worm.Move(0, -1);
                    break;

                case Dir.DOWN:
                    worm.Move(0, 1);
                    break;

                case Dir.RIGHT:
                    worm.Move(1, 0);
                    break;

                case Dir.LEFT:
                    worm.Move(-1, 0);
                    break;
                }
                Thread.Sleep(time);
            }
        }
Esempio n. 3
0
 private void Timer_Elapsed(object sender, ElapsedEventArgs e)
 {
     worm.Clear();
     worm.Move();
     worm.Draw();
     CheckColision();
     ShowStatusBar(worm.body.Count.ToString());
 }
Esempio n. 4
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();
        }
Esempio n. 5
0
 private void Timer_Elapsed(object sender, ElapsedEventArgs e)
 {
     worm.Clear();
     worm.Move();
     worm.Draw();
     CheckCollision();
 }
Esempio n. 6
0
        public void Draw()
        {
            while (true)
            {
                worm.Move();

                if (worm.body[0].Equals(food.body[0]))
                {
                    worm.body.Add(new Point {
                        X = food.body[0].X, Y = food.body[0].Y
                    });
                }
                else
                {
                    foreach (Point p in wall.body)
                    {
                        if (p.Equals(worm.body[0]))
                        {
                            Console.Clear();
                            Console.WriteLine("GAME OVER!!!");
                            isAlive = false;
                            break;
                        }
                    }
                }

                Console.Clear();
                foreach (GameObject g in g_objects)
                {
                    g.Draw();
                }
                Thread.Sleep(Game.speed);
            }
        }
Esempio n. 7
0
 public void elapsed(object sender, ElapsedEventArgs e)
 {
     if (Butter.gameOver)
     {
         gameover();
     }
     else
     {
         if (Butter.score == 2)
         {
             timer.Enabled = !timer.Enabled;
             Nextlvl.Serialize(2, Butter.username, 20, 20, 1, 3);
             System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.FriendlyName);
             Environment.Exit(0);
         }
         else
         if (Butter.score == 5)
         {
             timer.Enabled = !timer.Enabled;
             Nextlvl.Serialize(3, Butter.username, 20, 20, 1, 6);
             System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.FriendlyName);
             Environment.Exit(0);
         }
         worm.Clear();
         worm.Move();
         worm.Draw();
         CheckCollision();
     }
 }
Esempio n. 8
0
 private void Timer_Elapsed(object sender, ElapsedEventArgs e)
 {
     w.Clear();
     w.Move();
     w.Draw();
     CheckFood();
 }
Esempio n. 9
0
 private void T_Elapsed(object sender, ElapsedEventArgs e)
 {
     worm.Clear();
     worm.Move();
     CanWormEat();
     IsItCrash();
     worm.Draw();
 }
Esempio n. 10
0
        public void Process(ConsoleKeyInfo pressedButton)
        {
            switch (pressedButton.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:
                break;
            }

            if (worm.body[0].Equals(food.body[0]))
            {
                worm.body.Add(new Point {
                    X = food.body[0].X, Y = food.body[0].Y
                });
            }
            else
            {
                foreach (Point p in wall.body)
                {
                    if (p.Equals(worm.body[0]))
                    {
                        Console.Clear();
                        Console.WriteLine("GAME OVER!!!");
                        isAlive = false;
                        break;
                    }
                }
            }
        }
Esempio n. 11
0
        public void ProcessKeyEvent(ConsoleKeyInfo consoleKeyInfo)
        {
            switch (consoleKeyInfo.Key)
            {
            case ConsoleKey.UpArrow:
                if (worm.dir != Worm.Direction.Down)
                {
                    worm.Move(0, -1);
                    worm.dir = Worm.Direction.Up;
                }
                break;

            case ConsoleKey.DownArrow:
                if (worm.dir != Worm.Direction.Up)
                {
                    worm.Move(0, 1);
                    worm.dir = Worm.Direction.Down;
                }
                break;

            case ConsoleKey.RightArrow:
                if (worm.dir != Worm.Direction.Left)
                {
                    worm.Move(1, 0);
                    worm.dir = Worm.Direction.Right;
                }
                break;

            case ConsoleKey.LeftArrow:
                if (worm.dir != Worm.Direction.Right)
                {
                    worm.Move(-1, 0);
                    worm.dir = Worm.Direction.Left;
                }
                break;

            case ConsoleKey.Spacebar:
                Serialize(player);
                break;
            }

            CheckCollision();
        }
Esempio n. 12
0
 private void Timer_Elapsed(object sender, ElapsedEventArgs e)
 {
     CheckGameOver();
     CheckFood();
     Console.ForegroundColor = ConsoleColor.Green;
     f.Draw();
     w.Move();
     Console.ForegroundColor = ConsoleColor.Yellow;
     w.Draw();
 }
Esempio n. 13
0
        public void ChangeFrame()
        {
            worm.Clear();
            worm.Move();
            worm.Draw();

            PrintInfo(worm.body[0].ToString() + "===" + food.body[0].ToString());

            CheckCollision();
        }
Esempio n. 14
0
        static void Main(string[] args)
        {
            Worm worm = new Worm();
            Food food = new Food();
            Wall wall = new Wall(1);

            while (worm.isAlive)
            {
                Console.Clear();
                worm.Draw();
                food.Draw();
                wall.Draw();
                ConsoleKeyInfo pressedKey = Console.ReadKey();
                switch (pressedKey.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:
                    worm.isAlive = false;
                    break;
                }

                if (worm.CanEat(food))
                {
                    food = new Food();
                }
            }
        }
Esempio n. 15
0
        public void PressedKey(ConsoleKeyInfo consoleKeyInfo)
        {
            switch (consoleKeyInfo.Key)
            {
            case ConsoleKey.UpArrow:

                w.Move(0, -1);

                break;

            case ConsoleKey.DownArrow:

                w.Move(0, 1);

                break;

            case ConsoleKey.LeftArrow:

                w.Move(-1, 0);

                break;

            case ConsoleKey.RightArrow:

                w.Move(1, 0);

                break;

            case ConsoleKey.Spacebar:
                string number = score.ToString();
                string all    = name + " " + number + " " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToShortDateString();
                Console.SetCursorPosition(0, 23);
                Console.CursorVisible = false;
                Console.SetCursorPosition(33, 23);
                Console.WriteLine("GAME OVER");
                File.WriteAllText(file, all);
                Environment.Exit(0);
                break;
            }

            CheckFood();
        }
Esempio n. 16
0
 private void Timer_Elapsed(object sender, ElapsedEventArgs e)
 {
     w.Clear();
     w.Move();
     w.Draw();
     CheckWall();
     CheckBody();
     CheckFood();
     timer.Interval = speed;
     comment();
 }
Esempio n. 17
0
        public void ProcessKey(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;
            }
            CheckCollision();
        }
Esempio n. 18
0
        public void MoveSnake()
        {
            while (anal)
            {
                worm.Clear();

                worm.Move();
                worm.Draw();
                ShowStatusBar(worm.body.Count.ToString());
                //worm.Draw();

                //CheckCollision();
                if (worm.IsIntersected(wall.body))
                {
                    Console.Clear();
                    Console.SetCursorPosition(15, 20);
                    Console.Write("Game over!");
                    anal = false;
                }
                if (worm.IsIntersected(food.body))
                {
                    anal = true;
                    worm.Eat(food.body);
                    food.GenerateLocation(worm.body, wall.body);
                    food.Draw();
                }
                else if (worm.snakegavno(worm.body))
                {
                    Console.Clear();
                    Console.SetCursorPosition(15, 20);
                    Console.Write("Game over!");
                    anal = false;
                }
                if ((Convert.ToInt32(worm.body.Count.ToString()) >= 1 && (Convert.ToInt32(worm.body.Count.ToString()) <= 3)))
                {
                    Thread.Sleep(150);
                }
                else if ((Convert.ToInt32(worm.body.Count.ToString()) >= 4 && (Convert.ToInt32(worm.body.Count.ToString()) <= 7)))
                {
                    Thread.Sleep(100);
                }
                else if ((Convert.ToInt32(worm.body.Count.ToString()) >= 8 && (Convert.ToInt32(worm.body.Count.ToString()) <= 10)))
                {
                    Thread.Sleep(50);
                }
                else if ((Convert.ToInt32(worm.body.Count.ToString()) >= 11))
                {
                    Thread.Sleep(10);
                }
            }
        }
Esempio n. 19
0
        public void PressedKey(ConsoleKeyInfo consoleKeyInfo)
        {
            switch (consoleKeyInfo.Key)
            {
            case ConsoleKey.UpArrow:
                w.Move(0, -1);
                break;

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

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

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

            CheckFood();
        }
Esempio n. 20
0
 void Moving(object sender, ElapsedEventArgs e)
 {
     w.Move();
     if (CheckCollisionFoodWithWorm())
     {
         w.Increase(w.body[0]);
         f.Generate();
         cnt++;
     }
     if (CheckCollisionWallWithWorm())
     {
         wormTimer.Stop();
         IsRunning = false;
     }
 }
Esempio n. 21
0
        public void Draw()
        {
            food.Draw();


            wall.Draw();

            while (true)
            {
                worm.Move();


                if (worm.body[0].Equals(food.body[0]))
                {
                    worm.body.Add(new Point {
                        X = food.body[0].X, Y = food.body[0].Y
                    });
                    food.body[0] = food.CreateFood(wall.body, worm.body);
                }
                else
                {
                    foreach (Point p in wall.body)
                    {
                        if (p.Equals(worm.body[0]))
                        {
                            Console.Clear();
                            Console.SetCursorPosition(10, Game.boardH / 2);
                            Console.WriteLine("GAME OVER!!!");
                            isAlive = false;
                            Console.ForegroundColor = ConsoleColor.Black;

                            Stop();
                        }
                    }
                }

                worm.Draw();
                food.Draw();
                Thread.Sleep(Game.interval);
            }
        }
Esempio n. 22
0
        /* private void ChangeTime(object sender, ElapsedEventArgs e)
         * {
         *   Console.SetCursorPosition(47, 27);
         *   Console.WriteLine(DateTime.Now.Second);
         * }*/

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            w.Clear();
            w.Move();
            if (!end)
            {
                w.Draw();
            }
            CheckFood();
            if (Bally.Score >= 20)
            {
                b.body.Clear();
                Console.Clear();
                b.LoadLevel(2);
                Bally.PrevS += Bally.Score;
                Bally.Score  = 0;
            }
            CheckWall();
            if (w.body.Count >= 4)
            {
                CheckBody();
            }
        }
Esempio n. 23
0
        public void Process()
        {
            ConsoleKeyInfo pressedButton = Console.ReadKey();

            switch (pressedButton.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:
                break;
            }

            if (worm.body[0].Equals(food.body[0]))
            {
                worm.body.Add(new Point {
                    X = food.body[0].X, Y = food.body[0].Y
                });
                count++;
                if (count == 3)
                {
                    wall.Clear();
                    worm.Clear();
                    worm.body.Clear();
                    wall.LoadLevel(gameLevel);
                    wall.Draw();
                    gameLevel++;
                    count = 0;
                }
                Random rx = new Random();
                while (wall.IsPointBelong(food.body[0]) || worm.IsPointBelong(food.body[0]))
                {
                    food.body[0] = new Point {
                        X = rx.Next(1, 30), Y = rx.Next(1, 30)
                    };
                }
            }
            else
            {
                foreach (Point p in wall.body)
                {
                    if (p.Equals(worm.body[0]))
                    {
                        Console.Clear();
                        Console.WriteLine("GAME OVER!!!");
                        isAlive = false;
                        break;
                    }
                }
                for (int i = 1; i < worm.body.Count; i++)
                {
                    if (worm.body[i].Equals(worm.body[0]))
                    {
                        Console.Clear();
                        Console.WriteLine("GAME OVER!!!");
                        isAlive = false;
                        break;
                    }
                }
            }
        }
Esempio n. 24
0
        public void Draw()
        {
            food.Draw();
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.SetCursorPosition(15, 24);

            Console.WriteLine("SCORE:{0}", score);
            wall.Draw();

            while (true)
            {
                worm.Move();
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.SetCursorPosition(15, 24);
                Console.WriteLine("              ");
                Console.SetCursorPosition(15, 24);
                Console.WriteLine("SCORE:{0}", score);

                if (worm.body[0].Equals(food.body[0]))
                {
                    worm.body.Add(new Point {
                        X = food.body[0].X, Y = food.body[0].Y
                    });
                    food.body[0] = food.CreateFood(wall.body, worm.body);

                    score++;
                }
                else
                {
                    foreach (Point p in wall.body)
                    {
                        if (p.Equals(worm.body[0]))
                        {
                            Console.Clear();
                            Console.SetCursorPosition(10, Game.boardH / 2);
                            Console.WriteLine("GAME OVER!!!");
                            isAlive = false;
                            Console.ForegroundColor = ConsoleColor.Black;

                            Stop();
                        }
                    }
                }
                if (score == 3)
                {
                    wall.CleanBody();


                    interval  = 200;
                    gameLevel = GameLevel.Second;
                    wall.LoadLevel(GameLevel.Second);

                    Console.Clear();
                    wall.Draw();

                    food.body[0] = food.CreateFood(wall.body, worm.body);
                    worm.body.Clear();
                    worm.body.Add(new Point {
                        X = 24, Y = 16
                    });
                    score++;
                    continue;
                }
                worm.Draw();
                food.Draw();
                Thread.Sleep(Game.interval);
            }
        }
Esempio n. 25
0
        static void Keys(Wall wall, Worm worm, Food food)
        {
            System system = new System(wall, worm);


            while (worm.isAlive)
            {
                Console.Clear();
                worm.Draw();


                food.Draw(); Console.ForegroundColor = ConsoleColor.Yellow;
                wall.Draw();
                Console.WriteLine(" Your points:");
                Console.WriteLine(Global.points);
                Console.WriteLine(" Your level:");
                Console.WriteLine(Global.level);
                ConsoleKeyInfo pressedKey = Console.ReadKey();

                switch (pressedKey.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:
                    Serialize(system);
                    worm.isAlive = false;
                    break;

                case ConsoleKey.Backspace:
                    Console.Clear();
                    Console.WriteLine(Global.points);
                    break;

                case ConsoleKey.Spacebar:
                    Serialize(system);

                    break;

                case ConsoleKey.F2:
                    Console.Clear();
                    System system2 = Deserialize();
                    food.WhereisFood(system2.wall, system2.worm);
                    Keys(system.wall, system.worm, food);
                    break;
                }

                if (worm.IsDead(wall))
                {
                    worm.isAlive = false;
                }
                if (worm.body.Count > 3)
                {
                    Global.level++;
                    Global.points = +50;
                    Worm newworm = new Worm();
                    Console.Clear();
                    Wall newwall = new Wall(Global.level);
                    newwall.Draw();
                    Food newfood = new Food();
                    newfood.WhereisFood(newwall, newworm);

                    worm.isAlive = true;
                    Keys(newwall, newworm, newfood);
                }
                if (worm.CanEat(food))
                {
                    food = new Food();
                    food.WhereisFood(wall, worm);
                    Global.points += 10;
                }

                Serialize(system);


                if (worm.IsDead(wall))
                {
                    Console.Clear();
                    Console.WriteLine();
                    Console.WriteLine("GAME OVER");
                    Console.WriteLine("Your points:");
                    Console.WriteLine(Global.points);
                    Console.WriteLine("max level:");
                    Console.WriteLine(Global.level);
                }
            }
        }
Esempio n. 26
0
        public void DrawWorm(object sender, ElapsedEventArgs e)
        {
            if (points == 30)
            {
                points = 40;
                LoadLevel2();
            }
            if (points == 70)
            {
                points = 80;
                LoadLevel3();
            }
            if (points == 110)
            {
                points = 120;
                LoadLevelInf();
            }

            DrawBar();
            worm.Clear();
            worm.Move();
            worm.Draw();

            if (worm.body[0].Equals(food.body[0]))
            {
                worm.body.Add(new Point {
                    X = food.body[0].X, Y = food.body[0].Y
                });
                points = points + 10;
                bool goodFood = false;
                while (!goodFood)
                {
                    food = new Food(new Point {
                        X = new Random().Next() % 57 + 1, Y = new Random().Next() % 15 + 6
                    }, ConsoleColor.Red, '+');
                    goodFood = true;
                    foreach (Point p in worm.body)
                    {
                        if (food.body[0].Equals(p))
                        {
                            goodFood = false;
                            break;
                        }
                    }
                    foreach (Point p in wall.body)
                    {
                        if (food.body[0].Equals(p))
                        {
                            goodFood = false;
                            break;
                        }
                    }
                }
                food.Draw();
            }

            else
            {
                foreach (Point p in wall.body)
                {
                    if (p.Equals(worm.body[0]))
                    {
                        Stop();
                        alive = false;
                        DrawGameOver();
                    }
                }

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

            if (points >= 110)
            {
                lev = "inf";
            }
            else if (points >= 70)
            {
                lev = "3";
            }
            else if (points >= 30)
            {
                lev = "2";
            }
        }
Esempio n. 27
0
        static void Main(string[] args)
        {
            int l;

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

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

            string way = "none";

            bool f0 = true;

            while (f0 == true)
            {
                worm = new Worm();
                food = new Food();
                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();
                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();

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

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

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

                if (worm.CanEat(food))
                {
                    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");
        }
Esempio n. 28
0
        public void PressedKey(ConsoleKeyInfo consoleKeyInfo)
        {
            switch (consoleKeyInfo.Key)
            {
            case ConsoleKey.UpArrow:
                if (w.body.Count > 1)
                {
                    if (movement != "down")
                    {
                        w.Move(0, -1);
                        movement = "up";
                    }
                }
                else
                {
                    w.Move(0, -1);
                    movement = "up";
                }
                break;

            case ConsoleKey.DownArrow:
                if (w.body.Count > 1)
                {
                    if (movement != "up")
                    {
                        w.Move(0, 1);
                        movement = "down";
                    }
                }
                else
                {
                    w.Move(0, 1);
                    movement = "down";
                }
                break;

            case ConsoleKey.LeftArrow:
                if (w.body.Count > 1)
                {
                    if (movement != "right")
                    {
                        w.Move(-1, 0);
                        movement = "left";
                    }
                }
                else
                {
                    w.Move(-1, 0);
                    movement = "left";
                }
                break;

            case ConsoleKey.RightArrow:
                if (w.body.Count > 1)
                {
                    if (movement != "left")
                    {
                        w.Move(1, 0);
                        movement = "right";
                    }
                }
                else
                {
                    w.Move(1, 0);
                    movement = "right";
                }
                break;
            }

            CheckFood();
            if (Bally.Score >= 20)
            {
                b.body.Clear();
                Console.Clear();
                b.LoadLevel(2);
                Bally.PrevS += Bally.Score;
                Bally.Score  = 0;
            }
            CheckWall();
            if (w.body.Count >= 4)
            {
                CheckBody();
            }
        }
Esempio n. 29
0
        static void Main(string[] args)
        {
            Worm worm = new Worm();
            Food food = new Food();
            Wall wall = new Wall(1);

            worm.Start();
            while (worm.isAlive)
            {
                Console.Clear();
                worm.Draw();
                food.Draw();

                int currx = worm.Getheadx();
                int curry = worm.Getheady();
                int k     = wall.Draw(currx, curry);
                if (k == 1)
                {
                    break;
                }
                ConsoleKeyInfo pressedKey = Console.ReadKey();
                switch (pressedKey.Key)
                {
                case ConsoleKey.F8:
                    Game q = new Game(worm, wall, food);
                    Serialize(q);
                    break;

                case ConsoleKey.F9:

                    Game h = Deserialize();
                    worm = h.worm;
                    food = h.food;
                    wall = h.wall;

                    break;

                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:
                    worm.isAlive = false;
                    break;
                }

                if (worm.CanEat(food))
                {
                    food = new Food();
                }
            }
            Console.Clear();
            Console.WriteLine("YOU LOSE");
        }
Esempio n. 30
0
        static void Main(string[] args)
        {
            FileStream fs = new FileStream("save.xml", FileMode.Create);
            fs.Close();
        VeryBegin:
            Point p;
            int l = 1;
            Console.CursorVisible = false;
        /*
         while (true)
         {
             Console.Clear();
             Console.WriteLine("Chose level (1-5)");
             l = int.Parse(Console.ReadLine());
             if (l > 0 && l <= 5)
                 break;
         } 
        */
        Begin:
            dx = 0;
            dy = 0;
            Console.Clear();
            int score = 0;

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

            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)
            {                                
                p = new Point(worm.body[worm.body.Count - 1].x, worm.body[worm.body.Count - 1].y);
                worm.Draw();                               
                food.Draw();                                                                                
                wall.Draw();
                Console.WriteLine("\n\nScore: {0}", score);
                if (score >= 10)
                {
                    Console.WriteLine("\n\n Next level availible");
                }

                
                Thread.Sleep(200);
                worm.Move(dx, dy);
                Thread t = new Thread(new ThreadStart(Keys));
                t.Start();

                /*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;
                        Console.Clear();
                        break;
                    case ConsoleKey.OemPlus:
                        if(score >=10)
                        {
                            l++;
                            goto Begin;
                        }
                        break;
                    case ConsoleKey.OemMinus:
                        if (l > 1)
                        {
                            l--;
                            goto Begin;
                        }
                        break;
                }*/

                Console.SetCursorPosition(p.x, p.y);
                Console.Write(' ');

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