为什么我的if语句不起作用?巨蟒/ pygame的

时间:2017-06-12 00:27:27

标签: python pygame

     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键与空格键一起按下的情况,但是,最后一个如果黑色在最底部不起作用。我不明白这两个是相同的,除了后者使用鼠标点击而不是空格键。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我认为第5行有问题:

keys_pressed_p2 += 1

运营商似乎应该=而不是+=

如果提及的if语句为...and keys_pressed_p2:而非...and (keys_pressed_p2 == 1):,则您的代码也会有效,因为0以外的值被认为是True

祝你好运!