예제 #1
0
 public Game()
 {
     alive = true;
     worm  = new Worm(new Point {
         X = 32, Y = 13
     }, ConsoleColor.DarkGreen, 'o');
     food = new Food(new Point {
         X = 20, Y = 14
     }, ConsoleColor.Red, '+');
     wall = new Wall(null, ConsoleColor.DarkMagenta, '#');
 }
예제 #2
0
파일: Worm.cs 프로젝트: YernurTenizbay/ICT
        public static Worm Load(string title)
        {
            Worm res = null;

            using (FileStream fs = new FileStream(title + ".xml", FileMode.Open, FileAccess.Read))
            {
                XmlSerializer xs = new XmlSerializer(typeof(Worm));
                res = xs.Deserialize(fs) as Worm;
            }
            return(res);
        }
예제 #3
0
        static void Main(string[] args)
        {
            Console.SetWindowSize(40, 50);
            Console.SetBufferSize(40, 50);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Continue - 1");
            Console.WriteLine("New game - 2");
            Console.WriteLine("Pause - space");
            string option = Console.ReadLine();
            Food   food1  = new Food();

            Console.Clear();
            if (option == "1")
            {
                System system = Deserialize();
                food1.WhereisFood(system.wall, system.worm);
                Keys(system.wall, system.worm, food1);
                if (!system.worm.isAlive)
                {
                    Console.WriteLine("Start new game?");
                    Console.WriteLine("2-yes");
                    string option1 = Console.ReadLine();
                    if (option1 == "2")
                    {
                        Console.Clear();
                        Console.SetWindowSize(40, 40);
                        Worm worm = new Worm();
                        Console.Clear();
                        Global.level = 1;
                        Wall wall = new Wall(Global.level);
                        wall.Draw();
                        Food food = new Food();
                        food.WhereisFood(wall, worm);

                        ///  System system = new System(wall, worm);
                        Keys(wall, worm, food);
                    }
                }
            }
            else
            {
                Console.SetWindowSize(40, 40);
                Worm worm = new Worm();

                Wall wall = new Wall(Global.level);
                wall.Draw();
                Food food = new Food();
                food.WhereisFood(wall, worm);

                ///  System system = new System(wall, worm);
                Keys(wall, worm, food);
            }
        }
예제 #4
0
파일: Food.cs 프로젝트: zhengissov/PT2017
        public void Generate(Wall wall, Worm worm)
        {
            this.points.Clear();
            Point newFood = new Point(new Random().Next() % 30, new Random().Next() % 30);

            FoodLocation = newFood;
            while (wall.points.Contains(newFood) || worm.points.Contains(newFood))
            {
                newFood      = new Point(new Random().Next() % 30, new Random().Next() % 30);
                FoodLocation = newFood;
            }

            this.points.Add(FoodLocation);
        }
예제 #5
0
파일: Game.cs 프로젝트: zhengissov/PT2017
        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();
        }
예제 #6
0
파일: Game.cs 프로젝트: Alibek120699/PP2
        public Game()
        {
            isAlive   = true;
            gameLevel = GameLevel.First;

            worm = new Worm(new Point {
                X = 10, Y = 10
            },
                            ConsoleColor.Magenta, '0');
            food = new Food(new Point {
                X = 20, Y = 10
            },
                            ConsoleColor.Green, '1');
            wall = new Wall(null, ConsoleColor.Cyan, '#');

            wall.LoadLevel(GameLevel.First);
        }
예제 #7
0
 public void WhereisFood(Wall wall, Worm worm)
 {
     location = new Point(new Random().Next() % 30, new Random().Next() % 30);
     for (int i = 0; i < wall.bricks.Count; i++)
     {
         if (location.Equals(wall.bricks[i]))
         {
             WhereisFood(wall, worm);
         }
     }
     for (int i = 0; i < worm.body.Count; i++)
     {
         if (location.Equals(worm.body[i]))
         {
             WhereisFood(wall, worm);
         }
     }
 }
예제 #8
0
 public void LoadLevelInf()
 {
     Stop();
     Console.Clear();
     worm.body.Clear();
     wall.body.Clear();
     food.body.Clear();
     DrawField();
     wall.LoadLevel("inf");
     worm = new Worm(new Point {
         X = 32, Y = 13
     }, ConsoleColor.DarkGreen, 'o');
     food = new Food(new Point {
         X = 20, Y = 14
     }, ConsoleColor.Red, '+');
     isPaused = false;
     Draw();
     Start();
 }
예제 #9
0
파일: Food.cs 프로젝트: luikaaa27/PP2
        public void Generate(object[] o)
        {
            Random random = new Random(DateTime.Now.Second);

            body.Clear();
            int  x     = random.Next(1, 39);
            int  y     = random.Next(1, 39);
            bool check = false;
            Worm w     = o[0] as Worm;
            Wall b     = o[1] as Wall;

            while (!check)
            {
                check = true;
                foreach (Point i in w.body)
                {
                    if (i.X == x && i.Y == y)
                    {
                        check = false;
                        break;
                    }
                }
                foreach (Point i in b.body)
                {
                    if (i.X == x && i.Y == y)
                    {
                        check = false;
                        break;
                    }
                }
                if (check)
                {
                    break;
                }
                x = random.Next(1, 39);
                y = random.Next(1, 39);
            }
            body.Add(new Point
            {
                X = x,
                Y = y
            });
        }
예제 #10
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();
                }
            }
        }
