Beispiel #1
0
        public void Handle(string[] fullCommand)
        {
            Console.Clear();
            Prepare();
            _difficulty           = DifficultyStats.Create((Difficulty)SelectDifficulty());
            Console.CursorVisible = false;
            Console.Clear();

            // Create some enemies.
            CreateEnemies();

            // Draw the Spaceship
            _spaceShip.Draw();

            // Prepare reset event
            _gameOver = new ManualResetEvent(false);

            // Start reading the keybord for movements
            _cycleTimer = new Timer(ReadInput, null, 0, 30);

            // Start the movement timer.
            _moveElementsTimer = new Timer(MoveAllElements, null, 100, 100);

            // Wait for game to finish
            _gameOver.WaitOne();

            Console.Clear();
            Console.WriteLine("Your Score: " + _score);
            Console.WriteLine("Game Over");
            Console.CursorVisible = true;
            Console.ReadLine();
            Console.Clear();
        }
Beispiel #2
0
        /// <summary>
        /// Generate and draw a new food icon
        /// </summary>
        /// <returns></returns>
        private UiElement GenerateFood()
        {
            // Find coordinates not occupied by the snake
            int x = 0, y = 0;

            while (x == 0 && y == 0)
            {
                x = _rnd.Next(Console.WindowWidth);
                y = _rnd.Next(Console.WindowHeight);
                if (_snake.Any(e => e.PositionX == x && e.PositionY == y))
                {
                    x = y = 0;
                }
            }
            var food = new UiElement
            {
                PositionX = x,
                PositionY = y,
                Icon      = FoodIcon,
                Color     = _rnd.Next(3) % 2 == 0 ? ConsoleColor.Red : ConsoleColor.Yellow
            };

            food.Draw();
            return(food);
        }
 public override void Draw(GameTime gameTime, SpriteBatch batch)
 {
     batch.Begin();
     bg.Draw(batch);
     title.Draw(batch);
     foreach (Button button in buttons)
     {
         button.Draw(batch);
     }
     batch.End();
 }
Beispiel #4
0
        /// <summary>
        /// Creates a Bullet for the spaceship and enqueue it to the movement list.
        /// </summary>
        private void Shoot()
        {
            if (_shots.PlayerShots.Count >= _difficulty.BulletsForSpaceShip)
            {
                return;
            }

            var rocket = new UiElement()
            {
                PositionX = _spaceShip.PositionX, PositionY = _spaceShip.PositionY - 2, Icon = "|"
            };

            rocket.Draw();
            _shots.PendingShots.Enqueue(rocket);
        }
Beispiel #5
0
        /// <summary>
        /// Create some enemies.
        /// </summary>
        private void CreateEnemies()
        {
            var npcRows = new List <List <UiElement> >();

            for (var enemyRow = 0; enemyRow < EnemyLines; enemyRow++)
            {
                var row  = new List <UiElement>();
                var ypos = (EnemyLines + 1) - enemyRow;
                var icon = EnemyIcons[enemyRow];

                for (var xpos = 20 + (enemyRow % 2); xpos <= 60 - (enemyRow % 2); xpos += 2)
                {
                    var enemy = new UiElement {
                        PositionX = xpos, PositionY = ypos, Icon = icon
                    };
                    enemy.Draw();
                    row.Add(enemy);
                }
                npcRows.Add(row);
            }
            _npcRows = npcRows.ToArray();
        }
Beispiel #6
0
 /// <summary>
 /// Draws the socre on the screen.
 /// </summary>
 /// <param name="addToScore"></param>
 private void DrawScore(int addToScore)
 {
     _score        = _score + addToScore;
     _scoreUi.Icon = _score.ToString("D8");
     _scoreUi.Draw();
 }
Beispiel #7
0
        /// <summary>
        /// Moves the bullets of the npcs
        /// </summary>
        private void MoveNpcRockets()
        {
            var removedRockets = new List <UiElement>();

            // If it is possible to fire again, then try it.
            if (_shots.NpcShots.Count < _difficulty.NumberOfBulletsAtTheSameTimeFromEnemy)
            {
                // Get a random number if the enemy will fire or not.
                if (_luckCreator.Next(1000) > _difficulty.FireFireFrequenceOfEnemy)
                {
                    // Check the lists, only the lower line of enemies can fire
                    UiElement newShot = null;
                    while (newShot == null && _npcRows.Any(row => row.Count > 0))
                    {
                        var enemyLine = _luckCreator.Next(EnemyLines);
                        var enemies   = _npcRows[enemyLine];
                        if (!enemies.Any())
                        {
                            continue;
                        }
                        var shooter = enemies[_luckCreator.Next(enemies.Count)];
                        newShot = new UiElement
                        {
                            PositionX = shooter.PositionX,
                            PositionY = shooter.PositionY + 2 + enemyLine,
                            Icon      = "|"
                        };
                    }

                    if (newShot != null)
                    {
                        newShot.Draw();
                        _shots.NpcShots.Add(newShot);
                    }
                }
            }
            // move all visible bullets.
            foreach (var shot in _shots.NpcShots)
            {
                var nextY = shot.PositionY + 1;
                if (nextY == Console.WindowHeight)
                {
                    shot.Clear();
                    removedRockets.Add(shot);
                    continue;
                }
                if (_spaceShip.PositionX == shot.PositionX && _spaceShip.PositionY == nextY)
                {
                    EndGame();
                    return;
                }

                // Move shot
                shot.UpdatePosition(shot.PositionX, nextY);
            }

            foreach (var shot in removedRockets)
            {
                _shots.RemovePlayerShot(shot);
            }
        }