当我从 IDLE 运行后尝试退出时,pygame 冻结了!

发布于 2024-11-16 10:23:33 字数 379 浏览 3 评论 0原文

我通过 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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦上情书 2024-11-23 10:23:33

类似的问题: 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 事件只是设置一个标志,使您脱离 中的 while 循环代码>主循环函数。我猜测,如果您在 Python 目录中找到此文件,并在 mainloop 的最后一行后面添加 pygame.quit(),它就会解决您的问题。

Similar question: 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.

Those results probably address the same problem you're having. This is the relevant part of the games.py file from livewires, and nowhere does it call 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()

The QUIT event just sets a flag which drops you out of the while loop in the mainloop function. I'm guessing that if you find this file in your Python directory and stick a pygame.quit() after the last line in mainloop, it will solve your problem.

风流物 2024-11-23 10:23:33

我同意。你需要将整个程序(要执行的部分,而不是定义等)放入 while 循环中。问题是,在 IDLE 中退出时,pygame 不会被告知关闭,但在 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()

I agree. you need to put the whole program (the part to be executed, not the definitions and such) into a while loop.The problem is that pygame is not told to close when exited in IDLE, but outside of IDLE the closure of the program overrides the need to close pygame.

here is the loop:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文