python - Pygame-同时运行两件事

标签 python timer pygame

我正在创建一个打字游戏,它基本上显示一个单词,然后检查您是否键入了该单词,然后移动到下一个单词。我希望这一切都在计时器下完成。问题是,定时器和游戏不能同时运行,任何关于如何做到这一点的想法,这里是代码(如果需要,将提供更多):

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()

最佳答案

使用pygame.time.set_timer(eventid, milliseconds)eventid 在当前版本的 pygame 中是 24。要同时使用多个计时器,请使用 eventid 25,26 等。当计时器关闭时,它将在事件队列中生成一个 pygame.USEREVENT 事件,您可以使用 pygame.event.get() 获取该事件,我假设您知道如何使用。

关于python - Pygame-同时运行两件事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31954291/

相关文章:

python - 在 Ruby 中是否有 Python 的 vars() 方法的模拟?

swift - 为什么我的 swift 计时器一直在加速?

python - 怎样才能增加难度呢? (例如增加敌人的速度、增加敌人的数量)

python - 当数据流入 python 时,将数据输出到文本文件中

python - Matplotlib - 强制在脚本内显示

c++ - 在计时器执行过程中使用 c++ KillTimer() 是否安全?

java - 如何将计时器融入到心跳动画中?

python - Pygame 应用程序在 Mac 上的运行速度比在 PC 上慢

python - Pygame,从二维 numpy 数组创建灰度

python - 从元组列表中删除一个元组