Advertisement Jump to content
Sign in to follow this  
Kylotan

Recommendation: pyglet library for Python

This topic is 3981 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been looking for an alternative to PyGame for some time, and finally I can say I've found it. The problems with PyGame are two-fold: first, that it's poorly maintained, and second, that it relies on SDL which - although well tried and tested - is also poorly maintained, and getting quite long in the tooth as a result. Today I finally got around to trying pyglet, and it seems to be a more than adequate replacement for PyGame. I'm always in favour of getting more people to use Python, so they can spend their time making the games they want to make instead of wrestling with the intricacies of C++, and pyglet looks like the best choice currently, for these reasons: - BSD open-source license, good for almost any project, commercial or not; - runs on Windows, Mac OS X, and Linux, with a decent installer for Windows at least; - Uses OpenGL natively for rendering, not DirectDraw, GDI, or some other slow and/or old approach; - Great documentation, including a very readable Programming Guide plus seemingly accurate API docs; - various cool things built in, such as text rendering, playing of streaming audio and video, z-ordering of blits, sprite sheets, all accomplishable in under 10 lines of code. Here, have an OpenGL-accelerated bouncing ball program:
from pyglet import image
from pyglet import window

# Set these to the size of your background image
win = window.Window(width=768, height=768)

# Set these paths to a couple of graphics
ball = image.load('some_small_image.png')
background = image.load('some_background.jpg')

ball_coords = [0,0]
ball_velocity = [5,3]

while not win.has_exit:
    win.dispatch_events()
    
    ball_coords[0] += ball_velocity[0]
    ball_coords[1] += ball_velocity[1]
    
    if ball_coords[0] > win.width - ball.width or ball_coords[0] < 0:
        ball_velocity[0] = -ball_velocity[0]
    if ball_coords[1] > win.height - ball.height or ball_coords[1] < 0:
        ball_velocity[1] = -ball_velocity[1]
        
    win.clear()
    background.blit(0, 0)
    ball.blit(*ball_coords)
    win.flip()    


All you need to run this is to install pyglet (which requires Python 2.5, or Python 2.4 with ctypes installed), save this as a .py file, ensure you have 2 images in the same directory with the names you supply in the code, and run it. If you want to verify that it's using OpenGL, add "from pyglet import gl" at the top and add "gl.glColor3f(1.0, 0.0, 0.0)" just before background.blit(), and see what you get. It's not perfect yet though: you have to use low-level OpenGL calls to get anything advanced done (eg. glEnable(GL_BLEND) followed by glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) if you want alpha blended sprites), the sprite interface is a bit bare right now (though allegedly this is where Rabbyt comes in), and sound support on Linux seems a little ropey if the docs are accurate. Hopefully nobody minds me posting this; it's not my project and I don't stand to gain anything. I'm just quite excited at Python becoming a more viable platform for our games. Comments and criticisms welcomed, of course.

Share this post


Link to post
Share on other sites
Advertisement
Pretty cool. Someone mentioned Pyglet on #gamedev a day or two ago. I'll probably install it and play around with it over the weekend. I like to stay on top of all Python-related developments.

This also reminds me to look into PyPy, IronPython maturity (and suitability for eventual Managed DX/XNA/SlimDX programming), and StarKiller (is it still alive?). If you can develop Silverlight applications in IronPython...

Share this post


Link to post
Share on other sites
I have also recently gotten into pyglet. I always wanted to make games in python, but I thought my only option was pygame which just seemed too bloated for my tastes.

In my opinion, it is a lot easier to learn (the supplied tutorial tells you what you want to know- and you are not overwhelmed by overly lengthy descriptions of things you would already know if you have ever programmed a game before) than pygame. The included OpenGL support is great (I personally have never used OpenGL before- I am going through the nehe lessons), there isn't any fiddling around with a variation of the commands which you are left to figure out on your own. And the fact that it is included and incorporated into the entire design of the engine makes it very much easier to get into, as opposed to the pygame+PyOpenGL combination of doom.

I definitely recommend this also!

Share this post


Link to post
Share on other sites
Something along the lines of the chimp game tutorial on the pygame site would be nice to see. I didn't dig all that deeply into the pyglet programming guide, into all the nitty gritty details of the implementation. But the five-odd example programs in the "Writing a pyglet application" section were kind of trivial.

Wait, never mind, I see there's some example programs in the downloadable documentation.

Share this post


Link to post
Share on other sites
The fundamentals of game development apply pretty much equally to PyGame, pyglet, and any other similar library. The important stuff is how you handle the input and output, and I believe the documentation is very good for that.

Share this post


Link to post
Share on other sites
Panda3d:

panda3d

Has far more features then pyglet. Its also coded with python. Python is he first class citizen while the engine is written in c++. It can be extended with c++ for speed because python does get slow.

It has your basic and advanced scene graph stuff nodes,instances,rigid body combiners,transparency sorting, view ports.

It has build it physics and collision system (ODE is being integrated as well as roots Physx and Newton)

It has extensive sound support (fMod and openAL)

It has actor animation morphing and stuff (I am not shure about bones - i deal with spaceships)

It has shaders with post processing - which i use alot.

It even has a network library and a patching system for those who want to build a mmo.

Runs on linux an mac as well as that other OS. (also mac support is a little behind - you have to build yourself - while linux support is high quality with) I develop on linux and it runs every where

It has many other things ...

Some images from my mmorts project (
aff:2aw) can be found here :
screenshots of what is possible with panda3d

[EDIT: removed screenshots as they were a bit big for the thread. If you want to repost smaller ones, be my guest -- Kylotan]

[Edited by - Kylotan on December 22, 2007 6:05:24 AM]

Share this post


Link to post
Share on other sites
Today I implemented a Pyglet window manager plugin for my engine, and I love it! Much cleaner than Pygame overall. I might finally let Pygame go, after a long time of not really being happy with it...

Share this post


Link to post
Share on other sites
Damn! Here I am with a half-built Pygame engine and you come along with this. Oh, all the pain Pygame has caused me -- and it's finally coming together!

You've pretty much already got me sold but I'm going to specifically not use Pyglet until I'm ready to build my next game, otherwise I'd be forced to re-do my whole engine in it and drive myself crazy again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!