示例#1
0
        /// <summary>
        /// Main game loop, handling player and guard movement, explosions, etc.
        /// </summary>
        private void GameLoop()
        {
            if (_bomb == null || !_bomb.Detonated)
            {
                // Player and guards cannot move once the bomb detonates.
                Paper = Colors.Black;
                Print(_playerRow, _playerCol, " ");

                if (LastKeyPress == Key.Up && _playerRow > 1 && !_map[_playerRow - 1, _playerCol])
                {
                    _playerRow--;
                }
                else if (LastKeyPress == Key.Down && _playerRow < ScreenRows - 1 && !_map[_playerRow + 1, _playerCol])
                {
                    _playerRow++;
                }
                else if (LastKeyPress == Key.Left && _playerCol > 0 && !_map[_playerRow, _playerCol - 1])
                {
                    _playerCol--;
                }
                else if (LastKeyPress == Key.Right && _playerCol < ScreenCols - 1 && !_map[_playerRow, _playerCol + 1])
                {
                    _playerCol++;
                }
                else if (LastKeyPress == Key.Space && _bomb == null)
                {
                    _bomb = new Bomb(
                        this,
                        _map,
                        _playerRow,
                        _playerCol);
                }

                Pen = Colors.Cyan;
                Print(_playerRow, _playerCol, "¬a");

                // Guards move at half the speed of the player
                if (_frameCount % 2 == 0)
                {
                    foreach (var guard in _guards)
                    {
                        guard.Draw(_playerRow, _playerCol);
                    }
                }

                foreach (var guard in _guards)
                {
                    var shot = guard.ShootIfClear(_playerRow, _playerCol);
                    if (shot)
                    {
                        _playerKilled = true;
                        _state        = GameState.GameOver;
                        break;
                    }
                }

                _frameCount++;
            }

            if (_bomb != null && !_bomb.ExplosionFinished)
            {
                // Draw the bomb
                _bomb.Draw(
                    _playerRow,
                    _playerCol,
                    _guards,
                    ref _score);

                Paper = Colors.Blue;
                Pen   = Colors.White;
                Print(0, 53, _score.ToString("D5"));

                if (_bomb.ExplosionFinished)
                {
                    _playerKilled = _bomb.PlayerKilled;
                    _state        = GameState.GameOver;
                }
            }
        }
示例#2
0
        private void StartNewGame()
        {
            _map = new bool[ScreenRows, ScreenCols];

            // Draw warehouse crates of different shapes.
            Paper = Colors.RosyBrown;
            Pen   = Colors.SaddleBrown;
            // 2x2
            for (var i = 0; i < 250; i++)
            {
                var r = _rnd.Next(1, ScreenRows - 1);
                var c = _rnd.Next(0, ScreenCols - 1);

                Print(r, c, "¬b¬b");
                Print(r + 1, c, "¬b¬b");
                _map[r, c]         = true;
                _map[r + 1, c]     = true;
                _map[r, c + 1]     = true;
                _map[r + 1, c + 1] = true;
            }
            // 2x1
            for (var i = 0; i < 100; i++)
            {
                var r = _rnd.Next(1, ScreenRows);
                var c = _rnd.Next(0, ScreenCols - 1);

                Print(r, c, "¬b¬b");
                _map[r, c]     = true;
                _map[r, c + 1] = true;
            }
            // 1x2
            for (var i = 0; i < 100; i++)
            {
                var r = _rnd.Next(1, ScreenRows - 1);
                var c = _rnd.Next(0, ScreenCols);

                Print(r, c, "¬b");
                Print(r + 1, c, "¬b");
                _map[r, c]     = true;
                _map[r + 1, c] = true;
            }

            // Row 0 is reserved for scores and messages. There won't be any crates drawn in there, but
            // configure the map to ensure guards don't move into that row.
            for (var c = 0; c < ScreenCols; c++)
            {
                _map[0, c] = true;
            }

            Paper = Colors.Black;

            // Position the player and guard(s) at random locations.
            GenerateRandomCharacterPosition(out var row, out var col);
            _playerRow = row;
            _playerCol = col;

            _guards = new List <SecurityGuard>();
            for (var i = 0; i < _numGuards; i++)
            {
                double distance;
                do
                {
                    GenerateRandomCharacterPosition(out row, out col);

                    // Ensure that the guards aren't positioned too close to the player.
                    distance = Math.Sqrt(Math.Pow(Math.Abs(row - _playerRow), 2) + Math.Pow(Math.Abs(col - _playerCol), 2));
                }while (distance < 15);
                var guard = new SecurityGuard(this, row, col, _map);
                _guards.Add(guard);
            }

            Paper = Colors.Blue;
            Print(0, 0, new string(' ', ScreenCols));
            Pen = Colors.Cyan;
            Print(0, 46, "Score:");

            _frameCount   = 0;
            _score        = 0;
            _playerKilled = false;
            _bomb         = null;
            _state        = GameState.InGame;
        }