为什么我的基本 PyGame 模块这么慢? [英] Why is my basic PyGame module so slow?

查看:91
本文介绍了为什么我的基本 PyGame 模块这么慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

操作系统 - Ubuntu硬件 - Macbook Pro

导入pygame导入 pygame.localspygame.mixer.init()屏幕 = pygame.display.set_mode((640, 480))pygame.display.set_caption("bla")背景 = pygame.Surface(screen.get_size())背景 = background.convert()background.fill(pygame.Color("green"))screen.blit(背景, (0, 0))循环 = 真循环时:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:循环=假elif event.type == pygame.KEYDOWN:keyName = pygame.key.name(event.key)打印按下的键:",keyName如果 event.key == pygame.K_SPACE:打印加载音乐"pygame.mixer.music.load("born.mp3")elif event.key == pygame.K_ESCAPE:循环=假pygame.display.flip()

如果我能提供任何进一步的信息,我很乐意提供帮助.

解决方案

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

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

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

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.

解决方案

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.

这篇关于为什么我的基本 PyGame 模块这么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