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.
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.
By reduz on June 25
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.
By Daniel Benmergui on June 25
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..
By tenuki on June 25
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.
By reduz on June 25
reduz:
You have jit for python too. Does having Jit makes it interpreted or not?? Come on..
By tenuki on June 26
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
By Daniel Benmergui on June 26
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!
By yñZ on June 27
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!
By Fran on June 30
@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).
By Daniel Benmergui on June 30
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/
By Nick on July 12
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.
By Farbs on July 16
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.
By Daniel Benmergui on July 16
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.
By Farbs on July 16
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).
By Daniel Benmergui on July 16
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?
By JonL on July 16
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?
By Daniel Benmergui on July 16
Quite so, thanks Daniel =)
By JonL on July 17
Good post.
By Bertha on October 28
If you do try python again, look at Pyglet (http://www.pyglet.org/) instead of Pygame. Pyglet does everything in OpenGL, and you could totally implement this game by rendering a grid of quads instead of editing a bitmap.
Also, as far as browser plugins go, Unity (http://unity3d.com/) is a very fast one that can deliver OpenGL and DirectX. Blurst.com shows off a lot of games made with it. You can script it in Boo (a python dialect), JavaScript, or C# (similar to Java). Again, you could use colored quads =].
By Nick Retallack on August 2
Java and Javacript are not the same thing just so you this article should have been listed as javacript not java
By just another programmer on August 22
@Just Another Programmer: I was talking about Java, not Javascript
By Daniel Benmergui on August 22
Wow, I had to stop right at the first paragraph. Why the hell would you have a loop that aruns 655360 times a second? For a game that simple? You really need to rethink it’s design IMHO buddeh.
By Anonymous on September 16
I made the same mistake you did when starting programming in python.
By looking quickly at the code I think the bottleneck is on Field.update(self, surface) (I didn’t even run the code, so I apologize if there is a slower function).
The problems seems to be:
You generate a double list of width*height bools each time you run update (which I guess runs on every frame?). That’s a very costly operation.
Then you run two nested fors, looping through height and width. That’s a no-go in python. For loops are inocent operations on C and probably on Java, but really performance killers on python.
Generally speaking, you’ll want to rethink the update function and probably other parts of the code too. Replace fors with maps, for example.
And you’ll definitely want to consider using numeric manipulation with numerics.
Finally, Flash is the way to go if you seek browser penetration (Python having 0%
)
By Ezequiel on November 7
Hello visit that page regarding oil crisis Bye Bye
By peak oil on November 12
thanks good post
By yilbasi otelleri on November 12
Ohh very much thanks admin
By Childhood Obesity Facts on November 12
I like your blog. Really helpful and very motivational. Thanks a lot. This will help me a lot.
By Free Link Exchange on November 12
good quality post thanks
By chat on November 12
Hey – wonderful website, just looking around some blogs, seems a pretty great platform you are using. I’m presently using Drupal for a few of my sites but looking to modify one of them over to a platform very much the same to yours as a trial run. Anything in particular you would advise about it?
By bad credit loans on November 12
thanks good post
By zayiflama lida fx15 ve biber hapi zlfvbh on November 12
This blog post gives the light in which we can observe the reality. this is very awesome one and gives indepth information. cheers for this awesome post!
By cash payday loans on November 12
Ohh very much thanks admin
By film izle on November 12
Great! Thanks for post
By adana oto kiralama on November 12
Ty for the comparison
i’ve been wondering for a while now
By Scott on December 13