Beispiel #1
0
        static void Main()
        {
            // console window settings
            short windowWidth = 80;
            short windowHeight = 58;

            // set the console window settings
            Console.Title = "Galaxian";
            Console.BufferHeight = Console.WindowHeight = windowHeight; // the size can be changed, but probably will break CoverPage
            Console.BufferWidth = Console.WindowWidth = windowWidth;   // the size can be changed, but probably will break CoverPage
            Console.CursorVisible = false;

            // game settings
            int levels = 2;
            int currentLevel = 1;
            char shellDesign = '*';
            char enemyShellDesign = '|';
            bool playerWon = false;
            // end of game settings

            // Initially starting page
            TitleScreen();
            MainPage();

            // Start game
            FaceTheEnemyMessage();

            // create the player spaceship
            Spaceship playerShip = CreatePlayerShip();

            int enemyShipsNumber = 5; // number of enemy ships on one line. Can be changed later
            List<Spaceship> enemyShips = new List<Spaceship>();
            string enemyDirection = "right"; // first direction
            bool moveLeft = false;

            CreateEnemyShips(enemyShipsNumber, enemyShips);

            // initialize screen buffer
            buffer = new ScreenBuffer(windowWidth, windowHeight);

            // initiate two lists for the stars
            List<Star> backgroundStars = GenerateStars(70);
            List<Star> movedStars = new List<Star>(70);

            // initialize a list for the shells
            List<Shell> shells = new List<Shell>();

            // initialize a list for the enemyShells
            List<enemyShell> enemyShells = new List<enemyShell>();

            // game cycle
            while (playerShip.lives > 0)
            {
                // handle keypresses
                KeyDetector(shellDesign, ref playerShip, ref shells);

                // move enemy Ships
                // we still have enemies alive
                if (enemyShips.Count > 0 && currentLevel <= levels)
                {
                    MoveEnemies(ref enemyShips, ref moveLeft);
                }
                // we don't have any enemies left, but the game is far from finished
                else if(enemyShips.Count == 0 && currentLevel < levels)
                {
                    // increment the level and add enemies
                    currentLevel++;
                    CreateEnemyShips(enemyShipsNumber + currentLevel * 2, enemyShips);
                }
                // no more enemies and no more levels! We won!
                else if (enemyShips.Count == 0 && currentLevel == levels)
                {
                    playerWon = true;
                    break;
                }

                // STARS !
                // move the stars
                movedStars = MoveStars(backgroundStars);
                // draw the new moved stars
                DrawStars(movedStars);
                // save the new stars into the old stars list
                backgroundStars.Clear();
                backgroundStars = new List<Star>(movedStars);
                movedStars.Clear();
                // end of STARS !

                // draw the scoreboard
                DrawScoreboard();

                // draw the player spaceship
                DrawSpaceship(playerShip);

                // draw the spaceship shells
                shells = UpdateShells(shells);
                DrawShells(shells);

                // draw the spaceship shells
                enemyShells = UpdateEnemyShells(enemyShells);
                DrawEnemyShells(enemyShells);

                //draw enemy ships
                foreach (var enemyShip in enemyShips)
                {
                    DrawSpaceship(enemyShip);
                }

                // draw the buffer/screen
                buffer.DrawBuffer();

                // slow down the game
                Thread.Sleep(50);

                // add score points to the player
                if (isPlayerShipHitEnemy(shells, enemyShips))
                {
                    playerScore++;
                }

                // not working - need more investigation
                if (CheckEveryEnemyShipBumpPlayer(enemyShips, playerShip))
                {
                    playerLives--;
                    break;
                }

                // clear the buffer/screen
                buffer.ClearBuffer();
            }

            // clear the console screen and display the results
            Console.Clear();
            if (playerWon)
            {
                Console.WriteLine("YOU WIN!");
                Console.WriteLine("Score: {0}", playerScore);
            }
            else
            {
                Console.WriteLine("LOSER!");
                Console.WriteLine("Score: {0}", playerScore);
            }
        }