-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
'key_right'和'arrow_key_right'有什么区别?这也有点不清楚。我不确定发生了什么,以及准确给你带来什么问题。你可以尝试提供一个更清晰的[mcve]? – idjaw
key_right和arrow_key_right是区分玩家1和玩家2的变量。玩家1以w,a,s d为控件。如果他们按下正确的选项,那么key_right变为1,然后允许下一个if块激活。 arrow_key_right和player 2也是如此。另外keys_pressed是一个变量,用于确保它们只按空格键或单击一次,所以只有一个来自测验的答案被选中而不是多个。希望这能说明问题! – Noobcoder