Python 更新Pygame分数文本

Python 更新Pygame分数文本,python,pygame,Python,Pygame,我已经检查了这个问题的其他答案,但我的有点不同 在我下面的代码中,当我点击错误的答案时,分数会更新,但它会变回初始值,这让我很困惑。我检查了代码,感觉很好,但我知道我遗漏了什么或做了什么错误。请告诉我哪里做错了。下面是我的代码 #GAME SCREEN def game_screen(): player_score = 25 timer = pygame.time.get_ticks() start = True while start : fo

我已经检查了这个问题的其他答案,但我的有点不同

在我下面的代码中,当我点击错误的答案时,分数会更新,但它会变回初始值,这让我很困惑。我检查了代码,感觉很好,但我知道我遗漏了什么或做了什么错误。请告诉我哪里做错了。下面是我的代码

#GAME SCREEN
def game_screen():

    player_score = 25
    timer = pygame.time.get_ticks()
    start = True
    while start :
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        seconds = (pygame.time.get_ticks()- timer)/1000

        main_font = pygame.font.Font("ArialRounded.TTF", 22)
        sub_font = pygame.font.Font("ArialRounded.TTF", 22)
        timer_font = sub_font.render(str(seconds), True, SEABLUE)
        question_font = main_font.render("Question:", True, SEABLUE)

        star_img = pygame.image.load("starscore.png")
        menu_screen_img = pygame.image.load("quizzappbackgroundscreen.png")
        blureffect_img = pygame.image.load("blureffect.png")
        onoff_button_img = pygame.image.load("onoffbutton.png")
        knobone_img = pygame.image.load("knob_a.png")
        knobtwo_img = pygame.image.load("knob_a.png")

        knobrect_a = knobone_img.get_rect(center=(97.5,647.5))
        knobrect_b = knobtwo_img.get_rect(center=(514.5,647.5))
        mpos = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        if knobrect_a.collidepoint(mpos):
            knobone_img = pygame.image.load("knob_b.png")
            if click[0] == 1:
                knobone_img = pygame.image.load("rotatedknob_a.png")
                click_sound.set_volume(0.3)
                click_sound.play()

        if knobrect_b.collidepoint(mpos):
            knobtwo_img = pygame.image.load("knob_b.png")
            if click[0] == 1:
                knobtwo_img = pygame.image.load("rotatedknob_a.png")
                click_sound.set_volume(0.3)
                click_sound.play()

        screen.blit(menu_screen_img, [0,0])
        screen.blit(star_img, [50,47])
        screen.blit(timer_font, [485,55])
        screen.blit(question_font, [50,95])
        question1(player_score)
        screen.blit(blureffect_img, [0,0])
        screen.blit(onoff_button_img, [25,726])
        screen.blit(knobone_img, [50,599])
        screen.blit(knobtwo_img, [465,599])

        pygame.display.update()

#QUESTIONS FUNCTIONS

def question1(player_score):

    main_font = pygame.font.Font("ArialRounded.TTF", 20)
    question_font = main_font.render("Are the points G, C, A, and Y coplanar?", True, SEABLUE)
    option1_font = main_font.render("- Yes", True, SEABLUE)
    option2_font = main_font.render("- No", True, SEABLUE)
    question_img1 = pygame.image.load("question1img.png")

    option1rect = option1_font.get_rect(center=(93.5,402))
    option2rect = option2_font.get_rect(center=(89.5,452))
    mpos = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if option1rect.collidepoint(mpos):
        option1_font = main_font.render("- Yes", True, DSEABLUE)
        if click[0] == 1:
            print("Right Answer")
            #right_answers += 1
            print(right_answers)

    if option2rect.collidepoint(mpos):
        option2_font = main_font.render("- No", True, DSEABLUE)
        if click[0] == 1:
            print("Wrong Answer")
            player_score -=1


    screen.blit(question_font,[60,130])
    screen.blit(question_img1,[150,170])    
    screen.blit(option1_font, [70, 390])
    screen.blit(option2_font, [70, 430])
    draw_score(player_score)

#DRAW SCORE TEXT
def draw_score(player_score):

    font = pygame.font.Font("ArialRounded.TTF", 22)
    text = font.render("x" + str(player_score), True, SEABLUE)
    screen.blit(text, [85,55])

将一个变量从一个函数传递到另一个函数并在那里进行更改不会更新原始函数中的变量。由于您正在将
player\u score
game\u screen()
传递到
question1()
,在
question1()
中更新它,然后使用此更新值调用
draw\u score()
,您会看到片刻的变化,但是
player\u score
值在源函数
game\u screen()中不会得到更新
,因此在下一次调用中,您再次传递原始值,而不是更新后的值

一种解决方案是使用全局变量,但是

您可以尝试以下方法(我描述的是一个想法,而不是更新代码):


您正在将player_分数传递给函数并在本地更新它。您可以将其用作全局变量,这将使分数更新永久。我曾尝试全局定义玩家的分数,但它仍在执行相同的操作。现在,它会在我单击时立即更改,但会更改为初始值,并且在单击时也会更改,但会恢复为初始值。出现以下错误:+=上不支持的操作数类型另一件事是,这只是一个问题,我将有不止一个问题。好的,这里的想法不是更新单个问题函数中的玩家分数,而是在原始游戏函数中。不管你有多少问题。
def game_screen():

    player_score = 25    # your start score.

    while True:
        # receive question score here, update player's score and display it.
        question_score = question1()
        player_score += question_score
        draw_score(player_score)

def question1():

    # return question score from here regardless of player's current score.
    if correct_answer:
        return 1     # or return your value for correct answer
    else:
        return -1    # or return your value for incorrect answer

def draw_score(score):

    # your draw logic here.