python - 在没有“后端终止”消息的情况下在循环中打开和关闭 pygame 窗口
问题描述
我正在为学校开发一个程序,并且需要使用 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
如果用户想查看累计总数,他们输入“y”作为 totalconf。这段代码位于一个连续的 while 循环中,该循环接受命令,直到输入一个空白。程序第一次运行时效果很好,可以在不停止程序的情况下打开和关闭。但是,第二次,如果我希望查看更新的累积总数并打开 pygame 窗口,我会收到一条消息,上面写着“后端终止或断开连接。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.event模块)仅在按键状态更改时发生一次。KEYDOWN
每次按下某个键时,该事件发生一次。KEYUP
每次释放键时发生一次。将键盘事件用于单个操作。
推荐阅读
- objective-c - 如何将现有的带有 View 的 Swift 模块桥接到 React-Native?
- c++ - 构建独立发布项目 CryEngine V
- java - 替代已弃用的 WebMvcConfigurerAdapter?
- c# - C# ASP.NET MVC - 将 XML 下载到浏览器
- outlook - 当我尝试按 ID 检索附件时,我收到 ErrorItemNotFound 响应
- python - 尝试使用 python 3.7 (x64) 连接到 pyqt5 中的 SQL 数据库时出现“未加载驱动程序”
- php - 跨 DST 边界添加时,PHP DateTime 无法正确计算时间分量
- android - 如何修复在某些手机上放错位置的矩形可绘制对象
- google-apps-script - 我如何制作一个脚本,在打开时(作为编辑器)向自己发送电子邮件?(谷歌文档)
- javascript - if 语句未按预期检查值