예제 #11
0
        public Game()
        {
            isAlive = true;

            worm = new Worm(new Point {
                X = 10, Y = 10
            },
                            ConsoleColor.White, '*');
            food = new Food(new Point {
                X = 20, Y = 10
            },
                            ConsoleColor.Magenta, 'o');
            wall = new Wall(null, ConsoleColor.White, '#');

            wall.LoadLevel(1);

            g_objects.Add(worm);
            g_objects.Add(food);
            g_objects.Add(wall);
        }
예제 #12
0
파일: Game.cs 프로젝트: abkabd/PP2_2018S
        public Game()
        {
            isAlive   = true;
            gameLevel = GameLevel.First;

            worm = new Worm(new Point {
                X = 10, Y = 10
            },
                            ConsoleColor.White, '*');
            food = new Food(new Point {
                X = 20, Y = 10
            },
                            ConsoleColor.White, '+');

            food.Draw();
            wall = new Wall(null, ConsoleColor.White, '#');

            wall.LoadLevel(GameLevel.First);
            wall.Draw();
        }
예제 #13
0
        public Game()
        {
            isAlive   = true;
            gameLevel = GameLevel.First;

            worm = new Worm(new Point {
                X = 10, Y = 10
            },
                            ConsoleColor.DarkYellow, '▲');
            food = new Food(new Point {
                X = 20, Y = 10
            },
                            ConsoleColor.White, '☺');
            wall = new Wall(null, ConsoleColor.White, '#');

            wall.LoadLevel(GameLevel.First);

            g_objects.Add(worm);
            g_objects.Add(food);
            g_objects.Add(wall);
        }
예제 #14
0
        public Game()
        {
            gameLevel = GameLevel.First;

            worm = new Worm(new Point {
                X = 10, Y = 10
            },
                            ConsoleColor.White, '*');
            food = new Food(new Point {
                X = 14, Y = 10
            },
                            ConsoleColor.White, '+');
            wall = new Wall(null, ConsoleColor.White, '#');

            wall.LoadLevel(GameLevel.First);


            wormTimer          = new Timer();
            wormTimer.Interval = 500;
            wormTimer.Elapsed += T_Elapsed;
        }
예제 #15
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");
        }
예제 #16
0
 public System(Wall wall, Worm worm)
 {
     this.wall = wall;
     this.worm = worm;
 }
예제 #17
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);
                }
            }
        }
예제 #18
0
파일: Game.cs 프로젝트: zhengissov/PT2017
        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;
                }
            }
        }
예제 #19
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");
        }
예제 #20
0
 void Chewed(Worm worm)
 {
     // TODO: Implement
 }
예제 #21
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");
        }
예제 #22
0
파일: Game.cs 프로젝트: abkabd/PP2_2018S
        public void Process(ConsoleKeyInfo pressedButton)
        {
            switch (pressedButton.Key)
            {
            case ConsoleKey.UpArrow:
                worm.Clear();
                worm.Move(0, -1);
                worm.Draw();
                break;

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

                break;

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

                break;

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

                break;

            case ConsoleKey.Escape:
                break;

            case ConsoleKey.F2:
                worm.Save();
                food.Save();
                break;

            case ConsoleKey.F1:
                worm = worm.Load() as Worm;
                food = food.Load() as Food;
                break;
            }

            if (worm.body[0].Equals(food.body[0]))
            {
                worm.body.Add(new Point {
                    X = food.body[0].X, Y = food.body[0].Y
                });
                food.Draw();
            }
            else
            {
                foreach (Point p in wall.body)
                {
                    if (p.Equals(worm.body[0]))
                    {
                        Console.Clear();
                        Console.WriteLine("GAME OVER!!!");
                        isAlive = false;
                        break;
                    }
                }
            }
        }
예제 #23
0
        public void Process(ConsoleKeyInfo pressedButton)
        {
            switch (pressedButton.Key)
            {
            case ConsoleKey.LeftArrow:
                worm.DY = 0;
                worm.DX = -1;
                break;

            case ConsoleKey.RightArrow:
                worm.DY = 0;
                worm.DX = 1;
                break;

            case ConsoleKey.UpArrow:
                worm.DY = -1;
                worm.DX = 0;
                break;

            case ConsoleKey.DownArrow:
                worm.DY = 1;
                worm.DX = 0;
                break;

            case ConsoleKey.Spacebar:
                if (isPaused)
                {
                    isPaused = false;
                    Start();
                }
                else
                {
                    isPaused = true;
                    Stop();
                }
                break;

            case ConsoleKey.F1:
                if (isPaused)
                {
                    pts = points;
                    l   = lev;
                    worm.Save();
                    food.Save();
                    wall.Save();
                }
                break;

            case ConsoleKey.F2:
                if (isPaused)
                {
                    Console.Clear();
                    DrawField();
                    points = pts;
                    lev    = l;
                    worm   = worm.Load() as Worm;
                    food   = food.Load() as Food;
                    wall   = wall.Load() as Wall;
                    Draw();
                }
                break;

            case ConsoleKey.Escape:
                alive = false;
                Stop();
                DrawGameOver();
                break;

            default:
                break;
            }
        }
예제 #24
0
파일: Game.cs 프로젝트: vmun/snake
 public Game(Worm worm, Wall wall, Food food)
 {
     this.worm = worm;
     this.wall = wall;
     this.food = food;
 }
예제 #25
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");
        }