为什么我的基本 PyGame 模块这么慢?

发布于 2024-11-10 07:22:46 字数 1091 浏览 1 评论 0原文

我计划在 Pygame 中编写代码,但我刚刚开始学习基础知识,发现执行代码非常慢。当我按下一个键时,它需要一段时间才能在终端中打印它(似乎没有任何模式)。

我正在运行Python 2.6,遇到这个问题后我降级了。经过进一步测试,我发现整个系统速度变慢。有没有人遇到过这个问题或找到了解决方案,使其运行得更快或/并防止系统变慢?

操作系统-Ubuntu 硬件 - Macbook Pro

import pygame
import pygame.locals
pygame.mixer.init()

screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("bla")

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(pygame.Color("green"))
screen.blit(background, (0, 0))

looping = True
while looping:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            looping = False
        elif event.type == pygame.KEYDOWN:
            keyName = pygame.key.name(event.key)
            print "key pressed:", keyName

            if event.key == pygame.K_SPACE:
                print "Loading Music"
                pygame.mixer.music.load("born.mp3")

            elif event.key == pygame.K_ESCAPE:
                looping = False

    pygame.display.flip()

如果我可以提供任何进一步的信息,我很乐意提供帮助。

I've planning on writing a code in Pygame and I was just getting started with the basics and found that the executing code was really slow. When I press a key it takes a while for it to print it in the terminal (there doesn't seem to be any pattern to it).

I'm running Python 2.6, I downgraded after coming across this problem. With further testing I've found that the whole system slows down. Has anyone come across this or got a solution so it runs faster or/and prevents the system from slowing down?

OS - Ubuntu
Hardware - Macbook Pro

import pygame
import pygame.locals
pygame.mixer.init()

screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("bla")

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(pygame.Color("green"))
screen.blit(background, (0, 0))

looping = True
while looping:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            looping = False
        elif event.type == pygame.KEYDOWN:
            keyName = pygame.key.name(event.key)
            print "key pressed:", keyName

            if event.key == pygame.K_SPACE:
                print "Loading Music"
                pygame.mixer.music.load("born.mp3")

            elif event.key == pygame.K_ESCAPE:
                looping = False

    pygame.display.flip()

If there's any further information I can provide I would be happy to help.

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

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

发布评论

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

评论(1

悍妇囚夫 2024-11-17 07:22:46

pyGame 基于 SDL,SDL 内部基于线程。

当你有线程时,打印消息基本上是禁忌。因为很多时候由于调度程序切片(SDL 中很大),打印消息会延迟。这并不是说 pygame 很慢(在某些情况下是这样,但不是在这种情况下),只是 print 语句位于单独的事件线程中。

尝试在 pygame 中执行这个,它会运行得很好。

pyGame is based on SDL which is internally based on threads.

When you have threading, print messages are basically a no-no. Because often times because of the scheduler slices (which are large in SDL), the print messages get delayed. Its not that pygame is slow (it is some situations, but, not in this one), its just that the print statement is in a seperate event thread.

Try doing this in pygame, it'll run pretty well.

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