python - 开始菜单运行缓慢。是什么原因?

标签 python python-3.x performance pygame

可玩的游戏运行得非常好,绝对没有延迟。然而,我的开始菜单非常慢。是什么原因??相关代码如下:

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/

相关文章:

python - “QCombination”对象不可迭代

python - 使docker容器通过端口进行通信

python - 如何在 Python 中正确声明 ctype 结构 + 联合?

python - 如何将输出数据排序为列和行

python - 强制对可迭代对象进行所有迭代

database - 雪花比索引更好?

c++ - 在 Rcpp 中为具有重复迭代的模型正确设置 GSL RNG 种子

python - Django 休息框架 : Permissions based on Object Attributes/Ownership

regex - 查找以具有特定模式的下划线开头的单词的换行符

mysql - 多少查询太多了?