在循环内打开和关闭 pygame 窗口,不显示"backend terminated"消息



我正在为学校做一个程序,我需要使用pygame窗口来显示我的程序的累计总数(这是为了订购咖啡)。下面是窗口打开部分的代码:

if totalconf == 'y':
# if user wishes to view cumulative totals
pygame.init()
background_colour = (231,247,146)
(width, height) = (1500, 900)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Running Totals')
# initialise new window with yellow background and title
myFont = pygame.font.SysFont("arial", 40)
myFont2 = pygame.font.SysFont("arial", 20)
# initialise small and large fonts
text = myFont.render("In store totals:", True, (0, 0, 0))
text3 = myFont.render("Takeaway totals:", True, (0, 0, 0))
text4 = myFont.render("In store earnings:", True, (0, 0, 0))
text5 = myFont.render("Takeaway earnings:", True, (0, 0, 0))
text6 = myFont.render(f"Total discounts: ${total_discounts}", True, (0, 0, 0))
text7 = myFont.render(f"Total gst: ${total_gst}", True, (0, 0, 0))
text8 = myFont.render(f"Total surcharges: ${total_takeawaycharges}", True, (0, 0, 0))
# initialise headings and totals to display on screen
screen.fill(background_colour)
# fill screen with background color
pygame.display.flip()
# update screen
running = True
# run window
while running:
# while window is running
screen.blit(text, (20, 20))
screen.blit(text3, (300, 20))
screen.blit(text4, (600, 20))
screen.blit(text5, (900, 20))
screen.blit(text6, (20, 700))
screen.blit(text7, (20, 740))
screen.blit(text8, (20, 780))
# project all text onto screen
pygame.display.update()
# update screen
initial = 70
# set initial as 70 down 
for item, values in totalsdict.items():
# for values stored in totals dict
text2 = myFont2.render(f'{item.title()}: {values[0]}', True, (0,0,0))
# print out item and equivalent total in small font
screen.blit(text2, (20, initial))
# display text on screen
pygame.display.update()
# update screen
initial += 40
# increase inital by 40 so values print vertically down
initial = 70
# set initial as 70 down 
for item, values in totalsdict.items():
# for values stored in totals dict
text2 = myFont2.render(f'{item.title()}: {values[1]}', True, (0,0,0))
# print out item and equivalent total in small font
screen.blit(text2, (300, initial))
# display text on screen
pygame.display.update()
# update screen
initial += 40
# increase inital by 40 so values print vertically down
initial = 70
# set initial as 70 down 
for item, values in totalsdict.items():
# for values stored in totals dict
text2 = myFont2.render(f'{item.title()}: {values[2]}', True, (0,0,0))
# print out item and equivalent total in small font
screen.blit(text2, (600, initial))
# display text on screen
pygame.display.update()
# update screen
initial += 40
# increase inital by 40 so values print vertically down
initial = 70
# set initial as 70 down 
for item, values in totalsdict.items():
# for values stored in totals dict
text2 = myFont2.render(f'{item.title()}: {values[3]}', True, (0,0,0))
# print out item and equivalent total in small font
screen.blit(text2, (900, initial))
# display text on screen
pygame.display.update()
# update screen
initial += 40
# increase inital by 40 so values print vertically down
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if running == False:
pygame.quit()
# code to stop running if 'X' is pressed

如果用户想要查看累计总数,他们在totalconf中输入'y'。这段代码位于连续的while循环中,该循环执行命令,直到输入空白。程序第一次运行,这工作得很好,打开和关闭,而不停止程序。然而,第二次,如果我希望看到更新的累积总数并打开pygame窗口,我得到一条消息,说&;Backend terminated or disconnected。Windows致命异常:访问违规。这是一个很长的节目,但我希望我给的信息是足够的。

所以总的来说,我需要能够在每次循环运行时打开和关闭窗口,而不会收到上面的消息。

使用键盘事件:

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
running = False

键盘事件(参见pygame。事件模块)仅在键的状态更改时发生一次。每次按下一个键时,KEYDOWN事件发生一次。每次释放一个键时,KEYUP发生一次。将键盘事件用于单个操作。

最新更新