我正在通过 livewires 包装器运行它,它通常只是 pygame 和其他 python 模块的训练轮;但每次我运行它时,它都会执行,当我尝试退出时,它不会响应然后崩溃。
任何关于我如何解决这个问题的意见都会很棒。我的教科书中没有任何输入,谷歌似乎产生的所有结果都是使用 pygame 本身解决这个问题的结果。
显然 pygame 和 Tkinter 似乎有冲突?
提前致谢!
附录 - 这是我试图运行的代码:
from livewires import games
screen_w = 640
screen_h = 480
my_screen = games.Screen (wid, heit)
my_screen.mainloop()
最佳答案
类似问题:Pygame screen freezes when I close it
There isn't any input in my textbook and all google seems to yield are results to this problem using pygame itself.
这些结果可能解决了您遇到的相同问题。这是来自 livewires 的 games.py 文件的相关部分,它没有调用 pygame.quit()
:
def handle_events (self):
"""
If you override this method in a subclass of the Screen
class, you can specify how to handle different kinds of
events. However you must handle the quit condition!
"""
events = pygame.event.get ()
for event in events:
if event.type == QUIT:
self.quit ()
elif event.type == KEYDOWN:
self.keypress (event.key)
elif event.type == MOUSEBUTTONUP:
self.mouse_up (event.pos, event.button-1)
elif event.type == MOUSEBUTTONDOWN:
self.mouse_down (event.pos, event.button-1)
def quit (self):
"""
Calling this method will stop the main loop from running and
make the graphics window disappear.
"""
self._exit = 1
def mainloop (self, fps = 50):
"""
Run the pygame main loop. This will animate the objects on the
screen and call their tick methods every tick.
fps -- target frame rate
"""
self._exit = 0
while not self._exit:
self._wait_frame (fps)
for object in self._objects:
if not object._static:
object._erase ()
object._dirty = 1
# Take a copy of the _objects list as it may get changed in place.
for object in self._objects [:]:
if object._tickable: object._tick ()
self.tick ()
if Screen.got_statics:
for object in self._objects:
if not object._static:
for o in object.overlapping_objects ():
if o._static and not o._dirty:
o._erase ()
o._dirty = 1
for object in self._objects:
if object._dirty:
object._draw ()
object._dirty = 0
self._update_display()
self.handle_events()
# Throw away any pending events.
pygame.event.get()
QUIT 事件只是设置一个标志,使您退出 mainloop
函数中的 while 循环。我猜如果你在你的 Python 目录中找到这个文件并在 mainloop
的最后一行之后添加一个 pygame.quit()
,它会解决你的问题。
关于python - 当我从 IDLE 运行它后尝试退出它时,pygame 卡住了!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6441220/