埃里克·马特斯(Eric Matthes)撰写的Python速成课程《外星人入侵》中居中太空船在屏幕底部

时间:2019-10-17 23:42:46

标签: python

我已经完成了Python Crash Course中的Alien Invasion项目。我增加了上下左右移动的能力。但是,当我向上移动并撞击外星人3次时,它只是重画了外星人,并且没有将飞船重置回游戏开始时的底部中心。我试图更新center_ship()函数,但是我没有做任何工作。这是我的代码:

def ship_hit(ai_settings, stats, sb, screen, ship, aliens, bullets):
    """Respond to ship being hit by alien."""
    if stats.ships_left > 0:
        #Decrement ships left
        stats.ships_left -= 1

        #update scoreboard
        sb.prep_ships()

        #Empty list of aliens and bullets
        aliens.empty()
        bullets.empty()

        #Create a new fleet and center the ship
        create_fleet(ai_settings, screen, ship, aliens)
        ship.center_ship()

        #Pause
        sleep(0.5)

    else:
        stats.game_active = False
        pygame.mouse.set_visible(True)
        ship.center_ship()

和Ship类:

class Ship(Sprite):

    def __init__(self, ai_settings, screen):
        """Initialize the ship and it's starting position"""
        super(Ship, self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings

        #Load the ship image and get its rect.
        self.image = pygame.image.load('ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        #Start each new ship at the bottom center of the screen
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        #Store a decimal value for the ship's center
        self.center = float(self.rect.centerx)
        self.centery = float(self.rect.centery)
        self.bottom = float(self.rect.bottom)

        #Movement flag
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False

    def update(self):
        """Update ship's position based on movement flag"""
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.center += self.ai_settings.ship_speed_factor
        if self.moving_left and self.rect.left > 0:
            self.center -= self.ai_settings.ship_speed_factor
        if self.moving_up and self.rect.top > 0:
            self.centery -= self.ai_settings.ship_speed_factor
        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.centery += self.ai_settings.ship_speed_factor

        #Update rect from self.center
        self.rect.centerx = self.center
        self.rect.centery = self.centery


    def blitme(self):
        """Draw the ship at it's current location"""
        self.screen.blit(self.image, self.rect)

    def center_ship(self):
        """Center the ship on the screen"""
        self.center = self.screen_rect.bottom

1 个答案:

答案 0 :(得分:0)

您的center_ship()方法看上去与book's resourcescenter_ship()方法不同。

以下是您可以在pygame屏幕上定位对象的各种rect属性中的一个不错的illustration。让我们看一下这一行:

self.center = self.screen_rect.bottom

这是将shipself.center的属性设置为等于屏幕底部的y值。 self.center不是rect属性;它只是与Ship对象关联的变量。