0

I have checked other answers for this problem but mine is a little bit different.

In my code below the score updates when I click on wrong answer but then it changes back to it's initial value, that is confusing me I checked the code and I feel like it is fine but I know that I am missing something or doing something wrong. Please kindly let me know where I am doing wrong. Below is my code.

#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])
Vivek Pabani
  • 452
  • 3
  • 11
  • You are passing the player_score to functions and updating it locally. You may use it as a global variable which will make the score update permanent. – Vivek Pabani May 15 '18 at 02:56
  • I did tried with defining the player_score globally but it was still doing the same thing.Right now it changes as soon as I click but it changes to initial value and on blitting it also changes on click but revert back to initial value. – Waqas Moosa May 15 '18 at 05:20

1 Answers1

0

Passing a variable from one function to another function and changing it there does not update it in the original function. Since you are passing player_score from game_screen() to question1(), update it in question1() and then call draw_score() with this updated value, you see the change for a moment, but player_score value doesn't get updated in your source function game_screen(), so in next call you again pass the original value and not the updated one.

One solution is to use global variable, but global variables are not recommended.

You could try something like this (I am describing an idea instead of updating your code):

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.
Vivek Pabani
  • 452
  • 3
  • 11