更新Pygame Score文本

时间:2018-05-15 02:41:38

标签: python pygame

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

在下面的代码中,当我点击错误的答案然后它更改回原来的初始值时,分数会更新,这让我感到困惑,我检查了代码,我觉得它很好,但我知道我我错过了什么或做错了什么。请让我知道我做错了什么。以下是我的代码。

#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])

1 个答案:

答案 0 :(得分:0)

将变量从一个函数传递到另一个函数并在其中进行更改不会在原始函数中更新它。由于您将player_scoregame_screen()传递到question1(),请在question1()中更新,然后使用此更新值调用draw_score(),您会立即看到更改,但player_score值未在源函数game_screen()中更新,因此在下次调用中,您再次传递原始值而不是更新的值。

一种解决方案是使用全局变量,但global variables are not recommended.

您可以尝试这样的事情(我正在描述一个想法,而不是更新您的代码):

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.