Sign in to follow this  
Followers 0
Kylotan

Recommendation: pyglet library for Python

24 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
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...
0

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!
0

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.
0

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.
0

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]
0

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...
0

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.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by dbaumgart
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.

Wise choice.

By the way, I've been enjoying your journal very much!
0

Share this post


Link to post
Share on other sites
Quote:
Original post by treeform
Panda3d:

panda3d

Has far more features then pyglet.


Panda3D is quite impressive, but it's not really comparing like-with-like - you could also be comparing Python-Ogre, for example. It looks good for 3D work though. Bit of a shame the license is a bit odd, however.
0

Share this post


Link to post
Share on other sites
Sounds cool. I'll probably move my PyGame experiments with steering behaviours over to Pyglet over the holiday.

I'm actually quite glad low-level OpenGl is part of the equation - means I can use all my existing OpenGL stuff, once I've applied a little boost.python magic fairy dust.
0

Share this post


Link to post
Share on other sites
The problem with OpenGL and libraries that use it is that if you don't have working hardware acceleration, you're out of luck. The bouncing ball example runs at a whopping 4FPS here using software rendering (Linux/Mesa). If you're making a simple 2d game, consider that some people play 2d games *because* they don't require special hardware / drivers.

0

Share this post


Link to post
Share on other sites
Well, it's definitely a choice people will have to consciously make. What's important to me at this stage is that with the hardware now commonly available for 10 years, we should be able to take advantage of it easily.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Barius
The problem with OpenGL and libraries that use it is that if you don't have working hardware acceleration, you're out of luck. The bouncing ball example runs at a whopping 4FPS here using software rendering (Linux/Mesa). If you're making a simple 2d game, consider that some people play 2d games *because* they don't require special hardware / drivers.

Even the lousy intergrated intel chipsets have enough oomph to push around a few textured quads, and will have drivers preinstalled. If you can't be bothered to install suitable drivers for your own linux box thats your own problem.
0

Share this post


Link to post
Share on other sites
@kylotan
I currently have your logo bouncing around a screenshot from your demos on your site with alpha blending working, this python stuff is fun! I call it KYLO_BALL! ;)

Andy
0

Share this post


Link to post
Share on other sites
finally got around to checking this out, but lack of joystick support kind of killed it for me, which is too bad as I'm still looking for a cross-platform game library that supports joysticks and rumble among other needs.
0

Share this post


Link to post
Share on other sites
Apparently you can use the input handling from Pygame and stick with Pyglet's rendering and everything else if you want. Not ideal though, I admit. :)
0

Share this post


Link to post
Share on other sites
Yeah, I've been interested in the number of creation libraries/engines/tools using scripting languages. Some to check out.

Pycap (tuxcap on linux) for the Popcap Framework
Fenix (uses its own language)
Novashell/Agen (both use Lua and both early in development, Agen more so)
0

Share this post


Link to post
Share on other sites
Thanks for posting that. I was about to start learning PyGame tonight, but if Pyglet is as good as it sounds, I'll learn that instead.

I also wish more people would use Python for game dev. It seems like the saved time would be a huge benefit for hobbyist programmers especially.

~Cody
0

Share this post


Link to post
Share on other sites
FYI: I started making a Pyglet-based 2D game from scratch on Monday: http://www.brainfold.org/blog/. So far, I've liked it very much, and can't think of anything seriously wrong with it. Even the documentation is far better than Pygame's. The only "problem" is the lack of joystick support, but as I already said in an earlier reply, that's apparently in the works. I think Pygame deserves to get dethroned. [smile]
0

Share this post


Link to post
Share on other sites
Err, it's a mess, because my OpenGL skills are weak.

# these are your window dimensions
width = 800
height = 600

# Setting it up
framebuffer = c_uint(0)
depthbuffer = c_uint(0)
img = c_uint(0)

if not gl_info.have_extension("GL_EXT_framebuffer_object"):
return False
print "Verifying Framebuffer Support: %i" % gl_info.have_extension("GL_EXT_framebuffer_object")
if not gl_info.have_extension("GL_ARB_draw_buffers"):
return False
print "Verifying ARB buffer drawing Support: %i" % gl_info.have_extension("GL_ARB_draw_buffers")

# Set up rendering state for the offscreen buffer
glClearColor(0.0, 0.0, 0.2, 0.5)
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)

# "Setting Up"
glGenFramebuffersEXT(1, byref(framebuffer))
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer)

# "Adding a Depth Buffer"
glGenRenderbuffersEXT(1, byref(depthbuffer))
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer)
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height)
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer)

# "Adding a Texture To Render To"
glGenTextures(1, byref(img))
glBindTexture(GL_TEXTURE_2D, img)

# only works with these two lines!!!!!!!!!!!! (nearest works, as well as linear)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, None)
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, img, 0)
status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)
print status
if status == GL_FRAMEBUFFER_COMPLETE_EXT:
print "complete, yay!"

...

# Using it
# First we bind the FBO so we can render to it
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer)

# Save the view port and set it to the size of the texture
glPushAttrib(GL_VIEWPORT_BIT)
glViewport(0,0,width,height)

# your rendering goes here...

# Restore old view port and set rendering back to default frame buffer
glPopAttrib()
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)

...

# "Cleaning Up"
glDeleteFramebuffersEXT(1, byref(framebuffer))
glDeleteRenderbuffersEXT(1, byref(depthbuffer))
glDeleteTextures(1,byref(img))


Most of it is just copy and pasted from NeHe I think. There are some byref() calls too but it's usually obvious where they go. You can even leave the semi-colons in if you like; Python doesn't care.

Now, if someone can show me how to implement a simple post-processing full screen effect, please do. :)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0