Why is my basic PyGame module so slow?

半世苍凉 提交于 2019-12-23 19:14:20

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6156485/why-is-my-basic-pygame-module-so-slow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!