Phil Hassey - game dev blog
Phil Hassey as Snidely Whiplash
"You can't buy awesomeness.
You're born that way."

Archive for the 'python' Category

xkcd “Self-Description” solved with python+pygame

Wednesday, January 13th, 2010

Well, something about this xkcd comic drew me in …

The 3rd panel reminded me of a study I did in college on iterated function system fractals.  These fractals are known for creating ferns, trees, and the Sierpinski triangle.  There are a number of ways of creating these fractals, but one of them is called (something like) the reducing photo copier method.  Where you can start with ANY starting image on your photo copier, and just shrink the images, make a few copies, position them on the copier, and repeat.  Eventually your image converges towards the fern / triangle / whatever if you are precise.  (This can be done with a ‘Real Life’ photo copier for simple IFS like the Sierpinski triangle.)

So as a way to avoid work that I’m supposed to be doing this evening, I figured I’d see how “accurate” this comic was.  Here’s my pygame code that checks it out.  If you run main.py it will start with an image (the xkcd logo) and each time you press a key it will do an iteration of re-creating the comic.  You can see it flashing between the current iteration and the original strip.  After about 4-5 iterations the images converge (pretty closely.)

The most interesting bit was trying different starting images and seeing how long it took to converge.  Starting with all black  it takes 6 iterations.  Starting with all white takes 5 iterations.  And starting with the xkcd logo takes 4 iterations.  The logo probably goes the quickest because it has some black and white in it (like this image) so it converges faster.  (You can edit main.py towards the bottom page to try these different starting points.)

So .. there ya go!  Enjoy!

-Phil

Is None more like 0 or -1 ?

Thursday, January 7th, 2010

In tinypyC++ every variable is a certain type.  In some cases I may have an int x = None;  But I’m not sure what that should mean?

What’s the most pythonic? Here’s some anecdotal evidence:

>>> None,None==None,None==-1,None==0,None==1,None==False,None==True,bool(None)
(None, True, False, False, False, False, False, False)
>>> -1,-1==None,-1==-1,-1==0,-1==1,-1==False,-1==True,bool(-1)
(-1, False, True, False, False, False, False, True)
>>> 0,0==None,0==-1,0==0,0==1,0==False,0==True,bool(0)
(0, False, False, True, False, True, False, False)
>>> 1,1==None,1==-1,1==0,1==1,1==False,1==True,bool(1)
(1, False, False, False, True, False, True, True)

So .. you can see -1 gets 4/7.  0 also gets 4/7.  (1 gets a mere 3/7, so it’s out.)

Anyone care to tilt the scales to -1 or 0?

Seahorse Adventures – Loading TGAs (and more)

Tuesday, January 5th, 2010

Here’s a screenshot.  This is my Ubuntu desktop of my work on a new iPhone game.  Take a look at it nice and big.  Below I’ll explain all of what is going on …

bsa_dev1

In short, I’m working on porting my pyweek#3 team entry to the iPhone.  Here’s the details:

  • Top left, you see Kate, my text editor.  You’ll notice I’ve got -what appears- to be python.  But that’s actually tinypyC++ code!  My converter still has some rough edges, but it’s starting to get pretty good.  If you weren’t looking to closely you might mistake that for ordinary python code!  (In fact, it should be 100% python parser friendly.)  You can see how I have to use a touch of annotation to make it all go.  But for the most part, the types get inferred.  (The C++ outputted is about 2x as long, so I’m saving a ton of keystrokes!)
  • Top right, you see the level being painted.  It isn’t all working yet, but those are the basic tiles.  Interesting bit about loading the level, when I created this game I used my own level editor and library I made with pygame.  To save on disk and time I save all the levels to .tga files.  Since I’m targeting the iPhone I need an easy way to read the data from .tga files.  I don’t have SDL_image available, so a friend pointed me towards this great site.  It appears to have some awesome bits of code, including very simple and easy to use image loader that can load tga/png/jpg/etc.  I think it has the most painless interface I’ve ever seen for a C-based image-loader.
  • Bottom right you can see me working on tinypyC++.  As I’m working on BSA, I’m always finding new bugs in it.  Lately most of the bugs have been of the “add more graceful error handling” nature.  tinypy will point out what line (and character) an error happened on, but with a bit of extra work I’m able to add in some coherent error messages to tell the user what is going on.  In this case it was to inform the user of an undefined module name.
  • Bottom left you can see the startup of the game.  I’m using irrKlang for all my  game audio now.  It is not open source, but it’s “free” to use for free games, and the price is quite reasonable for commercial projects.  irrKlang is not portable to the iPhone yet, so I have to have a separate driver for my iPhone audio.

And that’s it for today’s report!

-Phil

New Year’s Python meme

