Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

By profiling my game, I see that the vast majority of the execution time of my hobby game is between the blit and the flip calls. Currently, it's only running at around 13fps. My video card is fairly decent, so my guess is that pygame is not using it.

Does anyone know of any graphics/display options I need to set in pygame to make this faster? Or is this just something that I have to live with since I've chosen pygame?

share|improve this question
add comment

6 Answers

up vote 11 down vote accepted

If you're running in full-screen mode, you can use the pygame.HWSURFACE flag when you initialise the display to tell pygame to try to use a hardware surface. I believe that if a hardware surface can't be used, pygame will silently use a software surface.

If you're not using a hardware surface, consider using pygame.display.update(rectangle) rather than pygame.display.flip(). This will only update the given rectangle rather than the whole display, which can increase your performance quite a bit unless your game actually needs to update the whole display.

If you are using a hardware or OpenGL surface, there's also the pygame.DOUBLEBUF which uses hardware double buffering, meaning that your drawing functions will draw to one of the two buffers and flip() will swap which buffer is visible.

share|improve this answer
add comment

One bottleneck in pygame is actually font rendering. You should not render the same text (with the same font/color) more than once but rather cache the surfaces once rendered and reuse them. Take this into acount if you're using Font.render() alot.

share|improve this answer
add comment

In addition to talljosh's answer, I'd like to throw in a few more suggestions:

Avoid rotations and scaling wherever you possibly can. Such operations are done purely on the CPU and tend to be very, very slow.

If you're doing direct pixel reads/modifications, use the Surface.get_buffer() method and work with that, rather than using the Surface.set_at()/get_at() methods.

share|improve this answer
add comment

Comment out any test console output you have. This can drastically reduce framerate, especially if you are outputting between rotation and scaling, as Cody said above.

share|improve this answer
add comment

One incredibly simple thing to do is use psyco. I don't know how much it will help, but since all it requires is

 import psyco
 psyco.full()

to start running, you may as well give it a try.

share|improve this answer
    
Psyco really won't help much with modern idiomatic Python code. Use PyPy if you want a JIT (not sure if PyGame is working there though). –  coderanger Mar 20 '11 at 17:40
    
Pygame doesn't work under PyPy, but Pyglet does so there you go. –  coderanger Mar 21 '11 at 22:01
add comment

It's correct that Pygame does not use the 3D acceleration capabilities of your video card. This is because it is based on SDL which has the same limitation. One alternative is to rewrite your game in a system like pyglet which does natively support OpenGL accelerated sprites, to find a similar library for graphics only, or to write the OpenGL code yourself for this.

share|improve this answer
    
Pyglet is great. –  The Communist Duck Mar 20 '11 at 12:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.