2017-06-21 147 views
1

我正在pygame庫做一個簡單的遊戲。我注意到一個奇怪的錯誤,如果我點擊其中一個方向鍵來移動我的角色,它會將它向一個方向移動。按下鍵會在您按下的方向上觸發正向加速,然後向上鍵將加速度重置爲零。檢查這些值後,儘管按鍵發生,但加速度仍保持爲正值。查看所有的事件,它註冊一個鍵和鍵,但pygame不運行與該鍵關聯事件關聯的代碼。這可能是我鍵盤的一個信號問題,但它似乎可能是pygame中的事件處理。這裏是我的代碼:pygame失敗檢測鍵盤

import pygame 

pygame.init() 

display_x = 1024 
display_y = 512 

game_display = pygame.display.set_mode((display_x, display_y)) 
clock = pygame.time.Clock() 

fps = 30 
font = pygame.font.SysFont(None, 25) 

max_velocity = 16 
acceleration = 4 


def print_to_screen(text, color, x, y): 
    screen_text = font.render(text, True, color) 
    game_display.blit(screen_text, [x, y]) 


def game_loop(): 
    game_exit = False 

    lead_x = display_x/2 
    lead_y = display_y/2 
    vel_x = 0 
    vel_y = 0 
    acc_x = 0 
    acc_y = 0 
    tile_size = 32 

    while not game_exit: 
     for event in pygame.event.get(): 
      print(event) 
      if event.type == pygame.QUIT: 
       game_exit = True 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_LEFT: 
        acc_x = -acceleration 
       if event.key == pygame.K_RIGHT: 
        acc_x = acceleration 
       if event.key == pygame.K_UP: 
        acc_y = -acceleration 
       if event.key == pygame.K_DOWN: 
        acc_y = acceleration 
      if event.type == pygame.KEYUP: 
       if event.key == pygame.K_LEFT and vel_x < 0: 
        acc_x = 0 
       if event.key == pygame.K_RIGHT and vel_x > 0: 
        acc_x = 0 
       if event.key == pygame.K_UP and vel_y < 0: 
        acc_y = 0 
       if event.key == pygame.K_DOWN and vel_y > 0: 
        acc_y = 0 

     if -max_velocity < vel_x < max_velocity: # if velocity isn't max, adds acceleration to velocity 
      vel_x += acc_x 
     if -max_velocity < vel_y < max_velocity: 
      vel_y += acc_y 
     if vel_x < 0: # velocity decay, if vel is less than 0, adds one. no input = slowed down cube 
      vel_x += 1 
     elif vel_x > 0: 
      vel_x -= 1 
     if vel_y < 0: 
      vel_y += 1 
     elif vel_y > 0: 
      vel_y -= 1 

     if (lead_x + vel_x) < 0: # looks into future: if past border in next frame, sets x coord to 0, stops motion 
      vel_x = 0 
      acc_x = 0 
      lead_x = 0 
     elif (lead_x + vel_x) > (display_x - tile_size): 
      vel_x = 0 
      acc_x = 0 
      lead_x = (display_x - tile_size) 
     if (lead_y + vel_y) < 0: 
      vel_y = 0 
      acc_y = 0 
      lead_y = 0 
     elif (lead_y + vel_y) > (display_y - tile_size): 
      vel_y = 0 
      acc_y = 0 
      lead_y = (display_y - tile_size) 

     lead_x += vel_x 
     lead_y += vel_y 

     game_display.fill((255, 255, 255)) 
     pygame.draw.rect(game_display, (255, 0, 0), [lead_x, lead_y, tile_size, tile_size]) 
     print_to_screen(str(int(lead_x)) + ", " + str(int(lead_y)), (0, 0, 0), 0, 0) 
     if vel_x > max_velocity or vel_y > max_velocity: 
      print_to_screen(str(int(vel_x)) + ", " + str(int(vel_y)), (255, 0, 0), 0, 16) 
     else: 
      print_to_screen(str(int(vel_x)) + ", " + str(int(vel_y)), (0, 0, 0), 0, 16) 
     if acc_x > acceleration or acc_y > acceleration: 
      print_to_screen(str(int(acc_x)) + ", " + str(int(acc_y)), (255, 0, 0), 0, 32) 
     else: 
      print_to_screen(str(int(acc_x)) + ", " + str(int(acc_y)), (0, 0, 0), 0, 32) 

     pygame.display.update() 
     clock.tick(fps) 

game_loop() 
pygame.quit() 
quit() 

下面是從快速點擊的事件數據:

<Event(2-KeyDown {'unicode': '', 'key': 276, 'mod': 0, 'scancode': 75})> 
<Event(3-KeyUp {'key': 276, 'mod': 0, 'scancode': 75})> 

它顯示了一個關鍵的向上和向下的關鍵事件,不知道爲什麼關鍵行動將無法註冊。

回答

2

的問題是,你是否vel_x是與KEYUP事件結合小於或大於0。想想看,當我們向左移動(vel_x < 0),並迅速按下並釋放K_RIGHT會發生什麼,然後vel_x仍然是負的,因此if event.key == pygame.K_RIGHT and vel_x > 0:False,KeyUp事件被忽略,acc_x不會被重置爲0。

你應該檢查是否acc_xvel_x大於0

if event.type == pygame.KEYUP: 
    if event.key == pygame.K_LEFT and acc_x < 0: 
     acc_x = 0 
    if event.key == pygame.K_RIGHT and acc_x > 0: 
     acc_x = 0 

同樣爲UP和DOWN鍵和vel_y,acc_y。

+0

謝謝!不知道爲什麼我列入,但解決了這個問題。 –

+1

嘿,我可以告訴你爲什麼你添加了這個條件。 :P如果你快速按下'vel_x> 0'(或者在你的情況下爲'acc_x')左右按壓,則可能發生播放器完全停止,你必須釋放鍵並再次按下才能再次移動精靈。我認爲在重設「vel」之前檢查球員是否正朝着特定方向移動通常是一個好主意。 – skrx

+0

是的,這可能是爲什麼。似乎現在工作,從那時起我改變了一些其他的代碼。測試一下你說的話,如果我真的快點擊左右快速的加速度會再次卡住,就像你快速點擊一個方向一樣。奇怪你的修復沒有解決這個問題。 –

0

刪除KEYUP塊,並添加這KEYDOWN塊:

if event.type == pygame.KEYDOWN: 
    if event.key == pygame.K_LEFT: 
    acc_x = -acceleration 
    acc_y = 0 
if event.key == pygame.K_RIGHT: 
    acc_x = acceleration 
    acc_y = 0 
if event.key == pygame.K_UP: 
    acc_y = -acceleration 
    acc_x = 0 
if event.key == pygame.K_DOWN: 
    acc_y = acceleration 
    acc_x = 0 
+0

謝謝,但那不是我正在尋找的東西。運動如此複雜的原因是因爲我想對角地加速和移動。您的代碼將刪除此內容,並且在您釋放所有密鑰時播放器不會停止移動。 –

+0

哦,好的。感謝您的承認:) – sukhdeep