public void PlaceTeleport(bool[,] walls, DestinationPoint point)
        {
            var random = new Random();
            var x      = 0;
            var y      = 0;

            while (walls[x, y] || (point.X == this.X && point.Y == this.Y))
            {
                x = random.Next(Field.XLimit) + 1;
                y = random.Next(Field.YLimit) + 1;
            }

            X = x;
            Y = y;

            WriteSymbol(X, Y, 'O');
        }
Beispiel #2
0
        public static int Level(int lvlNumber, char playerSymbol)
        {
            PrepareConsole();
            const int frameDelay = 100;
            var       stopwatch  = new Stopwatch();

            var    field    = new Field();
            string fileName = Environment.CurrentDirectory.ToString() + "\\..\\..\\Levels\\Level" + lvlNumber + ".txt";

            bool[,] map = MapCreator.CreateMap(fileName);
            DrawWalls(map);
            GameObject.WriteLevelNumber(0, Field.YLimit + 3, "Level " + lvlNumber, ConsoleColor.Cyan);
            UpdateScore(score);

            #region <lvlEntities>
            var objects = MapCreator.GameObjects(fileName);

            var destinationPoint = new DestinationPoint();
            destinationPoint.Create();
            GameObject.WriteSymbol(destinationPoint.X, destinationPoint.Y, 'E', ConsoleColor.Green);

            var countOfButtons = objects["B"];
            var buttons        = new Button().CreateButtons(countOfButtons);

            var rollingStones = new RollingStone().Create(map, objects["R"]);

            var ghosts = new Ghost().CreateGhosts(objects["G"]);

            var hearts = new Heart().CreateHearts(objects["H"]);

            var coins = new Coin().CreateCoins(objects["C"]);

            var demons = new Demon().CreateDemons(objects["D"]);

            var gate = new GameObject {
                X = Field.XLimit - 1, Y = 1
            };
            GameObject.WriteSymbol(gate.X, gate.Y, '[', ConsoleColor.Red);
            map[gate.X, gate.Y] = true;
            #endregion

            Player player = new Player {
                X = 1, Y = Field.YLimit - 1, LivesCount = livesCount, Symbol = playerSymbol, color = ConsoleColor.Yellow
            };
            player.ShowCoordinatesStatistics(playerSymbol);
            player.UpdateLivesCount();

            bool death    = false;
            bool gameOver = false;

            while (!gameOver)
            {
                stopwatch.Start();

                if (Console.KeyAvailable)
                {
                    var keyPressed = Console.ReadKey(true).Key;
                    while (Console.KeyAvailable)
                    {
                        Console.ReadKey(true);
                    }
                    if (keyPressed == ConsoleKey.Escape)
                    {
                        stopGame = gameOver = true;
                        break;
                    }

                    player.Move(playerSymbol, map, keyPressed);
                }

                if (player.LivesCount <= 0)
                {
                    stopGame = death = gameOver = true;
                    break;
                }

                for (int i = 0; i < Math.Max(buttons.Count, Math.Max(hearts.Count, coins.Count)); i++)
                {
                    if (i < buttons.Count)
                    {
                        GameObject.UpdateObject(buttons[i]);
                    }

                    if (i < hearts.Count)
                    {
                        GameObject.UpdateObject(hearts[i]);
                    }

                    if (i < coins.Count)
                    {
                        GameObject.UpdateObject(coins[i]);
                    }
                }

                for (int i = 0; i < ghosts.Count; i++)
                {
                    ghosts[i].Move();
                }
                for (int i = 0; i < rollingStones.Count; i++)
                {
                    rollingStones[i].Move(map);
                }

                GameObject.UpdateObject(player);

                for (int i = 0; i < ghosts.Count; i++)
                {
                    if (player.X == ghosts[i].X && player.Y == ghosts[i].Y)
                    {
                        player.LivesCount--;
                        player.UpdateLivesCount();
                    }
                }

                for (int i = 0; i < hearts.Count; i++)
                {
                    if (player.X == hearts[i].X && player.Y == hearts[i].Y)
                    {
                        hearts.Remove(hearts[i]);
                        player.LivesCount++;
                        player.UpdateLivesCount();
                    }
                }

                for (int i = 0; i < buttons.Count; i++)
                {
                    if (player.X == buttons[i].X && player.Y == buttons[i].Y && buttons[i].IsStepped == false)
                    {
                        countOfButtons--;
                        buttons[i].IsStepped = true;
                        buttons[i].color     = ConsoleColor.Green;
                    }
                }

                for (int i = 0; i < rollingStones.Count; i++)
                {
                    if (player.X == rollingStones[i].X && player.Y == rollingStones[i].Y)
                    {
                        player.LivesCount--;
                        player.UpdateLivesCount();
                    }
                }

                if (countOfButtons == 0)
                {
                    GameObject.WriteSymbol(gate.X, gate.Y, '[', ConsoleColor.Green);
                    map[gate.X, gate.Y] = false;
                }

                for (int i = 0; i < coins.Count; i++)
                {
                    if (player.X == coins[i].X && player.Y == coins[i].Y)
                    {
                        coins.Remove(coins[i]);
                        score += 10;
                        UpdateScore(score);
                    }
                }
                for (int i = 0; i < demons.Count; i++)
                {
                    demons[i].Move(player);
                }
                for (int i = 0; i < demons.Count; i++)
                {
                    if (player.X == demons[i].X && player.Y == demons[i].Y)
                    {
                        player.LivesCount--;
                        player.UpdateLivesCount();
                    }
                }

                if (player.X == destinationPoint.X && player.Y == destinationPoint.Y)
                {
                    gameOver = true;
                }

                stopwatch.Stop();
                int sleepTime = Math.Max(frameDelay - (int)stopwatch.Elapsed.TotalMilliseconds, 0);
                stopwatch.Reset();
                Thread.Sleep(sleepTime);
            }
            Console.Clear();

            if (death)
            {
                Console.WriteLine("You've lost.");
            }
            else if (stopGame)
            {
                Console.WriteLine("Goodbye.");
            }
            else
            {
                Console.WriteLine("Level completed.");
                score += 1000;
            }

            Thread.Sleep(1000);
            return(score);
        }