想请教一下关于pygame 飞机大战 飞机控制移动的一部分
本帖最后由 雷锤 于 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 雷锤 发表于 2022-2-12 22:07
怎么删评论啊
点击编辑,附加选项里面有个删除本贴 isdkz 发表于 2022-2-12 22:50
点击编辑,附加选项里面有个删除本贴
谢谢 检测event里面,self.ship.moving_UP大写了,而判断是否移动y坐标的变量名是小写的up sccer 发表于 2022-2-13 20:58
检测event里面,self.ship.moving_UP大写了,而判断是否移动y坐标的变量名是小写的up
非常感谢
页:
[1]