Exemple #1
0
        /// <summary>
        /// Moves ennemies and do some checks
        /// </summary>
        public void GlobalMovesAndChecks()
        {
            Enemy.MoveEnnemies(ref _moveEnnemyAndControlShoot, ref _enemiesSpeed, ref _direction, ref _enemiesArray, _random, ref _bullets, ref _enemiesLimits);
            Shoot.MoveBullets(ref _bulletMove, ref _bullets);

            if (DateTime.Now > _ship.TempInvicibility)
            {
                Console.SetCursorPosition(_ship.PosX, _ship.PosY);
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(_ship.Sprite);
            }

            // check if as bullet hit ennemy then destroy ennemy and bullet
            foreach (Enemy ennemy in _enemiesArray)
            {
                for (int i = 0; i < _bullets.Count; i++)
                {
                    if (_bullets[i] != null && _bullets[i].PosX == ennemy.PosX && _bullets[i].PosY == ennemy.PosY && ennemy.IsAlive)
                    {
                        _bullets[i].DestroyBullet();
                        ennemy.IsAlive = false;
                        ennemy.DestroyEnemy();
                        _ennemyAlive--;
                        if (_difficulty == 0)
                        {
                            _ship.Score   += 100;
                            _enemiesSpeed -= (_enemiesArray.Length - _ennemyAlive) / 5;
                        }
                        else
                        {
                            _ship.Score   += 300;
                            _enemiesSpeed -= (_enemiesArray.Length - _ennemyAlive) / 5;
                        }
                        Hud.PrintPlayerScore(_ship.Score);
                    }
                }

                if (ennemy.IsAlive == true)
                {
                    _gameOver = false;
                }
            }

            // check if a bullet hit a block then destroy part of the block
            foreach (Block block in _blockList)
            {
                for (int i = 0; i < _bullets.Count; i++)
                {
                    if (_bullets[i] != null && block.IsInside(_bullets[i].PosX, _bullets[i].PosY))
                    {
                        _bullets[i].DestroyBullet();
                    }
                }
            }

            // check if a bullet hit the player then decrease lifes
            for (int i = 0; i < _bullets.Count; i++)
            {
                if (_bullets[i] != null && _bullets[i].PosX == _ship.PosX && _bullets[i].PosY == _ship.PosY)
                {
                    _bullets[i].DestroyBullet();
                    Console.SetCursorPosition(_ship.PosX, _ship.PosY);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(_ship.Sprite);
                    Console.ForegroundColor = ConsoleColor.White;

                    // invincibility time (when player is hit) & decrement life
                    if (DateTime.Now > _ship.TempInvicibility)
                    {
                        _ship.Invicibility();
                        Sound.PlaySound(Sound.Sounds.Hit_Hurt);
                        _ship.Life--;
                        Hud.PrintPlayerLifes(_ship.Life);
                    }
                }
            }

            // if player has no more lives or if the ennemies are dead, stop the game and display gameOver
            if (_ship.Life < 1 || _ennemyAlive == 0)
            {
                _gameOver = true;
            }
        }
Exemple #2
0
        /// <summary>
        /// Run game
        /// </summary>
        public void RunGame()
        {
            GC.Collect();
            Console.SetWindowSize(_windowWidth, _windowHeight);
            Console.SetBufferSize(_windowWidth, _windowHeight);

            _ship = new Player(39, 45, 3, 3.0);

            // Play Music
            Sound.PlaySound(Sound.Sounds.Song);

            // Dificulty system
            if (_difficulty == 0)
            {
                _enemiesSpeed = 400;
            }
            else
            {
                _enemiesSpeed = 300;
            }
            Hud.PrintAllInfos(_ship.Score, _ship.Life);
            // Init/Spawn enemies
            for (int y = 0; y < _enemiesArray.GetLength(1); y++)
            {
                for (int x = 0; x < _enemiesArray.GetLength(0); x++)
                {
                    _enemiesArray[x, y] = new Enemy(_enemiesSpawnPoint[0] + (2 * x), _enemiesSpawnPoint[1] + (1 * y));
                }
            }

            // Add blocks
            _blockList.Add(new Block(_blockXSize, _blockYSize, Console.WindowWidth / 4 - 6, 40));
            _blockList.Add(new Block(_blockXSize, _blockYSize, Console.WindowWidth / 4 + 8, 40));
            _blockList.Add(new Block(_blockXSize, _blockYSize, Console.WindowWidth / 4 + 24, 40));
            _blockList.Add(new Block(_blockXSize, _blockYSize, Console.WindowWidth / 4 + 38, 40));

            // init some vars
            ConsoleKeyInfo keyEnterred;

            _timeBeforeShoot           = new DateTime();
            _bulletMove                = new DateTime();
            _moveEnnemyAndControlShoot = new DateTime();

            // wait some time before start -> don't surprise player
            System.Threading.Thread.Sleep(400);

            // Main while (player input, enemies moves, bullets moves, ...)
            do
            {
                // do if game is not paused
                if (_gamePaused == false)
                {
                    GlobalMovesAndChecks();

                    if (Console.KeyAvailable)
                    {
                        keyEnterred = Console.ReadKey(true);
                        switch (keyEnterred.Key)
                        {
                        // Move right
                        case ConsoleKey.RightArrow:
                            _ship.Move(1);
                            break;

                        // Move left
                        case ConsoleKey.LeftArrow:
                            _ship.Move(-1);
                            break;

                        // Shoot
                        case ConsoleKey.Spacebar:
                            // wait one second before shoot again
                            if (DateTime.Now > _timeBeforeShoot)
                            {
                                _timeBeforeShoot = DateTime.Now.AddSeconds(_reloadTime);
                                Sound.PlaySound(Sound.Sounds.Laser_Shoot);
                                _bullets.Add(new Shoot(_ship.PosX, _ship.PosY - 1, -1));
                            }
                            break;

                        // Pause game
                        case ConsoleKey.Escape:
                            _gamePaused = true;
                            _menu.PauseMenu();
                            _gamePaused = false;
                            break;
                        }
                    }
                }
            }while (_gameOver == false);

            //Calls the methods according if you won or lost
            if (_ship.Life < 1)
            {
                _menu.GameOver(_ship.Score);
            }
            else
            {
                Console.Clear();
                _menu.Win(_ship.Score);
            }
        }