如何使用 PyGame 计时器事件?如何使用计时器向 pygame 屏幕添加时钟? [英] How do I use a PyGame timer event? How to add a clock to a pygame screen using a timer?
问题描述
我是 Python 新手,因此决定尝试在 pygame
中制作一个简单的游戏.我想添加一个计时器/时钟来显示你玩过/幸存了多久",所以基本上创建了一个时钟.
但是,我四处搜索并获得了 time.sleep(1)
函数,它确实可以用作时钟,但它使游戏的其他所有内容减慢到几乎没有的程度移动.
有没有一种简单的方法可以在游戏屏幕上添加时钟?
pygame.init()
之后的毫秒数可以通过
导入pygamepygame.init()窗口 = pygame.display.set_mode((200, 200))时钟 = pygame.time.Clock()font = pygame.font.SysFont(None, 100)计数器 = 0text = font.render(str(counter), True, (0, 128, 0))时间延迟 = 1000timer_event = pygame.USEREVENT+1pygame.time.set_timer(timer_event, time_delay)# 主应用程序循环运行 = 真运行时:时钟滴答(60)# 事件循环对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误elif event.type == timer_event:# 重新创建文本计数器 += 1text = font.render(str(counter), True, (0, 128, 0))# 清除显示window.fill((255, 255, 255))# 绘制场景text_rect = text.get_rect(center = window.get_rect().center)window.blit(文本,text_rect)# 更新显示pygame.display.flip()
I'm new to python and therefore decided to try to make a simple game in pygame
. I want to add a timer/clock that shows how long "you have played/survived", so creating a clock basically.
However, I've searched around and gotten the time.sleep(1)
function and it does indeed work as a clock, but it slows down everything else of the game to the point it barely moves.
Is there a simple way to add a clock to the gamescreen?
The number of milliseconds since pygame.init()
can be retrieved by pygame.time.get_ticks()
. See pygame.time
module.
Furthermore in pygame exists a timer event. Use pygame.time.set_timer()
to repeatedly create an USEREVENT
. e.g.:
time_delay = 500 # 0.5 seconds
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event , time_delay )
Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to start at pygame.USEREVENT
. In this case pygame.USEREVENT+1
is the event id for the timer event.
Receive the event in the event loop:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == timer_event:
# [...]
The timer event can be stopped by passing 0 to the time parameter.
See the example:
import pygame
pygame.init()
window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 100)
counter = 0
text = font.render(str(counter), True, (0, 128, 0))
time_delay = 1000
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, time_delay)
# main application loop
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == timer_event:
# recreate text
counter += 1
text = font.render(str(counter), True, (0, 128, 0))
# clear the display
window.fill((255, 255, 255))
# draw the scene
text_rect = text.get_rect(center = window.get_rect().center)
window.blit(text, text_rect)
# update the display
pygame.display.flip()
这篇关于如何使用 PyGame 计时器事件?如何使用计时器向 pygame 屏幕添加时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!