马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 雷锤 于 2022-2-12 22:42 编辑
飞机大战上下移动不能向上移动def _check_events(self):
"""响应按键和鼠标事件"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.key == pygame.K_UP:
self.ship.moving_UP = True
elif event.key == pygame.K_DOWN:
self.ship.moving_down = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
elif event.key == pygame.K_UP:
self.ship.moving_up = False
elif event.key == pygame.K_DOWN:
self.ship.moving_down = False
def update(self):
"""根据移动标志调整飞船位置"""
#更新飞船而不是rect的值
if self.moving_right and self.rect.right < self.screen_rect.right:
self.x+= self.settings.ship_speed
if self.moving_left and self.rect.left > self.screen_rect.left:
self.x-=self.settings.ship_speed
if self.moving_up and self.rect.up > self.screen_rect.up:
self.y-=self.settings.ship_speed
if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
self.y+=self.settings.ship_speed
self.rect.x = self.x
self.rect.y = self.y
检测event里面,self.ship.moving_UP大写了,而判断是否移动y坐标的变量名是小写的up
|