[英]Can you have two functions with separate FPS values at the same time?
I'm working on a game that shows Python drawn images on the screen and occasionally the user needs to click and drag an item from an inventory to have some collision on the screen.我正在开发一个在屏幕上显示 Python 绘制图像的游戏,偶尔用户需要单击并从库存中拖动一个项目才能在屏幕上产生一些碰撞。
The main function of the game has: clock.tick(60)
.游戏的主要功能有:
clock.tick(60)
。 I created a function that shows noise (like when a TV doesn't have reception) and at 60 FPS, it runs way too fast.我创建了一个显示噪音的功能(比如当电视没有接收时),并且在 60 FPS 时,它运行得太快了。 If I add a sleep, wait, delay, etc., it looks great, but, then dragging an item on the screen has a lot of lag.
如果我添加睡眠、等待、延迟等,它看起来很棒,但是,在屏幕上拖动一个项目会有很多滞后。 I would prefer to not have the items lag at all, so I need to know how to slow down the noise function.
我宁愿完全没有项目滞后,所以我需要知道如何减慢噪音功能。
def whitespace(surface, rect):
pixel_size = 4
pixel_length = rect.h / pixel_size
pixel_height = rect.w / pixel_size
start = rect.x
pixel_grid = [[1]*int(pixel_height) for n in range(int(pixel_length))]
colors = [(255, 255, 255), (205, 205, 205), (155, 155, 155), (100, 100, 100)]
for row in pixel_grid:
for col in row:
color = random.randint(0, 3)
surface.fill(colors[color], ((rect.x, rect.y), (pixel_size, pixel_size)))
rect.x += pixel_size
rect.y += pixel_size
rect.x = start
Create a function that can generate the a "whitespace" surface.创建一个可以生成“空白”表面的函数。 The generated surface is returned from the function:
生成的表面从函数返回:
def create_whitespace(rect):
surface = pygame.Surface(rect.size)
pixel_size = 4
pixel_length = rect.h / pixel_size
pixel_height = rect.w / pixel_size
start = rect.x
pixel_grid = [[1]*int(pixel_height) for n in range(int(pixel_length))]
colors = [(255, 255, 255), (205, 205, 205), (155, 155, 155), (100, 100, 100)]
for row in pixel_grid:
for col in row:
color = random.randint(0, 3)
surface.fill(colors[color], (0, 0, pixel_size, pixel_size))
rect.x += pixel_size
rect.y += pixel_size
rect.x = start
return surface
Create another function which blit
s the surface the the window:创建另一个函数,它
blit
s 窗口的表面:
def draw_whitespace(surface, ws_surf, rect):
surface.blit(ws_surf, rect)
Blit the surface to the window in every frame, but generate a new random "whitespace" surface less often.在每一帧中将表面 Blit 到窗口,但较少生成新的随机“空白”表面。 That causes that the same "whitespace" is draw for multiple frames:
这会导致为多个帧绘制相同的“空白”:
ws_cnt = 0
while True:
# [...]
if ws_cnt == 0:
ws_surf = create_whitespace(rect)
ws_cnt += 1
if ws_cnt == 5: # 5 is just an example
ws_cnt = 0
draw_whitespace(screen, ws_surf, rect)
# [...]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.