2017-06-12 137 views
-1
 for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
     if event.type == pygame.MOUSEBUTTONDOWN: 
      keys_pressed_p2 += 1 
    keys = pygame.key.get_pressed() 
    if keys[pygame.K_w] and (General_knowledge[general_knowlege_questions[0] - 1][5] == "a"): 
     key_right = 1 
    if keys[pygame.K_d] and (General_knowledge[general_knowlege_questions[0] - 1][5] == "b"): 
     key_right = 1 
    if keys[pygame.K_s] and (General_knowledge[general_knowlege_questions[0] - 1][5] == "c"): 
     key_right = 1 
    if keys[pygame.K_a] and (General_knowledge[general_knowlege_questions[0] - 1][5] == "d"): 
     key_right = 1 
    if keys[pygame.K_SPACE]: 
     keys_pressed += 1 
    if keys[pygame.K_UP] and (General_knowledge[general_knowlege_questions[0] - 1][5] == "a"): 
     arrow_key_right = 1 
    if keys[pygame.K_RIGHT] and (General_knowledge[general_knowlege_questions[0] - 1][5] == "b"): 
     arrow_key_right = 1 
    if keys[pygame.K_DOWN] and (General_knowledge[general_knowlege_questions[0] - 1][5] == "c"): 
     arrow_key_right = 1 
    if keys[pygame.K_LEFT] and (General_knowledge[general_knowlege_questions[0]- 1][5] == "d"): 
     arrow_key_right = 1 


    if (key_right == 1) and (rect_width > 0) and (keys_pressed == 1): 
     key_right = 0 
     keys_pressed = 0 
     player_1_score = rect_width/30 
     player_1_score = round(player_1_score, 0) 
     print(player_1_score) 

    if (arrow_key_right == 1) and (rect_width > 0) and (keys_pressed_p2 == 1): 
     arrow_key_right = 0 
     keys_pressed_p2 = 0 
     player_2_score = rect_width/30 
     player_2_score = round(player_2_score, 0) 
     print(player_2_score) 

我的代碼適用於當w,a,s,d鍵與空格鍵一起按下時,但最後一個如果最底部的黑色不起作用。我不明白這一點,因爲兩者都是相同的,除了後者使用鼠標點擊而不是空格鍵。有人可以幫忙嗎?爲什麼我的if語句不起作用? python/pygame

+0

'key_right'和'arrow_key_right'有什麼區別?這也有點不清楚。我不確定發生了什麼,以及準確給你帶來什麼問題。你可以嘗試提供一個更清晰的[mcve]? – idjaw

+0

key_right和arrow_key_right是區分玩家1和玩家2的變量。玩家1以w,a,s d爲控件。如果他們按下正確的選項,那麼key_right變爲1,然後允許下一個if塊激活。 arrow_key_right和player 2也是如此。另外keys_pressed是一個變量,用於確保它們只按空格鍵或單擊一次,所以只有一個來自測驗的答案被選中而不是多個。希望這能說明問題! – Noobcoder

回答

0

我認爲5號線,使問題:

keys_pressed_p2 += 1 

看來操作人員應=而不是+=

如果提到的if語句是...and keys_pressed_p2:而不是...and (keys_pressed_p2 == 1):,您的代碼也可以工作,因爲除0以外的值被認爲是True

祝你好運!

相關問題