0

I'm new to programming and I need a help. So, I'm trying to make a typing game using pygame. What I want to do is let the user choose the timer the user wants for the game. What I'm trying to do is if the function the_time is called, It will overwrite the variables necessary for controlling the time.

How do I make the values inside the button conditional statement, be accessed in another function?

This is my code:

def the_time(Timer):
    while True:
        global usertime, cc, wh, w, h
        win.fill(bg)
#BUTTONS
        MENU_MOUSE_POS = pg.mouse.get_pos()

        MENU_TEXT = text_font.render("SELECT TIME", True, text_correct)
        MENU_RECT = MENU_TEXT.get_rect(center=(720, 100))
        TIME1 = Button(image=pg.image.load("button.png"), pos=(720, 250),
                               text_input="30", font=text_font, base_color=text_color, hovering_color="White")
        TIME2 = Button(image=pg.image.load("button.png"), pos=(720, 400),
                               text_input="60", font=text_font, base_color=text_color, hovering_color="White")
        BACK= Button(image=pg.image.load("button.png"), pos=(720, 540),
                             text_input="BACK", font=text_font, base_color=text_color, hovering_color="White")

        win.blit(MENU_TEXT, MENU_RECT)
        for button in [TIME1, TIME2, BACK]:
            button.changeColor(MENU_MOUSE_POS)
            button.update(win)

        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()
            if event.type == pg.MOUSEBUTTONDOWN:
                if TIME1.checkForInput(MENU_MOUSE_POS):
                    cc = 2.5
                    wh = 2
                    w = 2
                    h = 2.5
                    user_time = 30
                    return Timer
                play()
                if TIME2.checkForInput(MENU_MOUSE_POS):
                    cc = 5
                    wh = 4
                    w = 4
                    h = 5
                    user_time = 60
                    return Timer
                play()
                if BACK.checkForInput(MENU_MOUSE_POS):
                    options()
        pg.display.update()

and I'm hoping the data would go here in my function play():

 if int(end-start) == the_time(Timer):
        run = False
        wpm(correct_char)
    end = time.time()
    draw_board(type_list, correct_st, start, end)
play()

and my function draw_board()

if end-start <= user_time:
    time = time_font.render(str(user_time-int(end-start)), 1, timer_color)
    win.blit(time, (width / 5, height / 2 - 50))
pg.display.update()

and if the time were changed, the WPM values would also change:

def wpm(correct_char):
    run = True
    while run:
        win.fill(bg)
        wpm = time_font.render('WPM:' + str(correct_char / cc), 1, timer_color)
        wid = wpm.get_width()
        win.blit(wpm, ((width//wh) - (wid//w), height/h))
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.display.quit()
                sys.exit()
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_TAB:
                    play()
        pg.display.update()

I hope you can help me. Thankss

I tried changing everything related to the timer etc. but I still failed.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Zeq Crill
  • 1
  • 2
  • It's hard to tell what the the problem actually is. I don't see a question or problem statement. – Carcigenicate Dec 20 '22 at 00:24
  • Sorry about that, my question is how do i fix this: SyntaxError: name 'cc' is assigned to before global declaration – Zeq Crill Dec 20 '22 at 00:33
  • 1
    Why in the world do you have global variable declarations inside a loop?? Put the global declarations at the top of the function. – John Gordon Dec 20 '22 at 00:34

0 Answers0