コード例 #1
0
def create_alien(cfg, screen, aliens, alien_number, row_number):
    alien = Alien(cfg, screen)
    alien_width = alien.rect.width  # 一个外星人的宽度
    alien.x = alien_width + 2 * alien_width * alien_number
    alien.rect.x = alien.x
    alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
    aliens.add(alien)
コード例 #2
0
 def _create_alien(self, alien_number, row_number):
     alien = Alien(self)
     alien_width, alien_height = alien.rect.size
     alien.x = alien_width + 2 * alien_width * alien_number
     alien.rect.x = alien.x
     alien.rect.y = alien_height + 2 * alien_height * row_number
     self.aliens.add(alien)
コード例 #3
0
def create_alien(ai_settings, screen, aliens, alien_number, row_number):
    """创建一个外星人并将其放在当前行"""
    alien = Alien(ai_settings, screen)
    alien_width = alien.rect.width
    alien.x = alien_width + 2 * alien_width * alien_number
    alien.rect.x = alien.x
    alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
    aliens.add(alien)
コード例 #4
0
def create_alien(ai_settings, screen, aliens, alien_number, row_number):
    # create a alien and add it to current row
    alien = Alien(ai_settings, screen)
    alien_width = alien.rect.width
    alien.x = alien_width + 2 * alien_width * alien_number
    alien.rect.x = alien.x
    alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
    aliens.add(alien)
コード例 #5
0
def create_alien(ai_settings, screen, aliens, alien_number, row_number):
    #Cria um alienígena e o posiciona na linha
    alien = Alien(ai_settings, screen)
    alien_width = alien.rect.width
    alien.x = alien_width + 2 * alien_width * alien_number
    alien.rect.x = alien.x
    alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
    aliens.add(alien)
コード例 #6
0
def create_alien(setting, screen, aliens, row_number, alien_number):
    alien = Alien(setting, screen)
    alien_width = alien.rect.width
    alien_height = alien.rect.height
    alien.x = alien_width + 2 * alien_width * alien_number
    alien.y = alien_height + 2 * alien_height * row_number
    alien.rect.x = alien.x
    alien.rect.y = alien.y
    aliens.add(alien)
コード例 #7
0
 def _create_alien(self, alien_number, row_number):
     """ Create an alien and place it in the row """
     alien = Alien(self)
     alien_width, alien_height = alien.rect.size
     # Double indent from left side of the screen for every even row
     # indent_width = alien_width if row_number % 2 == 0 else 2 * alien_width
     alien.x = alien_width + 2 * alien_width * alien_number
     alien.rect.x = alien.x
     alien.rect.y = alien_height + 2 * alien_height * row_number
     self.aliens.add(alien)
コード例 #8
0
def create_fleet(cfg, screen, aliens, ship):
    # 创建一个外星人,并计算一行可容纳多少个外星人
    alien = Alien(cfg, screen)
    number_aliens_x = get_number_aliens_x(cfg, alien.rect.width)
    number_rows = get_number_rows(cfg, ship.rect.height, alien.rect.height)
    for row_number in range(number_rows):
        for alien_number in range(number_aliens_x):
            create_alien(cfg, screen, aliens, alien_number, row_number)
コード例 #9
0
def create_fleet(setting: Settings, screen, ship, alias):
    alien = Alien(setting, screen)
    number_aliens_x = get_numbers_alien_x(setting, alien.rect.width)
    number_row = get_number_rows(setting, ship.rect.height, alien.rect.height)

    for row_number in range(0, number_row):
        for alien_number in range(0, number_aliens_x):
            create_alien(setting, screen, alias, row_number, alien_number)
