我的精灵在pygame中总是移出屏幕
我最近在用pygame玩精灵和移动的东西,但我遇到一个问题:当我按住键的时候,精灵一直在动,不会停下来。但是如果它已经超过了应该停的位置,然后我松开键再试一次,它就会按预期停下来,不再移动。有没有办法让我精灵能立刻停下来,这样它就不会从我的屏幕上消失了呢?
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if player.x + 4 < 700:
moveX = 4
else:
moveX = 0
if event.key == pygame.K_LEFT:
if player.x == 0:
moveX = 0
else:
moveX = -4
if event.key == pygame.K_UP:
moveY = -4
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
moveX = 0
if event.key == pygame.K_LEFT:
moveX = 0
if event.key == pygame.K_UP:
moveY = 0
player.falling = True
player.collision = False
player.onground = False
1 个回答
1
看起来缩进有点问题。
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if player.x + 4 < 700:
moveX = 4 # <----- should this be here ?
else: # <----- this should match the `event.key == pygame.K_RIGHT` condition
moveX = 0
if event.key == pygame.K_LEFT:
if player.x == 0:
moveX = 0
else:
moveX = -4
if event.key == pygame.K_UP:
moveY = -4
你能重新调整一下缩进并检查一下吗?
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if player.x + 4 < 700: moveX = 4
else: moveX = 0
if event.key == pygame.K_LEFT:
if player.x == 0: moveX = 0 # here you might want to try player.x <= 0 ...
else: moveX = -4
...
这样可能会更容易处理……