我正在通过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()
答案 0 :(得分:4)
类似的问题:Pygame screen freezes when I close it
我的教科书中没有任何输入 所有谷歌似乎屈服于 使用pygame导致这个问题 本身。
这些结果可能解决了您遇到的同样问题。这是来自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目录中找到这个文件并在pygame.quit()
的最后一行之后粘贴mainloop
,它将解决你的问题。
答案 1 :(得分:0)
我同意。你需要将整个程序(要执行的部分,而不是定义等)放入while循环。问题是pygame在IDLE退出时不会被告知关闭,但在IDLE之外,程序的关闭会覆盖需要关闭pygame。
这是循环:
done = False
while done==False:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
#Main program here
pygame.quit()