如何在Ipython / Python解释器的背景中运行Pygame?

发布于 2025-01-19 16:14:02 字数 249 浏览 0 评论 0 原文

我想在背景线程中从Ipython设置Pygame应用程序。

在将其他任何地方导入该线程之前,我已经尝试从该线程导入pygame。

问题是,当我在此线程内启动主循环时,很快就会冷冻,直到我按Enter在解释器中为止。

我怎么能始终保持它的运行?

注意:当启动仅打印东西的虚拟线程时,它不会被阻止,因此我认为它与Pygame更相关,而不是与Ipython或Python解释器。同样的行为出现在ipython和常规python解释器中

I would like to setup a pygame app from ipython in a background thread.

I already tried to import pygame from this thread before importing it anywhere else.

The problem is that when I launch my main loop inside this thread, soon, it gets frozen until I press enter in the interpreter.

How could I always keep it running ?

Note : that when starting a dummy thread that just print stuffs, it doesn't get blocked, so I think it's more related to pygame than to ipython or python interpreter. The same behaviour appears in IPython and regular python interpreter

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

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

发布评论

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

评论(1

薆情海 2025-01-26 16:14:02

我刚刚为我自己的项目构建了一个示例!

我不确定是什么导致你的冻结,尽管我知道操作系统一直试图强制它关闭,直到我开始实际处理事件队列中的事件 - 显然,当队列填满时,操作系统认为我的线程已损坏。

这是下面的基本想法,尽管您可以找到我的稍微更详细的示例,其中包含更多注释和要使用的示例实例,在 GitHub 上

import threading
import pygame


class ThreadedRenderer(threading.Thread):

    def __init__(self, instances=[], size=(128, 128), fps=30, event_functions={}):

        threading.Thread.__init__(self) # Run the Thread parent init

        # Set up program structure
        self.instances = instances
        self.fps = fps
        self.events = event_functions
        self.running = True

        # Set up Pygame
        pygame.init()
        self.screen = pygame.display.set_mode(size)
        self.clock = pygame.time.Clock()

        # Start the thread, which starts the loop
        self.start()

    def _step(self):
        """ Update the program objects, called each frame."""
        for inst in self.instances:
            inst.update()

    def _draw(self):
        """ Redraw the screen, called each frame."""
        self.screen.fill((0, 0, 0))
        for inst in self.instances:
            inst.draw(self.screen)
        pygame.display.flip()

    def run(self):
        """ The function that is Threaded. DO NOT call this function."""
        while self.running is True:
            self.clock.tick(self.fps)
            for event in pygame.event.get():
                if event.type in self.events:
                    self.events[event.type](event)
                elif event.type == pygame.QUIT: # May be overridden
                    self.running = False
            self._step()
            self._draw()
        # Below will be executed whenever the thread quits gracefully or kill is called
        pygame.quit()
        ThreadedRenderer.instantiated = False

    def kill(self):
        self.running = False

I just built an example of this for my own project!

I'm not sure what caused yours to freeze, though I know for mine the OS kept trying to force it to close until I started actually processing the events from the event queue - apparently when the queue filled up the OS thought my thread was broken.

Here's the basic idea below, though you can find my slightly more detailed example, with more comments and with example instances to use, Here on GitHub.

import threading
import pygame


class ThreadedRenderer(threading.Thread):

    def __init__(self, instances=[], size=(128, 128), fps=30, event_functions={}):

        threading.Thread.__init__(self) # Run the Thread parent init

        # Set up program structure
        self.instances = instances
        self.fps = fps
        self.events = event_functions
        self.running = True

        # Set up Pygame
        pygame.init()
        self.screen = pygame.display.set_mode(size)
        self.clock = pygame.time.Clock()

        # Start the thread, which starts the loop
        self.start()

    def _step(self):
        """ Update the program objects, called each frame."""
        for inst in self.instances:
            inst.update()

    def _draw(self):
        """ Redraw the screen, called each frame."""
        self.screen.fill((0, 0, 0))
        for inst in self.instances:
            inst.draw(self.screen)
        pygame.display.flip()

    def run(self):
        """ The function that is Threaded. DO NOT call this function."""
        while self.running is True:
            self.clock.tick(self.fps)
            for event in pygame.event.get():
                if event.type in self.events:
                    self.events[event.type](event)
                elif event.type == pygame.QUIT: # May be overridden
                    self.running = False
            self._step()
            self._draw()
        # Below will be executed whenever the thread quits gracefully or kill is called
        pygame.quit()
        ThreadedRenderer.instantiated = False

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