简体   繁体   English

当我运行Python程序时,pygame窗口打开片刻,然后退出

[英]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 . 这就像它运行我的循环一次,然后改变finishedTrue 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.

相关问题 每当我尝试使用 pygame 运行我的 python 代码时,window 打开,然后在一秒钟或任何输入后停止响应 - Whenever i try run my python code with pygame the window opens and then stops responding after a second or any input Pygame 窗口不显示任何内容,当我退出程序时,它会显示我的程序一瞬间然后关闭 - Pygame window does not displays anything, when i exit the program, it displays my program for a split second and then closes 为什么我在尝试运行 Pygame 时不断收到“Python 意外退出”和错误? - Why do I keep getting “Python quits unexpectedly” and errors when trying to run Pygame? pygame项目在IDE中运行,我将其转换为.exe并运行后,游戏窗口打开半秒才关闭 - pygame project works in IDE, after I converted it to .exe and run it, the game window opens for half a second before closing Pygame 已安装,但是当我尝试运行我的程序时,window 不会打开 - Pygame is installed, but when I try to run my program the window won't open 当我用 python 运行这个 selenium 代码时,谷歌打开一秒钟然后关闭 - When I run this selenium code with python, google opens for a second and then closes pygame 立即退出 window - pygame instantly quits window 是否可以让我的程序用 pygame 绘制一个形状,稍等片刻,然后再绘制另一个? 但是,我仍然希望程序的其余部分运行 - Is it possible to have my program draw a shape with pygame, wait a second, and then draw another? But, I still want the rest of the program to run 当我运行我的代码时,它会打开窗口但不显示图像 - When I run my code, it opens the window but doesn't show the image 当我在KIVY中运行程序时,如何更改窗口上的图标 - How to change the icon on the window when I run my program in KIVY
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM