【问题标题】:Call function by button click in Pygame在 Pygame 中通过按钮单击调用函数
【发布时间】:2019-10-03 06:30:12
【问题描述】:

在下面的代码中,我在 Pygame 中得到了一个带有按钮的屏幕。现在我想点击按钮,然后random() 函数启动,5 秒后它从按钮开始返回屏幕,让我可以选择再次点击并再次调用随机函数。

def loop():
    clock = pygame.time.Clock()
    number = 0
    # The button is just a rect.
    button = pygame.Rect(300,300,205,80)
    done = False
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            # This block is executed once for each MOUSEBUTTONDOWN event.
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # 1 is the left mouse button, 2 is middle, 3 is right.
                if event.button == 1:
                    # `event.pos` is the mouse position.
                    if button.collidepoint(event.pos):
                        # Incremt the number.
                        number += 1

random()
loop()
pygame.quit()

【问题讨论】:

    标签: python button pygame


    【解决方案1】:

    添加一个状态变量(runRandom),表示函数random是否必须运行:

    runRandom = False
    while not done:
    
        # [...]
    
        if runRandom:
            random()
    

    添加用户定义的pygame.event,可用于定时器:

    runRandomEvent = pygame.USEREVENT + 1
    
    for event in pygame.event.get():
    
       # [...]
    
       elif event.type == runRandomEvent:
          # [...]
    

    如果random 没有运行,则允许按下按钮。如果按下按钮,则状态 runRandom 并以确定的时间段(例如 5000 毫秒 = 5 秒)启动计时器 (pygame.time.set_timer()):

    # [...]
    
    elif event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == 1:
            if button.collidepoint(event.pos) and not runRandom:
                # [...]
    
                runRandom = True
                pygame.time.set_timer(runRandomEvent, 5000)
    

    当时间过去后,通过runRandom = False停止运行random并停止计时器:

    # [...]
    
    elif event.type == runRandomEvent:
        runRandom = False
        pygame.time.set_timer(runRandomEvent, 0)
    

    以某种方式将建议应用于您的代码:

    # define user event for the timer
    runRandomEvent = pygame.USEREVENT + 1
    
    def loop():
        clock = pygame.time.Clock()
        number = 0
        # The button is just a rect.
        button = pygame.Rect(300,300,205,80)
        done = False
        runRandom = False
        while not done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True
                # This block is executed once for each MOUSEBUTTONDOWN event.
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    # 1 is the left mouse button, 2 is middle, 3 is right.
                    if event.button == 1:
                        # `event.pos` is the mouse position and  "random" is not running
                        if button.collidepoint(event.pos) and not runRandom:
                            # Incremt the number.
                            number += 1
    
                            # Start timer and enable running "random"
                            runRandom = True
                            pygame.time.set_timer(runRandomEvent, 5000) # 5000 milliseconds
    
                elif event.type == runRandomEvent:
                    runRandom = False
                    pygame.time.set_timer(runRandomEvent, 0)
    
            # [...]
    
            # run "random"
            if runRandom:
                random()
    
            # [...]  
    

    【讨论】:

      猜你喜欢
      • 2017-10-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 2022-11-14
      • 2015-08-20
      • 2016-09-24
      • 2016-09-16
      相关资源
      最近更新 更多