我的精灵在pygame中不断移出屏幕

时间:2014-04-25 00:57:50

标签: python 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 个答案:

答案 0 :(得分: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
   ...

可能更容易处理...