1

I'm running it through the livewires wrapper which is just training wheels for pygame and other python modules in general; but everytime I run it, it'll execute and when I try to exit, it will not respond and then crash.

Any input on how I could go about fixing this would be great. There isn't any input in my textbook and all google seems to yield are results to this problem using pygame itself.

Apparently pygame and Tkinter seem to conflict?

Thanks in advance!

Addendum - This is the code I was trying to run:

from livewires import games

screen_w = 640
screen_h = 480

my_screen = games.Screen (wid, heit)
my_screen.mainloop()
4

2 回答 2

4

类似问题:Pygame 屏幕在我关闭时冻结

我的教科书中没有任何输入,所有谷歌似乎都是使用 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,它会解决你的问题。

于 2011-06-22T20:46:04.620 回答
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()
于 2013-04-07T02:49:50.110 回答