Python &引用;口吃;Pygame的开始菜单

Python &引用;口吃;Pygame的开始菜单,python,pygame,pygame-surface,Python,Pygame,Pygame Surface,几年来,我一直在断断续续地开发一款基于文本的冒险游戏。它最初是作为学习Python的一种方式开始的,后来在我熟悉Python语言后,我转向了与我的职业生涯更相关的项目。我现在有点能干(仍然是一个noob,但你知道,进步),我想回到游戏中添加更复杂的功能 让我恼火的一件事是我的“开始”菜单中有一个视觉错误。代码如下: import pygame from pygame_functions import setBackgroundImage import gamefile pygame.init(

几年来,我一直在断断续续地开发一款基于文本的冒险游戏。它最初是作为学习Python的一种方式开始的,后来在我熟悉Python语言后,我转向了与我的职业生涯更相关的项目。我现在有点能干(仍然是一个noob,但你知道,进步),我想回到游戏中添加更复杂的功能

让我恼火的一件事是我的“开始”菜单中有一个视觉错误。代码如下:

import pygame
from pygame_functions import setBackgroundImage
import gamefile

pygame.init()

display_width = 1500
display_height = 750
startMenu = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("The Woodsman's Tale")


black = (0, 0, 0)
green = (0, 200, 0)
white = (255, 255, 255)
dark_red = (200, 0, 0)
bright_green = (0, 255, 0)
leaf_green = (0, 175, 75)
brown = (102, 51, 0)
red = (255, 0, 0)


clock = pygame.time.Clock()


def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()


def button(msg, x, y, w, h, ic, ac, action=None):

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(startMenu, ac, (x, y, w, h))
        if click[0] == 1 and action is not None:
            if action == 'play':
                gamefile.rungame()
                start_menu().intro = False
            elif action == 'quit':
                pygame.quit()
                quit()
    else:
        pygame.draw.rect(startMenu, ic, (x, y, w, h))

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    TextSurf, TextRect = text_objects(msg, smallText)
    TextRect.center = (x + (w / 2), (y + (h / 2)))
    startMenu.blit(TextSurf, TextRect)


def start_menu():

    intro = True

    while intro:
        setBackgroundImage('startScreen.png')
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        largeText = pygame.font.Font('BRADHITC.ttf', 90)
        TextSurf, TextRect = text_objects("The Woodsman's Tale", largeText)
        TextRect.center = (display_width / 2, display_height / 3)
        startMenu.blit(TextSurf, TextRect)

        button('Play', 350, 500, 200, 150, green, leaf_green, 'play')
        button('Quit', 850, 500, 200, 150, dark_red, red, 'quit')

        pygame.display.update()
        clock.tick(15)

if __name__ == '__main__':
    start_menu()
正在导入的setbackgroundimage函数如下所示:

def setBackgroundImage(img):
    global bgSurface, backgroundImage
    surf = loadImage(img)
    backgroundImage = surf
    screen.blit(surf, [0, 0])
    bgSurface = screen.copy()
    updateDisplay()
现在,开始菜单工作得很好。一切正常。我的问题是,当我点击标题栏,例如移动窗口,开始菜单开始在视觉上口吃

确切的情况是:一旦我点击标题栏,开始菜单的“按钮”和游戏的标题文本就会消失。一旦点击被释放,按钮和标题文本就会重新出现,但会很快结巴


我真的不确定我是否提供了足够的信息让任何人都能知道出了什么问题,所以如果是这样,我很抱歉。

第一个问题是
setBackgroundImage
似乎在更新显示(
updateDisplay()
)。
显示必须在主应用程序循环结束时更新一次,不应更新多次。这会导致闪烁。从
setBackgroundImage
中删除显示更新:

def start_menu():

    intro = True

    while intro:
        setBackgroundImage('startScreen.png') # draw background but do no update the display

        # [...]

        pygame.display.update() # the one and only update at the end of the loop
        clock.tick(15)

第二个问题,背景图像加载到
setBackgroundImage
中。这导致图像在每一帧中连续加载。这会对性能产生影响。
在主循环之前加载图像,并将
曲面
对象传递到
setBackgroundImage

def start_菜单():
surf=loadImage('startScreen.png')
简介=正确
而简介:
setBackgroundImage(surf)
# [...]