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

2024-05-16 07:55:10 发布

您现在位置:Python中文网/ 问答频道 /正文

可玩的游戏运行非常好,绝对没有滞后。然而,我的开始菜单是难以置信的慢。原因是什么??以下是相关代码:

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()函数中,每次遇到新的鼠标位置时,它都会绘制一个新的矩形。如果是这样的话,我该怎么解决呢?提前谢谢


Tags: rectmessagegetbydefdisplaybuttonrender