Pygame - “错误:显示 Surface 退出" [英] Pygame - "error: display Surface quit"
问题描述
我制作了一个菜单屏幕,点击按钮会在同一窗口中打开不同的屏幕.
def main():导入 pygame,随机,时间pygame.init()大小=[800, 600]屏幕=pygame.display.set_mode(大小)pygame.display.set_caption("游戏")完成=假时钟=pygame.time.Clock()完成时==假:对于 pygame.event.get() 中的事件:pos = pygame.mouse.get_pos()如果 event.type == pygame.QUIT:完成=真休息如果 button_VIEW.collidepoint(pos):如果 event.type == pygame.MOUSEBUTTONDOWN:打印(查看.")看法()休息屏幕填充(黑色)...定义视图():完成=假时钟=pygame.time.Clock()完成时==假:对于 pygame.event.get() 中的事件:pos = pygame.mouse.get_pos()如果 event.type == pygame.QUIT:完成=真休息...
如果可能,我想知道如何避免错误:
screen.fill(black)错误:显示 Surface 退出>>>
查看此处的其他问题后,我尝试将 break
s 添加到任何循环的出口,但仍然发生错误.
我理解问题是程序试图在窗口关闭后执行 screen.fill(black)
,但我没有关于如何防止错误的进一步想法.>
感谢您的帮助.对不起,如果它看起来很简单.
几种可能性:
- 在
view
函数中结束进程(例如使用sys.exit()
).不理想. - 从
view
函数返回一个值来指示应用程序应该结束(例如return done
),并在main<中检查该返回值/code> 函数(
如果完成:返回
).更好. - 使
done
成为全局的,并在main
函数中检查它的值.我真的不喜欢这个解决方案. - 我的最爱:完全避免多个事件循环,这样问题就会自行解决(这样你就可以例如退出
main
函数并返回).
I've made a menu screen where clicking on a button leads to a different screen in the same window.
def main():
import pygame, random, time
pygame.init()
size=[800, 600]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Game")
done=False
clock=pygame.time.Clock()
while done==False:
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
done=True
break
if button_VIEW.collidepoint(pos):
if event.type == pygame.MOUSEBUTTONDOWN:
print("VIEW.")
view()
break
screen.fill(black)
...
def view():
done=False
clock=pygame.time.Clock()
while done==False:
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
done=True
break
...
If possible, I'd like to know how I can avoid the error:
screen.fill(black)
error: display Surface quit
>>>
After looking at other questions on here, I tried adding break
s to the exits of any loops, but still the error occurs.
I understand the issue is that the program is trying to execute screen.fill(black)
after the window has been closed, but I have no further ideas on how to prevent the error.
I appreciate any help. Sorry if it seems simple.
Several possibilities:
- end the process (with e.g.
sys.exit()
) in theview
function. Not ideal. - return a value from the
view
function to indicate that the application shoud end (e.g.return done
), and check for that return value in themain
function (if done: return
). Better. - make
done
global and check for its value in themain
function. I really would not like this solution. - my favourite: avoid multiple event loops altogether, so the problem solves itself (so you could just e.g. exit the
main
function with return).
这篇关于Pygame - “错误:显示 Surface 退出"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!