示例#1
0
        static void Main(string[] args)
        {
            Player player   = new Player();
            bool   gameOver = false;

            while (!gameOver)
            {
                List <Enemy>  enemies    = startUp(player);
                List <Bullet> bullets    = new List <Bullet>();
                bool          playerDied = false;
                while (!playerDied)
                {
                    foreach (Enemy enemy in enemies)
                    {
                        enemy.update();

                        if (enemy.GetType() == typeof(ShootingEnemy))
                        {
                            ShootingEnemy shootingEnemy = (ShootingEnemy)enemy;
                            Bullet        bullet        = shootingEnemy.shootBullet();
                            if (!(bullet == null))
                            {
                                bullets.Add(bullet);
                            }
                        }
                    }

                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.SetCursorPosition(0, 1);
                    Console.Write(player.life);

                    for (int i = 0; i < bullets.Count; i++)
                    {
                        Bullet bullet = bullets[i];
                        bullet.update();
                        if (bullet.y == Console.WindowHeight)
                        {
                            bullets.RemoveAt(i);
                            if (player.x == bullet.x)
                            {
                                playerDied = playerHit(player);
                            }
                        }
                    }

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.SetCursorPosition(player.x, Console.WindowHeight);
                    Console.Write("P");
                    System.Threading.Thread.Sleep(50);
                    Console.SetCursorPosition(player.x, Console.WindowHeight);
                    Console.Write(" ");
                    player.move();

                    foreach (Enemy enemy in enemies)
                    {
                        if (enemy.y == Console.WindowHeight)
                        {
                            if (player.x == enemy.x)
                            {
                                playerDied = playerHit(player);
                            }
                        }
                    }
                }

                if (player.life <= 0)
                {
                    gameOver = true;
                }
            }
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.SetCursorPosition(0, 1);
            Console.Write(0);
            Console.SetCursorPosition(Console.WindowWidth / 2 - 4, Console.WindowHeight / 2);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("Game Over!");
            System.Threading.Thread.Sleep(30000);
        }
示例#2
0
 public Bullet(ShootingEnemy shootingEnemy)
 {
     x = shootingEnemy.x;
     y = shootingEnemy.y + 2;
 }