Sign in to follow this  
Trapper Zoid

High level design for Python with C extensions

Recommended Posts

Trapper Zoid    1370
In 2008 I'm working on moving to higher level languages for game development: Flash/ActionScript for prototyping and web games, Python with extensions in C for non-web games. While I've done considerable programming in C and C++ in the past I've currently only dabbled in Python programming, so I'm planning the development steps I need to take from a Python beginner to professional games in a combination of Python and C. However it's been ages since I've worked on a program that uses two different languages together, and I'm having trouble grasping the high level concepts of going about designing an application that uses Python with C extensions. When I Google search for extending Pythons I get a lot of tutorials that explain the low level steps in code to make it work, but not a lot on how to plan a game with C extensions in mind. What I'd like to read is some kind of tutorial or reference book that gives me more insight into how someone goes about planning a non-trivial game or equivalent application in Python with the mindset that they will be coding part of the application in a low level language such as C. I'm hoping with a bit more information on the higher concepts of designing a program for Python I can choose a more intelligent way of structuring my Python programming so that it will be easier to compartmentalise into extensions efficiently. Of course I'll be learning this through experiments and practice myself, but it would certainly help to consider expert advice before I jump in. What references out there can you recommend for high level Python game design combined with home-made C low level extensions? Thanks in advance,

Share this post


Link to post
Share on other sites
Oluseyi    2116
There are basically none.

Have you written a Python game, using a library such as Pyglet or Pygame? Then you've done what you seek - half way. The other half is implementing the extension library, exposing it to Python. Consuming is easy.

Dive in! Make your own mistakes; forge your own path.

Share this post


Link to post
Share on other sites
Trapper Zoid    1370
Quote:
Original post by Oluseyi
Dive in! Make your own mistakes; forge your own path.

That's basically my plan by default, but I felt it makes sense to at least have a quick peek at what professionals recommend. It might help me make the right sort of mistakes [smile].

If there really isn't much good advice for planning in Python, I'll just adapt the approach I'd go about a C only development except implementing it in another language, then see how effective that is.

Share this post


Link to post
Share on other sites
eedok    982
The way we did things at work(wasn't games, but there was a lot of performance sensitive code) was to just do the program entirely in python for the first go around, the profile for problem parts, and if there was a problem that was related to python, we would isolate it to a module, then redo it in pyrex. In the rare cases this wasn't enough, then we'd drop down to the C level.

Share this post


Link to post
Share on other sites
Trapper Zoid    1370
Quote:
Original post by eedok
The way we did things at work(wasn't games, but there was a lot of performance sensitive code) was to just do the program entirely in python for the first go around, the profile for problem parts, and if there was a problem that was related to python, we would isolate it to a module, then redo it in pyrex. In the rare cases this wasn't enough, then we'd drop down to the C level.

That seems sensible, and the approach I've seen suggested from a lot of Python developers. I honestly do not know how much of a problem performance bottlenecks would be in pure Python as I have not yet tried anything performance intensive.

I've problem jumped the gun a little with this whole post, as I've as of yet only programmed enough Python to know it's straight-forward enough to be good choice of language for me. As of yet I've only used it with curses for console-based applications, so I should try it out with a bunch of small games using one of the Python game modules first to get an idea of design issues. But I guess discussing this sort of thing on a forum with other developers does help me make decisions for myself...

Thanks for the replies, it's helping me make up my mind about which way to go. The (semi-rhetorical) question for me now is: start with pyglet or the more well-known pygame?
(Semi-rhetorical because I'm sure they're both fine for early gamedev, although I'm leaning towards pyglet)

Share this post


Link to post
Share on other sites
Kylotan    10013
Quote:
Original post by Trapper Zoid
If there really isn't much good advice for planning in Python, I'll just adapt the approach I'd go about a C only development except implementing it in another language, then see how effective that is.


Personally, I think that is the best way to go. Python isn't so different that you need to approach the writing of a program in totally different ways. It's more in the way that you implement your algorithms that you'll find different approaches.

However, I will say the following:

- err on the side of dividing code into more modules rather than fewer. If you then need to compile some code in C++, or perhaps via Pyrex, this will be easier if you have more fine-grained modules. I think Eedok has pretty much implied this already, but it's worth repeating.

- get to know the Python standard library, and several of the better known external libraries. Many of them are already compiled in C for high performance. If you can use one of these rather than coding up your own C extension, you will save time and probably gain performance over your own attempts. Read at least the first page of every section here to get an idea of what is available to you. And go here see what else is available.

- work with the language, not against it. That means not doing things like creating C++ or Java-like inheritance trees when Python rarely requires inheritance. It also means trusting built-in types like tuples and dicts rather than coding up your own thin data-storage classes. It means learning to love the list comprehension syntax instead of writing out explicit loops all the time. If you see some Python code and it looks a bit strange, try and understand why it's written that way, and you'll be better off.

PS. I would always recommend Pyglet over PyGame, but make sure you know the limitations of Pyglet first. Pyglet's documentation is very good so it doesn't take long to get a good idea of what it is capable of and what it is not.

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