Sheldon
Sheldon

Reputation: 10077

Why is my basic PyGame module so slow?

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.

Upvotes: 3

Views: 2346

Answers (1)

Dhaivat Pandya
Dhaivat Pandya

Reputation: 6536

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.

Upvotes: 3

Related Questions