pygame时钟和事件循环 [英] Pygame clock and event loops
问题描述
我是pygame的新手,我想知道事件循环是什么,在这种情况下时钟是什么,例如 clock.tick(60)
是什么?我不明白网上的任何解释
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
推荐答案
方法 tick()
.Clock" rel ="nofollow noreferrer"> pygame.time.Clock
对象以这种方式延迟游戏,循环的每次迭代都消耗相同的时间.
相当于循环:
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)
每秒运行60次.
用于pygame.event.get中的事件()
处理内部事件,并检索外部事件列表(这些事件已从内部事件队列中删除).
如果您按下窗口的关闭按钮,则会导致 QUIT
事件,然后通过pygame.event.get()中的 for事件获取该事件.有关不同事件类型,请参见
pygame.event
.例如按下键时, KEYDOWN
会发生一次.
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.
例如按下 a 键后,以下循环将打印该键的名称:
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屋!