首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游戏中的“结巴”StartMenu

游戏中的“结巴”StartMenu
EN

Stack Overflow用户
提问于 2020-02-07 18:45:17
回答 1查看 59关注 0票数 1

几年来,我一直在制作一款基于文本的冒险游戏。它最初是作为一种学习Python的方式开始的,之后我开始从事与我的职业生涯更相关的项目,因为我已经习惯了这种语言。我现在有点能力(仍然是个菜鸟,但你知道,进步),我想回到游戏中,加入更复杂的功能。

有一件事让我很恼火,那就是我的“开始”菜单中出现了一个视觉缺陷。以下是代码:

代码语言:javascript
复制
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函数如下:

代码语言:javascript
复制
def setBackgroundImage(img):
    global bgSurface, backgroundImage
    surf = loadImage(img)
    backgroundImage = surf
    screen.blit(surf, [0, 0])
    bgSurface = screen.copy()
    updateDisplay()

现在,开始菜单很好用。一切正常运转。我的问题是,当我点击标题栏,例如,移动窗口,开始菜单开始口吃视觉。

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

我不太确定我是否提供了足够的钱让任何人能够判断出出了什么问题,所以如果是这样的话,我很抱歉。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-07 20:01:36

第一个问题是setBackgroundImage似乎更新了显示(updateDisplay())。

显示必须在主应用程序循环结束时更新一次,不应多次更新。这会引起闪烁。从setBackgroundImage中删除显示更新:

代码语言:javascript
复制
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中加载的。这将导致图像在每个帧中连续加载。这会对性能产生影响。

在主循环之前加载映像,并将Surface对象传递给setBackgroundImage

代码语言:javascript
复制
def start_menu():

    surf = loadImage('startScreen.png')

    intro = True
    while intro:
        setBackgroundImage(surf)

        # [...]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60119356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档