user3814609
user3814609

Reputation: 31

Pygame- Running two things at the same time

I'm creating a typing game that basically shows a word, then checks to see if you typed the word, then moves to a next one. I want this all to be done under a timer. The thing is, well the timer and the game cant run at the same time, any idea on how to do this, here is the code (more will be provided if needed):

def countdown():
    clock = pygame.time.Clock()
    done = False

    font = pygame.font.Font(None, 25)

    frame_count = 0
    frame_rate = 60
    start_time = 15

    # -------- Main Program Loop -----------
    while not done:
        for event in pygame.event.get():  # User did something
            if event.type == pygame.QUIT:  # If user clicked close
                pygame.quit()
                quit()# Flag that we are done so we exit this loop

        button("",600,0,200,50,white,white)


        # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT

        # --- Timer going up ---
        # Calculate total seconds
        total_seconds = frame_count // frame_rate

        # Divide by 60 to get total minutes
        minutes = total_seconds // 60

        # Use modulus (remainder) to get seconds
        seconds = total_seconds % 60

        # --- Timer going down ---
        # --- Timer going up ---
        # Calculate total seconds
        total_seconds = start_time - (frame_count // frame_rate)
        if total_seconds < 0:
            total_seconds = 0

        # Divide by 60 to get total minutes
        minutes = total_seconds // 60

        # Use modulus (remainder) to get seconds
        seconds = total_seconds % 60

        # Use python string formatting to format in leading zeros
        output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)

        # Blit to the screen
        text = font.render(output_string, True, black)

        gameDisplay.blit(text, [600, 0])
        pygame.display.update()

        # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
        frame_count += 1
        # Limit to 20 frames per second
        clock.tick(frame_rate)

        # Go ahead and update the screen with what we've drawn.
        pygame.display.update()
def gameLoop(level):
    pygame.display.update()
    if level == '1':
        gameDisplay.fill(white)
        keyboard()
        for turn in range(3):
            randWord()
            count = 0
            while count < len(chars):
                if chars[count] == 'a':
                    key("A",132.5,430,45,40,white)
                    letter = pygame.K_a
                elif chars[count] == 'b':
                    key("B",342.5,470,45,40,white)
                    letter = pygame.K_b
                elif chars[count] == 'c':
                    key("C",252.5,470,45,40,white)
                    letter = pygame.K_c
                elif chars[count] == 'd':
                    key("D",222.5,430,45,40,white)
                    letter = pygame.K_d
                elif chars[count] == 'e':
                    key("E",207.5,390,45,40,white)
                    letter = pygame.K_e
                elif chars[count] == 'f':
                    key("F",267.5,430,45,40,white)
                    letter = pygame.K_f
                elif chars[count] == 'g':
                    key("G",312.5,430,45,40,white)
                    letter = pygame.K_g
                elif chars[count] == 'h':
                    key("H",357.5,430,45,40,white)
                    letter = pygame.K_h
                elif chars[count] == 'i':
                    key("I",432.5,390,45,40,white)
                    letter = pygame.K_i
                elif chars[count] == 'j':
                    key("J",402.5,430,45,40,white)
                    letter = pygame.K_j
                elif chars[count] == 'k':
                    key("K",447.5,430,45,40,white)
                    letter = pygame.K_k
                elif chars[count] == 'l':
                    key("L",492.5,430,45,40,white)
                    letter = pygame.K_l
                elif chars[count] == 'm':
                    key("M",432.5,470,45,40,white)
                    letter = pygame.K_m
                elif chars[count] == 'n':
                    key("N",387.5,470,45,40,white)
                    letter = pygame.K_n
                elif chars[count] == 'o':
                    key("O",477.5,390,45,40,white)
                    letter = pygame.K_o
                elif chars[count] == 'p':
                    key("P",522.5,390,45,40,white)
                    letter = pygame.K_p
                elif chars[count] == 'q':
                    key("Q",117.5,390,45,40,white)
                    letter = pygame.K_q
                elif chars[count] == 'r':
                    key("R",252.5,390,45,40,white)
                    letter = pygame.K_r
                elif chars[count] == 's':
                    key("S",177.5,430,45,40,white)
                    letter = pygame.K_s
                elif chars[count] == 't':
                    key("T",297.5,390,45,40,white)
                    letter = pygame.K_t
                elif chars[count] == 'u':
                    key("U",387.5,390,45,40,white)
                    letter = pygame.K_u
                elif chars[count] == 'v':
                    key("V",297.5,470,45,40,white)
                    letter = pygame.K_v
                elif chars[count] == 'w':
                    key("W",162.5,390,45,40,white)
                    letter = pygame.K_w
                elif chars[count] == 'x':
                    key("X",207.5,470,45,40,white)
                    letter = pygame.K_x
                elif chars[count] == 'y':
                    key("Y",342.5,390,45,40,white)
                    letter = pygame.K_y
                elif chars[count] == 'z':
                    key("Z",162.5,470,45,40,white)
                    letter = pygame.K_z
                pygame.display.update()
                true = True
                while true:
                    for event in pygame.event.get():
                        if event.type == pygame.KEYDOWN:
                            if event.key == letter:
                                keyboard()
                                count += 1
                                pygame.display.update()
                                true = False
                countdown()
        finishLevel()

Upvotes: 1

Views: 1695

Answers (2)

Luke B
Luke B

Reputation: 2121

Use pygame.time.set_timer(eventid, milliseconds). eventid is 24 in the current version of pygame. For more timers at once, use eventid 25,26, and so on. When a timer goes off, it will generate a pygame.USEREVENT event in the event queue that you can get with pygame.event.get(), which I am assuming you know how to use.

Upvotes: 2

Bartlomiej Lewandowski
Bartlomiej Lewandowski

Reputation: 11180

You can update as many things and draw as many things at the same time, but you need them in the same loop. I would propose for you to restructure the program a bit like so.

class Program:
    def __init__(self):
        timer = Timer()
        game = Game()
    def run():
        while not done:
            for event in pygame.event.get():  # User did something
                if event.type == pygame.QUIT:  # If user clicked close
                    pygame.quit()
                timer.handle_event(event)
                game.handle_event(event)
            timer.update()
            game.update()
            timer.draw(screen)
            game.draw(screen)

class Game:
    #will include all game logic

class Timer:
    #will include all timer logic

You could also extract the timer and pass the updated ms into the update method so that later on the Game object will be able to calculate some time based movement as well.

Upvotes: 1

Related Questions