Java vs Flash vs Python

This is a programming post. Those not interested in programming can quit now.

I’m evaluating switching my default development platform. So far, I’ve been doing all of my games in Java as applets because I was familiar with the language and they run in a web browser. But there are some downsides to it: it’s pretty unstable when managing sounds, some browser/network configurations prevent applets from running, and up-to-date runtimes don’t have the penetration that Flash has thanks to YouTube and friends. Also, I want to change because I feel like it.

Python

So first I grabbed Python. I once coded something simple on it and I loved its syntax and philosophy. Armed with Pygame and the latest runtime, I set to port my current in-development game, Pixelworld as a test. The game is mildly demanding as it is essentially a particle simulator, but it’s just a 128×128 field, so I figured it should not be a problem.

Wrong. Despite an honest effort to optimize as far as I could by tweaking and trying whatever trick I could find on google, a loop that runs 655360 times a second with some logic is too much for Python. The final veredict was that calling user-defined methods in Python is excruciatingly slow and there’s no workaround for it. If I am to inline the bulk of my particle logic without helpers, I might as well not code in Python at all. If you are a Python wizard and want to have a shot, here’s my final source code. If someone manages to make it run above 30 fps without making the code unreadable, I’ll apologise for this:

Conclusion: Any platform is good for games if you just draw a bunch of hardware-accelerated sprites. I just needed a bit more on the soft side, and as much as I liked the language, it does not provide the penetration of Flash or the raw performance expected from a standalone game.

On to…

ActionScript 3.0

Then I started with AS3 on Flex builder. Again, Pixelworld is about writing pixels on a surface, so I knew from the start that I was going to be pushing Flash beyond it’s area of expertise. However, Flash really stood up for the pixel mangling after implementing several obscure hacks I found here and there coupled with some code optimization in general. The language is ugly, but the runtime offers many possibilities (i.e. Kongregate). However, I had to optimize beyond what it’s healthy for this game. You can check out the Flash prototype.

Conclusion: I’m not going to do Pixelworld in Flash 9. But I’m probably going to switch over to it soon just because of the runtime. Maybe Flash 10 makes a difference to me. Maybe Silverlight will be out of beta… for now… I stop promiscuity and go back to safe sex.

18 Comments so far
Leave a comment


Maybe I can help a little.

Python is definitely not meant for speed. No matter what you do, its an interpreted language. It´s highly dynamically typed, so it is also purely interpreted.

Java and Ecmascript (flash) are a lot more static, which allow for JIT (just in time compilation) optimizations.

I personally like C for this kind of things, because even though it´s a low level language, it allows you to abuse your computer without guilt. “The Collage” does some fairly complex colorspace computations to determine how good your approximation to the picture is (and hence compute the perfection ratio), which would have needed a lot more code to do in python or flash, and probably not so much more in java.


It’s really not necessary to go as low as C or C++ for this…

Java is currently at 40 fps without jittering in a 128×172 particle grid and I did no hardcore optimizations.


Dani, I’m sorry you tried Python with PyGame, which relies on SDL.

If you sometime decide to give python a second chance, please try cocos2d ( http://cocos2d.org/ ). Built on OpenGL, it gives enough speed for the things you want.

Oh, btw, for reduz: python is as interpreted language as java! Come on..


tenuki:

Java is statically typed language, which allows for much, much better compile time optimizations. A function, for example, will always do the same and this allows for JIT compilation to happen (look up on this on wikipedia). In python, a function does wathever its parameters and self are (type-wise) at the time it’s called, so you can’t really compile that function.

pygame is probably a lot faster than cocos2d for what Daniel is trying to implement, since his demo (i guess) pretty much consists on iterating through all the dots on the screen and depending on their type, do one or another action. This is something Java can optimize great.


reduz:
You have jit for python too. Does having Jit makes it interpreted or not?? Come on..


If JITting in Python involves using Psyco, I did try it… better, but still nowhere good enough :).

Reduz, Tenuki: Arguing over theoretical aspects of languages is not very productive from my point of view :)


jaja te juro que no entiendo el juego.. pero me gusto! la animacion de los pixels esta genial!

