可玩的游戏运行得非常好,绝对没有延迟。然而,我的开始菜单非常慢。是什么原因??相关代码如下:
def message_display(text, fontsize, color, itemx, itemy):
font = pygame.font.Font('freesansbold.ttf', fontsize)
render = font.render(text, True, color)
render_rect = render.get_rect()
render_rect.center = (itemx/2),(itemy/2)
gameDisplay.blit(render, render_rect)
time.sleep(2)
def button(bw, bh, bx, by, msg, col1, col2,fontsi):
mouse = pygame.mouse.get_pos()
if bx < mouse[0] < bx + bw and by < mouse[1] < by + bh:
pygame.draw.rect(gameDisplay, col1, (bx, by, bw, bh))
else:
pygame.draw.rect(gameDisplay, col2, (bx, by, bw, bh))
message_display(msg, fontsi, black, bx*2+bw, by*2+bh)
def game_intro():
intro = True
b1w = 100
b1h = 75
b1x = 150
b1y = 450
b2w = 100
b2h = 75
b2x = 550
b2y = 450
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
message_display("Driving Mayhem", 90, black, display_width, display_height)
button(b1w, b1h, b1x, b1y, "Go!", dark_green, bright_green, 30)
button(b2w, b2h, b2x, b2y, "Quit", dark_red, bright_red, 30)
pygame.display.update()
clock.tick(15)
我的猜测是因为,在 Buttons() 函数中,每次遇到新的鼠标位置时,它都会绘制一个新的矩形。如果是这种情况,我该如何解决?预先感谢您!
最佳答案
这是因为你在 message_display
中调用了 time.sleep(2)
,这会让你的游戏停止 2 秒。
因此,此循环的每个周期至少需要 6 秒,因为 message_display
被调用了 3 次。
只需删除该行即可。
关于python - 开始菜单运行缓慢。是什么原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52865659/