gpt4 book ai didi

python - "Stuttering"Pygame 的开始菜单

转载 作者:行者123 更新时间:2023-12-01 06:26:26 25 4
gpt4 key购买 nike

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

令我烦恼的一件事是我的开始菜单中的视觉错误。下面是代码:

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中加载的。这导致图像在每一帧中连续加载。这会导致性能影响。
在主循环之前加载图像并将 Surface 对象传递给 setBackgroundImage:

def start_menu():

surf = loadImage('startScreen.png')

intro = True
while intro:
setBackgroundImage(surf)

# [...]

关于python - "Stuttering"Pygame 的开始菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60119356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com