Monday, January 4th, 2010
Hmmn, better late than never, ‘eh?
  1. What’s the coolest Python application, framework or library you have discovered in 2009 ?
    pygame – I’ve used it for ages, but I rediscovered it this year for a new use.  Now-a-days I use it for processing images (creating batches of icons, creating texture atlases, etc)
  2. What new programming technique did you learn in 2009?
    I’ve used databases for _ages_ in web development.  This year a friend suggested I could use databases to store data for games and 1. avoid recompiling 2. avoid parsing 3. use a nice database frontend to manage it.  I’ve only experimented with it briefly, but it seems like a pretty keen technique 🙂
  3. What’s the name of the open source project you contributed the most in 2009? What did you do?
    I was a bit of an open-source waste-of-space this year.  But in the last two weeks of the year I began doing work on my tinypyC++ converter.  So far I’ve used it to put one game on the App Store.
  4. What was the Python blog or website you read the most in 2009?
    Planet Python.  Thanks everyone 🙂
  5. What are the three top things you want to learn in 2010 ?
    How to do game PR.  How to present at conferences.  How to not go crazy.

Over the course of 2009 I used python much less than in 2005-2008.  This year (with the help of tinypyC++) I hope to be using (something like) python again.  C++ just isn’t nearly as fun!  I don’t plan on attending PyCon this year, though I wish I could.  There’s just a limit to how many conferences I can attend.  If anyone is at GDC, catch up with me!  I’ll be speaking at the iPhone Summit about multi-player network testing.  I always gotta thank the python community for introducing me to the magic of testing.  It’s what makes network code and mad projects like tinypy possible.

-Phil

Elephants! is free on the App Store!

Wednesday, December 23rd, 2009

My gift to you: A totally FREE iPhone game! I made this game several years ago with some other way-cool game-dev friends. In “Elephants!” you save the giraffes while jumping on a rolled up squirrel! More fun than a barrel of pigeons! For Christmas this year I figured I’d port it over to the iPhone for you 🙂 Check it out!

From the dev side, yeah, this is my first attempt at “python” on the iPhone. Even though it’s actually restricted tinypy code. Here’s a sample of game code so you get the idea of what’s behind this game. All this code is converted by tinypy into C++ code so I can compile it with Xcode for the iPhone:

def elephant_new(g:Level,pos:List(int))->Sprite:
    s = Sprite('elephant',pygame.Rect(43-14,8,28,48))
    s.rect.set_centerx(pos[0])
    s.rect.set_bottom(pos[1])
    
    g.sprites.append(s)
    
    s.ball = ball_new(g,(int(s.rect.get_centerx()),int(s.rect.get_bottom())))
    s.suit = ElephantSuit()
    s.vy = 0
    s.vx = 0
    s.jump = 0 
    s.facing = 'e'
    s.score = 0
    s.z = 1
    s.state = 'live'
    s.name = 'elephant'
    
    return s

Elephants! First python + pygame game submitted to App Store :)

Saturday, December 19th, 2009

So, I managed to submit a “python” app to the App Store — “Elephants!” Here’s a few crazy things I had to work out to get things going:

  • I found that ObjC doesn’t care much for C++ objects that do their own reference counting.  To have a “Game” object at all, I had to use a pointer to my object instead of using my reference counting object.  It seems that during some of the “magic” of ObjC it copies objects without calling any of the C++ copying methods, so reference counting gets killed.
  • I ported a basic subset of pygame to tinypyC++ and made it work under openGL.  I don’t have this available in my tinypyC++ repository because it takes a good deal of prep-work to use (isn’t out-of-the-box) so it wouldn’t be generally useful to anyone yet.
  • I was able to get pause on call / resume working by saving the game state to a text file.  Since tab-separated files are so easy to create and parse in python this seemed to be the easiest route to getting the project done.
  • I learned a ton about C++ templates.  I still haven’t even touched the tip of the iceberg, and I’m not sure I want to, but at least I’m getting the basics down and that seems to be enough for me to stumble through this project with.

You can check out my current progress at svn://www.imitationpickles.org/tinypy/branches/tinypy2 (in the tinypy/example folder you can check out a julia fractal demo.  It also includes a mini pygame module that depends on SDL not OpenGL, so it actually works out-of-the-box.)