コード例 #10
0
ファイル: alien_invasion.py プロジェクト: zeroxuw/PyGame
def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    # screen = pygame.display.set_mode((1200, 800))
    pygame.display.set_caption("Alian Invasion")

    # 创建Play按钮
    play_button = Button(ai_settings, screen, "Play")

    # 设置背景颜色
    bg_color = (85, 204, 220)

    # 创建一艘飞船
    ship = Ship(ai_settings, screen)

    # 创建一个用于存储子弹的编组
    bullets = Group()

    # 创建一艘飞船、一个子弹编组和一个外星人编组
    aliens = Group()

    # 创建一个外星人
    alien = Alien(ai_settings, screen)

    # 开始游戏的主循环
    while True:
        gf.check_events(ai_settings, screen, play_button, ship, bullets)
        ship.update()
        # bullets.update()

        # 删除已消失的子弹
        # for bullet in bullets.copy():
        #     if bullet.rect.bottom <= 0:
        #         bullet.remove(bullet)
        print(len(bullets))
        gf.update_bullets(bullets)
        gf.update_screen(ai_settings, screen, ship, alien, bullets,
                         play_button)
        gf.update_screen(ai_settings, screen, ship, aliens, bullets)
        # 创建外星人群
        gf.create_fleet(ai_settings, screen, ship, aliens)

        # 监视键盘和鼠标事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exeit()

        # 每次循环时都重绘屏幕
        screen.fill(ai_settings.bg_color)
        # screen.fill(bg_color)
        ship.blitme()
        # 让最终绘制的屏幕可见
        pygame.display.flip()
コード例 #11
0
    def _create_fleet(self):
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        available_space_x = self.settings.screen_width - (alien_width * 2)
        number_aliens_x = available_space_x // (2 * alien_width)

        ship_height = self.ship.rect.height
        available_space_y = self.settings.screen_height - 3 * alien_height - ship_height
        number_rows = available_space_y // (2 * alien_height)

        for row_number in range(number_rows):
            for alien_number in range(number_aliens_x):
                self._create_alien(alien_number, row_number)
コード例 #12
0
def create_fleet(ai_settings, screen, ship, aliens):
    """创建外星人群"""
    # 创建一个外星人,并计算一行可容纳多少个外星人
    # 外星人间距为外星人宽度
    alien = Alien(ai_settings, screen)
    number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width)
    number_rows = get_number_rows(ai_settings, ship.rect.height,alien.rect.height)

    # 创建外星人群
    for row_number in range(number_rows):
        for alien_number in range(number_aliens_x):
            create_alien(ai_settings, screen, aliens, alien_number,
                         row_number)
コード例 #13
0
def create_fleet(ai_settings, screen, ship, aliens):
    """create group of aliens"""
    # create a alien ,and calculate that a line can accommodate the amount of alines.
    # the space between aliens is alien's width.
    alien = Alien(ai_settings, screen)
    alien_width = alien.rect.width
    number_aliens_x = get_number_aliens_x(ai_settings, alien_width)
    number_rows = get_number_rows(ai_settings, ship.rect.height,
                                  alien.rect.height)

    # create a row aliens
    for row_number in range(number_rows):
        for alien_number in range(number_aliens_x):
            # create a alien and add it to current row
            create_alien(ai_settings, screen, aliens, alien_number, row_number)
コード例 #14
0
def create_fleet(ai_settings, screen, ship, aliens):
    """Cria uma frota completa de alienígenas."""
    # Cria um alienígena e calcula o número de alienígenas em uma linha
    alien = Alien(ai_settings, screen)
    number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width)
    number_rows = get_number_rows(
        ai_settings,
        ship.rect.height,
        alien.rect.height,
    )

    # Cria a frota de alienígenas
    for row_number in range(number_rows):
        for alien_number in range(number_aliens_x):
            create_alien(ai_settings, screen, aliens, alien_number, row_number)
コード例 #15
0
    def _create_fleet(self):
        """ Create the fleet of aliens """
        # Create and alien and finds a number of aliens in the row
        # Spacing between each alien is equal to one alien width
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        ship_height = self.ship.rect.height
        # Defying the number of alien to fill all available space in a row
        available_space_in_row = self.settings.screen_width - 2 * alien_width
        number_aliens_in_row = available_space_in_row // (2 * alien_width)
        # Determine a number of rows of alien to fit the screen
        available_space_for_rows = self.settings.screen_height - ship_height - (5 * alien_height)
        number_of_rows = available_space_for_rows // (2 * alien_height)

        # Creates the fleet of aliens
        for row_number in range(number_of_rows):
            for alien_number in range(number_aliens_in_row):
                self._create_alien(alien_number, row_number)