Pygame 时钟和事件循环 [英] Pygame clock and event loops
问题描述
我是 pygame 的新手,我想知道事件循环是什么以及时钟在这种情况下的作用,例如 clock.tick(60)
是什么?网上什么解释都看不懂
clock = pygame.time.Clock()运行 = 真运行时:时钟滴答(60)# 事件循环对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误
tick()
的 pygame.time.Clock
对象,以这种方式延迟游戏,即循环的每次迭代消耗相同的时间段.
这意味着循环:
clock = pygame.time.Clock()运行 = 真运行时:时钟滴答(60)
每秒运行 60 次.
用于 pygame.event.get 中的事件()
处理内部事件并检索外部事件列表(这些事件从内部事件队列中删除).
如果您按下窗口的关闭按钮,则会导致 QUIT
事件,您将通过 for event in pygame.event.get()
获得该事件.请参阅 pygame.event
了解不同的事件类型.例如KEYDOWN
在按下某个键时发生一次.
例如按下 a 键后,以下循环将打印该键的名称:
run = True运行时:# 事件循环对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误如果 event.type == pygame.KEYDOWN:打印(pygame.key.name(事件.key))
i am new to pygame and i was wondering what an event loop is and what clock does in this situation, like what is clock.tick(60)
? I don't understand any explanations online
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
The method tick()
of a pygame.time.Clock
object, delays the game in that way, that every iteration of the loop consumes the same period of time.
That meas that the loop:
clock = pygame.time.Clock() run = True while run: clock.tick(60)
runs 60 times per second.
for event in pygame.event.get()
handles the internal events an retrieves a list of external events (the events are removed from the internal event queue).
If you press the close button of the window, than the causes the QUIT
event and you'll get the event by for event in pygame.event.get()
. See pygame.event
for the different event types. e.g. KEYDOWN
occurs once when a key is pressed.
e.g. The following loop prints the names of the a key once it it pressed:
run = True
while run:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
print(pygame.key.name(event.key))
这篇关于Pygame 时钟和事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!