コード例 #1
0
ファイル: Game1.cs プロジェクト: GoncaloRod/PacmanWars
        /// <summary>
        /// Reloads the level by moving players and enemies to their initial positions and recreates every Pac-Dot and Power Pellet.
        /// </summary>
        private void ReloadLevel()
        {
            string[] file = File.ReadAllLines($@"{Content.RootDirectory}\board.txt");

            int width  = file[0].Length;
            int height = file.Length;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    switch (file[y][x])
                    {
                    case ' ':
                        PacDot dot = new PacDot(this, new Point(x, y));

                        _pacDots.Add(dot);
                        Components.Add(dot);
                        break;

                    case 'P':
                        PowerPellet pellet = new PowerPellet(this, new Point(x, y));

                        _powerPellets.Add(pellet);
                        Components.Add(pellet);
                        break;
                    }
                }
            }

            foreach (Player player in _players)
            {
                player.ResetPosition();
            }

            foreach (Enemy enemy in _enemies)
            {
                enemy.ResetPosition();
            }
        }
コード例 #2
0
ファイル: Game1.cs プロジェクト: GoncaloRod/PacmanWars
        /// <summary>
        /// Load level when game stars creating board, players, enemies, Pac-Dots and Power Pellets.
        /// </summary>
        private void LoadLevel()
        {
            string[] file = File.ReadAllLines($@"{Content.RootDirectory}\board.txt");

            int width  = file[0].Length;
            int height = file.Length;

            char[,] boardMatrix = new char[width, height];

            _ui           = new UI(this);
            _pacDots      = new List <PacDot>();
            _powerPellets = new List <PowerPellet>();
            _enemies      = new List <Enemy>();

            Components.Add(_ui);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    // Wall             'W'
                    // Invisible Wall   'I'
                    // Empty Space      'E'
                    // "Pac-Dot"        ' '
                    // "Power Pellet"   'P'
                    // Player 1         '1'
                    // Player 2         '2'
                    // Enemy Spawn      'S'

                    switch (file[y][x])
                    {
                    case 'W':       // Wall
                        boardMatrix[x, y] = 'W';
                        break;

                    case 'I':       // Invisible Wall
                        boardMatrix[x, y] = 'I';
                        break;

                    case ' ':       // "Pac-Dot"
                        boardMatrix[x, y] = ' ';

                        PacDot dot = new PacDot(this, new Point(x, y));

                        _pacDots.Add(dot);
                        Components.Add(dot);
                        break;

                    case 'P':       // "Power Pellet"
                        boardMatrix[x, y] = ' ';

                        PowerPellet pellet = new PowerPellet(this, new Point(x, y));

                        _powerPellets.Add(pellet);
                        Components.Add(pellet);
                        break;

                    case '1':       // Player 1
                        boardMatrix[x, y] = ' ';

                        _players[0] = new Player(this, new Point(x, y), _player1Controls, 1, Direction.Right);

                        Components.Add(_players[0]);
                        break;

                    case '2':       // Player 2
                        boardMatrix[x, y] = ' ';

                        _players[1] = new Player(this, new Point(x, y), _player2Controls, 2, Direction.Left);

                        Components.Add(_players[1]);
                        break;

                    case 'S':       // Enemy Spawn
                        boardMatrix[x, y] = ' ';

                        for (int i = 0; i < 4; i++)
                        {
                            Enemy enemy = new Enemy(this, new Point(x, y), i, i * 5.0f);

                            _enemies.Add(enemy);
                            Components.Add(enemy);
                        }
                        break;

                    default:        // Others
                        boardMatrix[x, y] = ' ';
                        break;
                    }
                }
            }

            _board = new Board(this, width, height, boardMatrix);
            Components.Add(_board);

            _graphics.PreferredBackBufferWidth  = width * TileSize;
            _graphics.PreferredBackBufferHeight = (height + 1) * TileSize;
            _graphics.ApplyChanges();
        }