问题描述
使用Pygame制作外星人入侵时,发现飞船的左右移动速度不一致,向左移动更快一些
问题解决
要根据self.center更新rect对象
def update(self):
"""根据移动标志调整飞船的位置"""
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
# 根据self.center更新rect对象
self.rect.centerx = self.center
其他要注意的地方:
这里要用两个if判断,如果使用if-elif会出现当两个按键同时按下,飞船向一边走的情况