예제 #1
0
        public Item CreateItem(TypeOfItem type)
        {
            Random random = new Random();
            int    x      = 0;
            int    y      = 0;

            while (GridField[x, y] != null)
            {
                x = random.Next(2, this.Width - 2);
                y = random.Next(2, this.Height - 2);
            }
            switch (type)
            {
            case (TypeOfItem.Tree):
                Tree tree = new Tree(x, y, this);
                GridField[tree.X, tree.Y] = tree;
                return(tree);

            case (TypeOfItem.Rock):
                Rock rock = new Rock(x, y, this);
                GridField[rock.X, rock.Y] = rock;
                return(rock);

            case (TypeOfItem.CherryBon):
                int       speedUp = 1;
                CherryBon cherry  = new CherryBon(speedUp, x, y, this);
                GridField[cherry.X, cherry.Y] = cherry;
                return(cherry);

            case (TypeOfItem.AppleBon):
                int      healthUp = 1;
                AppleBon apple    = new AppleBon(healthUp, x, y, this);
                GridField[apple.X, apple.Y] = apple;
                return(apple);

            case (TypeOfItem.Wolf):
                Wolf wolf = new Wolf(x, y, this);
                GridField[wolf.X, wolf.Y] = wolf;
                return(wolf);

            case (TypeOfItem.Bear):
                Bear bear = new Bear(x, y, this);
                GridField[bear.X, bear.Y] = bear;
                return(bear);

            case (TypeOfItem.LittleRedRidingHood):
                LittleRedRidingHood redHood = new LittleRedRidingHood(2, Height - 2, this);
                GridField[redHood.X, redHood.Y] = redHood;
                return(redHood);

            default: return(null);
            }
        }
예제 #2
0
        public static void StartGame(Field field)
        {
            Random    rand      = new Random();
            const int MAX_SPEED = 3;
            int       direct;
            int       counter = 0;

            LittleRedRidingHood redHood = (LittleRedRidingHood)field.CreateItem(TypeOfItem.LittleRedRidingHood);

            field.MakeBorder();
            field.CreateTreeObstracle();
            CherryBon cherry = (CherryBon)field.CreateItem(TypeOfItem.CherryBon);
            AppleBon  apple  = (AppleBon)field.CreateItem(TypeOfItem.AppleBon);
            Wolf      wolf   = (Wolf)field.CreateItem(TypeOfItem.Wolf);
            Bear      bear   = (Bear)field.CreateItem(TypeOfItem.Bear);

            while (true)
            {
                if (cherry.IsEated())
                {
                    cherry = (CherryBon)field.CreateItem(TypeOfItem.CherryBon);
                }
                if (apple.IsEated())
                {
                    apple = (AppleBon)field.CreateItem(TypeOfItem.AppleBon);
                }

                Console.WriteLine("Жизней у Красной Шапочки:{0}", redHood.Health);
                Console.WriteLine("Скорость у Красной Шапочки{0}", redHood.Speed);
                PrintGridField(field);

                //RedHood move
                if (counter % (MAX_SPEED - redHood.Speed + 1) == 0)
                {
                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo key = Console.ReadKey();
                        switch (key.Key)
                        {
                        case ConsoleKey.LeftArrow:
                            redHood.Move(Direction.Left);
                            break;

                        case ConsoleKey.UpArrow:
                            redHood.Move(Direction.Up);
                            break;

                        case ConsoleKey.RightArrow:
                            redHood.Move(Direction.Right);
                            break;

                        case ConsoleKey.DownArrow:
                            redHood.Move(Direction.Down);
                            break;
                        }
                    }
                }
                //wolf move
                if (counter % (MAX_SPEED - wolf.Speed + 1) == 0)
                {
                    direct = rand.Next(1, 4);
                    wolf.Move((Direction)direct);
                }
                //bear move
                if ((counter % (MAX_SPEED - bear.Speed + 1)) == 0)
                {
                    direct = rand.Next(1, 4);
                    bear.Move((Direction)direct);
                }
                counter += 1;

                System.Threading.Thread.Sleep(500);
                Console.Clear();
                if (!redHood.IsAlive())
                {
                    Console.Clear();
                    Console.WriteLine("                GAME OVER");
                    break;
                }
            }
        }