2014-04-25 92 views
0

所以我一直在玩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

東西似乎不正確的縮進。

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 
    ... 

可能會更容易處理...