[英]When I run my Python program, the pygame window opens for a split second then quits
I'm a beginning programmer trying to make a game using python and pygame from an online course. 我是一个刚开始尝试通过在线课程使用python和pygame制作游戏的程序员。 But when I run the following code, the pygame window opens for a split second, then closes.
但是,当我运行以下代码时,pygame窗口将打开一秒钟,然后关闭。
import pygame
pygame.init()
screen = pygame.display.set_mode((900,700))
finished = False
while finished == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finished = True
rectOne = pygame.Rect(0,0,30,30)
color = (0,0,255) #R,G,B
pygame.draw.rect(screen,color,rectOne)
pygame.display.flip()
I don't get any error messages either. 我也没有收到任何错误消息。 It's like it runs my loop once then changes
finished
to True
. 这就像它运行我的循环一次,然后改变
finished
到True
。 I've checked lots of times and my code matches the code of the online course exactly. 我已经检查了很多次,我的代码与在线课程的代码完全匹配。 I'm using a Mac and Python 2.7.13.
我正在使用Mac和Python 2.7.13。 Can anyone help?
有人可以帮忙吗? Update: I'm not sure if this helps, but I can still run the code below and it works perfectly fine.
更新:我不确定这是否有帮助,但是我仍然可以运行下面的代码,并且可以正常运行。 import pygame
导入pygame
pygame.init()
screen = pygame.display.set_mode((900,700))
finished = False
while finished == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finished = True
OK, i have searched about your problem a bit and i came to the conclusion that you need to try the following things: 好的,我对您的问题进行了一些搜索,得出的结论是您需要尝试以下操作:
A) Use pygame.display.update()
instead of pygame.display.flip()
A)使用
pygame.display.update()
代替pygame.display.flip()
B) Try placing these at the end of your program outside the main loop: pygame.QUIT
and on the next line quit()
B)尝试将它们放置在程序的末尾主循环之外:
pygame.QUIT
和下一行quit()
C) If none of my above speculations work, then you need to check if the QUIT event is getting triggered automatically, do this by changing your while
loop as the following: C)如果我的上述推测都不起作用,那么您需要检查QUIT事件是否被自动触发,可以通过如下更改
while
循环来做到这一点:
while not finished:
for event in pygame.event.get():
print(event)
After this when you run your program, in the Shell it will show the events that occur on the pygame screen, if there is a problem then the first and last event would be QUIT and then it is probably the problem of Mac or the version, because i recently read on reddit about it, check it out here 之后,当您运行程序时,它将在Shell中显示pygame屏幕上发生的事件,如果有问题,则第一个和最后一个事件将是QUIT,然后可能是Mac或版本问题,因为我最近在reddit上阅读过有关此内容的信息,请在此处查看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.