2017-12-02 20 views
1

ユーザーがWASDをクリックするとsmooth_xまたはsmooth_yが5になり、動きをシミュレートするためにxまたはy座標が常に追加されます。Pygame:同時にKEYDOWNとKEYUPが同時に起動しない

しかし、ユーザーがAキーを押し続けているときに同時にDキーを押すと、smooth_xが0になり、ユーザーがその場所に留まるという問題があります。ユーザがクリックするとDが保持され、次にAを保持してDを移動してsmooth_xが0になると、ユーザは移動を停止し、欲しい。このシナリオではsmooth_x =あるべき-5あなたはKEYDOWNではなく、反対の符号をどうのように

while gameloop == True: 
num_scraps = 0 
fps.tick(60) #Sets FPS to 60 
for event in pygame.event.get(): #Checks each event 
    if event.type == pygame.QUIT: #If one of the events are quit (when the user clicks the X in the top right corner) the window closes 
     pygame.quit() 
    if event.type == pygame.KEYUP: 
     print(event) 
     #If the user stop pressing one of the arrow keys it sets all the smooth values to 0 so it stops increasing the x or y coordinate 
     if event.key == pygame.K_w: 
      smoothy = 0 
     if event.key == pygame.K_s: 
      smoothy = 0 
     if event.key == pygame.K_a: 
      smoothx = 0 
     if event.key ==pygame.K_d: 
      smoothx = 0 

    if event.type == pygame.KEYDOWN: #Checks for a keypress 
     print(event) 
     if event.key == pygame.K_w: 
      smoothy -= 5 #reduces the y by 5 so player moves up 
     if event.key == pygame.K_s: 
      smoothy += 5 #increases the y by 5 so player moves down 
     if event.key == pygame.K_a: 
      smoothx -= 5 #reduces the x by 5 so player moves left 
     if event.key == pygame.K_d: 
      smoothx += 5 #increases the x by 5 so player moves right 
+0

は0に設定されていませんが、KEYDOWNのように値を加算/減算します – furas

答えて

0

Iは、通常、このように移動を扱う:私はキーが押された場合に所望の値にx軸とy速度を設定し、メインループ内の位置を更新します。左右の動きを止めるには、文字が左か右に移動しているかどうかを確認してから、値を0に戻す前にsmoothx > 0をチェックしてください。そして、左と右の両方のキーを押すと文字が停止しません同じ時間。上下動用

if event.type == pygame.KEYDOWN: 
    if event.key == pygame.K_w: 
     smoothy = -5 
    elif event.key == pygame.K_s: 
     smoothy = 5 
    elif event.key == pygame.K_a: 
     smoothx = -5 
    elif event.key == pygame.K_d: 
     smoothx = 5 
elif event.type == pygame.KEYUP: 
    if event.key == pygame.K_w and smoothy < 0: 
     smoothy = 0 
    elif event.key == pygame.K_s and smoothy > 0: 
     smoothy = 0 
    elif event.key == pygame.K_a and smoothx < 0: 
     smoothx = 0 
    elif event.key ==pygame.K_d and smoothx > 0: 
     smoothx = 0 

それは同時に両方の上下を押すことは非常に難しいので、それは、それほど重要ではありませんが、もちろんあなたにも確認することを確認することができます。 KEYUP内の

+0

ユーザが狂ったようにマッシしたり、左を押して右に何度も押すと完全には機能しませんあなたがすぐに左と右を押す通常の場合、それは正常に動作します。 – skrx

0

使用はKEYUPに/ substractを追加します。

if event.type == pygame.KEYUP: 
     print(event) 
     if event.key == pygame.K_w: 
      smoothy += 5 
     if event.key == pygame.K_s: 
      smoothy -= 5 
     if event.key == pygame.K_a: 
      smoothx += 5 
     if event.key ==pygame.K_d: 
      smoothx -= 5 

    if event.type == pygame.KEYDOWN: #Checks for a keypress 
     print(event) 
     if event.key == pygame.K_w: 
      smoothy -= 5 #reduces the y by 5 so player moves up 
     if event.key == pygame.K_s: 
      smoothy += 5 #increases the y by 5 so player moves down 
     if event.key == pygame.K_a: 
      smoothx -= 5 #reduces the x by 5 so player moves left 
     if event.key == pygame.K_d: 
      smoothx += 5 #increases the x by 5 so player moves right 
関連する問題