Here are some things I’ve been adding this week in preparation for my next project:

  • I’ve added “weak pointers” .. Since reference counting has the problem of cyclic references never getting collected, I had to do this for some use-cases.  In tinypyC++ weak pointers are just normal pointers, and if the object is collected, the pointer will be invalid, so be careful.  They are created by doing stuff like “x = ptr(GameData())”
  • I’ve added more C style types.  I’ve got uint16, cstr, Array, etc.  This all makes it possible for me to have a different kind of object – a struct in tinypyC++.  The struct is meant to be a C oriented type that can be saved directly to disk.  In Elephants I serialized via saving CSV data.  In the future, if I keep all the game state in C struct type data, I can just use a single simple line like “open(‘data.txt’,’wb’).write(struct_dump(game_data)”
  • I found that STL extensions include a “hash_map” which turns out to be about 2x as fast as the normal “map”.  I’m using that now.

I’m not entirely sure how useful this project is going to be for anyone else, or even for me for that matter.  But I think after my next game project I’ll have a much better feel for what the situation is.  TinypyC++ has some real tradeoffs in terms of being a bit of a hybrid of C++ and of python.  It doesn’t offer the full power of either language.  But I’m doing my best to capture a middle ground between them that will make my life easier.

Restricted tinypy to C++ compiler

Tuesday, December 1st, 2009

elephant1I’ve spent the last week working on a tinypy to C++ converter.  It works!  See the screenshot to the right – I’ve managed to port a pygame game over to C++.

Here’s how (and some of the catches):

  • I require type annotation of all the functions and methods.  “def test(x:str)->str: return x”
  • I do two passes on each file, the first pass to catch all the function types and class members, and the second pass to generate the code.
  • I generate C++ code that has automagic reference counting.  So you have to code your script so it won’t have any cyclic references if you want garbage collected for you.

How is this different from shedskin (really cool project!)?

  • Built-in reference counting, instead of using libgc.
  • I require the user to type annotate everything.
  • It only supports a subset of the tinypy subset of python.  Shedskin supports a much larger subset of python.

So what’s the point?

  • Well, I learned a lot about STL and C++.
  • I know it will produce iPhone friendly code, I’m pretty sure libgc isn’t iPhone friendly?  (At least, I haven’t found anything via a few searches…)
  • Way less magic.  Since everything is annotated, there are no surprises.
  • Implementing C++ modules is pretty easy – the code can be inlined within the python code and it just works.
  • This will make it easier for me to develop C++ games.

Anyway, if you’re super brave, you can check out svn://www.imitationpickles.org/tinypy/branches/tinypy2 .. I don’t have the elephants example in there, but the pygame.py that I include gives you a pretty good example of a complex module.

I’m going to chat with the tinypy folks to see if we’ll merge this into the tinypy trunk or have it as a separate project.  I’m not quite sure what makes sense to everyone else 🙂  The nice bit about merging this in is that I could unify the test suites nicely.  And tinypy would still function as normal, just better tested, and with function annotation parsing supported.  All that said, I should make a tinypy module for tinypyC++ so that I can do some code evals!

Galcon Labs on the App Store!

Friday, October 2nd, 2009

Hey,

So Galcon Labs is now available on the App Store! Check it out 🙂

Anyway, I just got back from 360iDev where I gave a talk about Galcon Multiplayer.  The two main points made in the talk were about managing community, and test driven development.  With so many devs talking about App rejections and other problems, I wanted to make sure the app was approved first time through the system.  And it was, in an excellent 7 day turn-around 🙂  So a real big thanks to all the folks out there who pressed me into reading the Kent Beck book on the subject!

-Phil

Galcon Labs – Preview!

Thursday, September 24th, 2009

labs-asn-1-p-txtHey,

After a few days of itunesconnect being down, I was able to submit Galcon Labs to the App Store.  Check it out!  (See Phil’s first ever video!)

On a dev note, this game was REALLY fun to make.  I got to do things that completely change the gameplay of Galcon so that Galcon Labs is really four totally different feeling games.  My favorites are the Billiards mode (where the planets move around) which is just a lot of fun, and the Assassin mode where instead of trying to conquer everyone, you are assigned a single player that you have to destroy first.  The interesting thing about that, is if you destroy another player, or the player who is trying to annihilate you gets destroyed – YOU LOSE!  It really changes up how you have to play!

As usual, testing (TDD) was a big deal.  Having a network game makes it so that everything has to be tested to make sure it really works.  I’m going to be doing a talk at 360iDev on Wednesday, so if you want to hear me compare UDP networking to herds of rabid animals, be sure to attend.

Also, check out pygame 1.8.  It is really awesome 🙂  While working on Galcon Labs I felt I needed to put more effort into differentiating the game from previous Galcon games visually.  As it’s still triangle ships and planets, this took some effort.  And during the last hour, I decided I needed a whole shiny new look for the planets.  In the game I use a texture with 64 planet designs on it.  So creating all those by hand would have been too hard.  I used pygame to generate new landscapes and found the new pygame 1.8 features really helpful.  I was able to use the new blending modes to trim the landscapes into circles and rotate and scale them down so I got a nice antialiased look for all the planets.  In previous versions of pygame, this would not have been nearly as easy!  So, not just for games, but for generating and doing automated image manipulation, pygame is really starting to shine 🙂

Cheers!
-Phil

Oddball python import issue?

Thursday, July 16th, 2009

It seems when I have the import within my function I get some strange behavior.  For lack of cooperative blog formatting, here’s a link to the code and the results.  I tested this with python 2.5.2 and 2.6.2.

Now that I’ve pinpointed the issue I can easily work around it.  But .. I’m still curious if anyone can tell me what is going on?  Is this a feature?  Or is there a bug at work here?