通过在Pygame中单击按钮来调用功能 [英] Call function by button click in Pygame
问题描述
在下面的代码中,我在Pygame中得到了一个带有按钮的屏幕.现在,我想单击按钮,然后启动 random()
函数,并在5秒钟后从按钮的开头开始返回屏幕,让我选择再次单击并调用随机函数的选项再次.
I got a screen with buttons in Pygame here in the code below. Now I want to click the button, then a random()
function starts and after 5 seconds it returns to the screen from the beginning with the buttons and let me the option to click again and call a random function again.
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()
推荐答案
添加状态变量( runRandom
),该变量指示是否必须运行函数 random
:
Add a state variable (runRandom
), which indicates if the the function random
has to be run:
runRandom = False
while not done:
# [...]
if runRandom:
random()
添加用户定义的 pygame.event
,可用于计时器:
Add user defined pygame.event
, which can be used for a timer:
runRandomEvent = pygame.USEREVENT + 1
for event in pygame.event.get():
# [...]
elif event.type == runRandomEvent:
# [...]
如果 random
不是正在运行,则允许按下按钮.如果按下按钮,则说明 runRandom
并启动计时器( pygame.time.set_timer()
),并带有确定的时间段(例如5000毫秒= 5秒):
Allow the button to be pressed if random
ins not running. If the button is pressed then state runRandom
and start the timer (pygame.time.set_timer()
) with the decided period of time (e.g. 5000 milliseconds = 5 seconds):
# [...]
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
并停止计时器:
When the time has elapse, the stop running random
by runRandom = False
and stop the timer:
# [...]
elif event.type == runRandomEvent:
runRandom = False
pygame.time.set_timer(runRandomEvent, 0)
以某种方式将建议应用于您的代码:
Apply the suggestion to your code somehow like this:
# 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()
# [...]
这篇关于通过在Pygame中单击按钮来调用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!