otro lime como el del heartbroken! (otro que no entendi mucho pero lo jugue varias veces)
(maldicion! desde las cleaning chronicles que perdi un poco de ese algo que me hace entender este tipo de cosas)…

cuanto falta para que cuentes el gran “por que”?! dos meses?
je.. un abrazo dani! que andes de pelos!! y que la suavidad te acompañe!


I really like the idea of having a world made of pixels, and every pixel with it’s own behavior but also being part of something larger or bigger… The idea came up after or before your post on ADVA’s forum? I’m really curious about what you are planning to do with it.

Saludos!


@Fran: I am still not sure, but I think it will be about exploring a manipulating such a world, which will be dynamic (ie: inhabited).


Did you try the NumPy module? It provides a lot of optimized mathematical functions, including a fair bit of linear algebra and arbitrary-dimensional matrices.

It doesn’t make up for Python’s slow function calls in general, but it may help to speed up math-intensive sections.

http://numpy.scipy.org/


Hey Dude,

Are you expecting to run levels where all the pixels need to be active all the time? I ask because the flash demo showed a mostly static level.

Iterating over “sleeping” pixels is going to waste a hell of a lot of processing time, so if you’re not doing it already I reckon you should try maintaining a list of active pixels and only running the update loop for them. It can be a bit of a pain to maintain this extra code, especially when determine whether to wake a particle or put it to sleep, but it should reduce your update time by an enormous margin and allow for far larger levels.


Hey Farbs! Nice having you here!

I would do the “active particle” you mention if I were completely forced to use Python. Still I would feel an itch, because the gameplay I’m thinking about might involve heavy movement every now and then. And the performance drops dead when I update many particles :(.

In any case, I would certainly love programming in Python… just not for this game.


Happy to be here :)

Running custom logic for every pixel every frame does allow some awesome flexibility, I agree. Still, if the alternative allows you to run levels tens of thousands of pixels high and wide it’s gotta be pretty tempting. Have you spoken to Petri about his blood surfing game?

Python is a bunch of fun, but convincing people to download an installer is not. Browser platforms are the future of freeware gaming IMO.


Petri makes his games on C++, I think… so this discussion is not something he would be very concerned about :).

I agree on the installer thing… that’s why I’m switching over to Flash.

Even though I like programming in Java, the runtime is a little unstable across browsers, so for the time being, I’ll just use it when raw horsepower is needed (which is rare).


I’m somewhat new to web type programming and I’ve got a question that’s related to this. I want to make a rather old school type RPG game that will run from a browser and also involve uploading pictures and text files as part of it (yeah…kinda weird, want to make it abit more interactive story).

I started writing the basic structure in Java, but looking into Flash (both seem to be the most used for Web Apps), it seems like it’d be easier to work with later on, but I’m worried about system performance (as I’ve tried some simple shooting games lag at times, and this post also mentioned the same issue). Both languages I’m only moderately familiar with, stronger in C/C++ but I was told by my buddy to work in Java as it gives less a headache when making web apps and for backend integration. Any advice/opinion regarding the coding matter?


Well… there are two different domains in your game, and I think it’s better for you to implement different solutions: the backend and the client.

Java as a client is fast as it comes, and runs in a browser. But the runtimes are sometimes a bit shaky (Night Raveler crashed some configurations of browsers). Also, Java APIs often err on the side of generality, which implies verbose code to do even simple things:).

There are techniques on Flash programming from which you can gain a lot of performance (ie: using BitmapData instead of Movieclips), the runtime is everywhere and is very stable. The language itself is ugly as Java, but with a more concise API. Also, you can publish in sites like Kongregate. The case of Pixelworld is that it’s extremely computation-heavy, but unless you’re doing such a game, Flash is a safe bet. I did not research into Silverlight.

On the other hand, the backend… I wouldn’t use Java if you are trying to do something simple… you can maybe try the Google App Engine, or Ruby on Rails, or plain PHP. Again, Java is made for generality, and more often than not you need to figure out quite a bit of different APIs to do simple server things. I think Flex (an environment that generates flash apps) has some server functionality, but I wouldn’t even touch that functionality with a stick.

Is this useful? :)


Quite so, thanks Daniel =)


Good post.



Leave a comment



  • daniel[@]ludomancy.com