Python Pygame.time.set_time,我想让它在特定点发生

Python Pygame.time.set_time,我想让它在特定点发生,python,python-2.7,pygame,Python,Python 2.7,Pygame,我试图使pygame.time.set_time()仅在单击后运行。我曾尝试将pygame.time.set_time放在if语句中,但没有成功。非常感谢您的回复!将计时器启动置于MOUSEBUTTONDOWN事件中时,它将仅在单击鼠标时启动,但每次单击时它都会重置。单击在何处递增?在这段代码中它没有改变。我添加了程序的所有代码。我添加了一个timer_on变量和一个if来测试它。如果未设置,计时器将启动。请看下面。因此,出现的问题是,每次我单击时,计时器都会重置。在下面,它显示gameExit

我试图使
pygame.time.set_time()
仅在
单击后运行。我曾尝试将pygame.time.set_time放在if语句中,但没有成功。非常感谢您的回复!将计时器启动置于
MOUSEBUTTONDOWN
事件中时,它将仅在单击鼠标时启动,但每次单击时它都会重置。

单击在何处递增?在这段代码中它没有改变。我添加了程序的所有代码。我添加了一个timer_on变量和一个if来测试它。如果未设置,计时器将启动。请看下面。因此,出现的问题是,每次我单击时,计时器都会重置。在下面,它显示gameExit=True添加pygame.time.set_timer(ENDTIMER,0)和timed=pygame.time.set_timer(ENDTIMER,1000)添加timer_on=True。请将答案标记为正确!!谢谢很高兴它有帮助!
import pygame
import time
import random

pygame.mixer.pre_init(44100, 16, 2, 4096)
pygame.init()

boop_sound = pygame.mixer.Sound("boop.wav")

display_width = 800
display_height = 600

white = (255,255,255)
black = (0,0,0)

blue = (0, 0, 150)

red = (200,0,0)
light_red = (255,0,0)

yellow = (200,200,0)
light_yellow = (255,255,0)

green = (34,177,76)
light_green = (0, 255, 0)


clock = pygame.time.Clock()

smallfont = pygame.font.SysFont("Rockwell", 25)
medfont = pygame.font.SysFont("Rockwell", 35)
largefont = pygame.font.SysFont("Rockwell", 50)

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("How fast can you tap?")

##icon = pygame.image.load("apple.png")#should be 32x32
##pygame.display.set_icon(icon)

pygame.display.update()

def score(score):
    text = smallfont.render("Clicks: "+str(score), True, blue)
    gameDisplay.blit(text, [2,0])

def game_intro():

    intro = True

    while intro:

        for event in pygame.event.get():
            if event.type== pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_c:
                    intro = False
                if event.key == pygame.K_q:
                    pygame.quit()
                    quit()

        gameDisplay.fill(white)
        message_to_screen("How many times can you",
                          blue,
                          -80,
                          "large")
        message_to_screen("click the button before the time runs out?",
                          blue,
                          -10,
                          "medium")

        #message_to_screen("Press C to play, P to pause or Q to quit.",black,180)

        button("Play", 150, 500, 100, 50, green, light_green, action="Play")
        button("How to play", 325,500,150,50, yellow, light_yellow, action="How to play")
        button("Quit", 550,500,100,50, red, light_red, action="Quit")


        pygame.display.update()
        clock.tick(15)

def game_over():

    game_over = True

    while game_over:

        for event in pygame.event.get():
            if event.type== pygame.QUIT:
                pygame.quit()
                quit()


        gameDisplay.fill(white)
        message_to_screen("Out Of Time!",
                          red,
                          -100,
                          "large")
        message_to_screen("You clicked: " + str(click) + " times",
                          blue,
                          -30)

        button("Play Again", 325, 440, 150, 50, green, light_green, action="Play")
        button("Quit", 350,500,100,50, red, light_red, action="Quit")


        pygame.display.update()
        clock.tick(15)

def text_objects(text, color, size):
    if size == "small":
        textSurface = smallfont.render(text, True, color)
    elif size == "medium":
        textSurface = medfont.render(text, True, color)
    elif size == "large":
        textSurface = largefont.render(text, True, color)

    return textSurface, textSurface.get_rect()


def text_to_button(msg, color, buttonx, buttony, buttonwidth, buttonheight, size = "small"):
    textSurf, textRect = text_objects(msg, color, size)
    textRect.center = ((buttonx +((buttonwidth/2)), buttony+(buttonheight/2)))
    gameDisplay.blit(textSurf, textRect)

def message_to_screen(msg,color, y_displace=0, size = "small"):
    textSurf, textRect = text_objects(msg, color, size)
    #screen_text = font.render(msg, True, color)
    #gameDisplay.blit(screen_text, [display_width/2, display_height/2])
    textRect.center = (display_width/ 2), (display_height / 2) + y_displace
    gameDisplay.blit(textSurf, textRect)



def game_controls():


    gcont = True

    while gcont:

        for event in pygame.event.get():
            if event.type== pygame.QUIT:
                pygame.quit()
                quit()


        gameDisplay.fill(white)
        message_to_screen("How to play", blue, -100,"large")

        message_to_screen("You have to click the button as many times", black, -40)
        message_to_screen("as you possible can before the time runs out", black, -20)

        button("Play", 150, 500, 100, 50, green, light_green, action="Play")
        button("Main", 350,500,100,50, yellow, light_yellow, action="Main")
        button("Quit", 550,500,100,50, red, light_red, action="Quit")


        pygame.display.update()
        clock.tick(15)




def button(text, x, y, width, height, inactive_color, active_color, action = None):
    cur = pygame.mouse.get_pos()
    clicked = pygame.mouse.get_pressed()
    global click



    if x + width > cur[0] > x and y + height > cur[1] > y:
        pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
        if clicked[0] == 1 and action != None:
            if action == "Quit":
                pygame.quit()
                quit()

            if action == "How to play":
                game_controls()
            if action == "Play":
                gameLoop()
            if action == "Main":
                game_intro()
            if action == "Click":
                click += 1








    else:
        pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))

    text_to_button(text, black, x, y, width, height)



ENDTIMER = pygame.USEREVENT+1
def gameLoop():

    gameExit = False
    gameOver = False
    FPS = 15
    click  = 0
    global click

    timed = pygame.time.set_timer(ENDTIMER, 25000)

    while not gameExit:


        gameDisplay.fill(white)


        button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action=None)



        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                pygame.mixer.Sound.play(boop_sound)
                button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action="Click")
                button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action=None)


            elif event.type == ENDTIMER:
                game_over()

        score(click)


        pygame.display.update()

        clock.tick(FPS)

    pygame.quit()
    quit()

game_intro()    
gameLoop()
def gameLoop():
    ENDTIMER = pygame.USEREVENT+1
    gameExit = False
    gameOver = False
    FPS = 15
    click  = 0
    global click

    timer_on =False

    while not gameExit:


        gameDisplay.fill(white)


        button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action=None)



        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                if not timer_on:
                     timed = pygame.time.set_timer(ENDTIMER, 25000)

                pygame.mixer.Sound.play(boop_sound)
                button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action="Click")
                button("CLICK", display_width/2-100,display_height/2-100, 200,200, red, light_red, action=None)


            elif event.type == ENDTIMER:
                gameExit = True



        score(click)


        pygame.display.update()

        clock.tick(FPS)

    game_over()
    pygame.quit()
    quit()