Pong in 349 bytes
Speed|90 (from over at http://www.flashbookmarks.com/ and #flash) decided to put a challenge to me. He pointed out people have been making pong in smaller and smaller versions, and my task was to try and make a version of pong in 200 bytes. I sort of failed only managing 393 bytes. I was tempted to do the old __bytecode__() trick, though you would be suprised that it doesnt actually reduce much in terms of size (in my tests it increased in size). So I cut down the variable count, removed and calls to Stage and such, removed as many compares and ALU calls. The origonal was drawing the square using API calls, very wasteful, so I used the API to create a static square and re-sized it. I also used the with() { … } command to save the compiler from making explicit calls to b every time. Here it places it on the stack at the start but doesnt pop it off till you exit the with clause, saves 2*n+1 bytes where n is the number of calls to b within the block. I did have to sacrafice alot to get it to this size though, it no longer scales (calls to Stage take up a few bytes too many). This isnt the sort of approach I usually take to programming so it was fun to play around with what changes the size of a flash file for an hour. I took a screenshot to prove I did it, the irony being the screenshot is around 100x as big as the file itself…
*EDIT*
I have dropped it down to 374 bytes. I was told by Chris Benjaminsen that Flash gzips itself including the bytecode. So I decided to push a certain letter higher up the huffman encode list, in this case ‘a’. Basically if you dont mind all your variables only containing one letter then this will save you around 1byte per character. Not much but if you are writing for the mobile platform, size can be everything. After that I cut down the constants so that the only constants it has in bytecode is 400, 200, 80 and 10, that cut down a substantial ammount. Since I saw it gzips the code, I thought I would do my own version of huffman encoding, by giving the most used variables the shortest variable names… It took about 10 mins to work out on a small program like this and it saved 1 byte… (I think this optimization thing is getting silly now.) I also found you can drop it down even further if you check the compiler option ‘Optimize for Flash Player6 r65′ and pull the flash player export version down to Flash Player 6. This minimizes the ammount of data in the swf to an absolute minimum. At this point I’ve ran out of ideas… Any suggestions give me a shout!
Here is my version:
//
// Pong.4k (374 bytes game)
// by Matthew Lloyd
//
// inspired by monoPong-1k and mrdoob and Kirstof Neirynck
// http://www.crydust.be/blog/2008/01/30/pong05k-a-game-in-510bytes/
// http://pouet.net/prod.php?which=48976
// http://mrdoob.com/blog/post/485/
// speed
var a = 10, aa = 10;
//var a:Function = v;
// player 1
attachMovie("a", "aaa", 1);//var p:MovieClip = attachMovie("a", "p", 1);//createEmptyMovieClip("p", 1);
// player 2
attachMovie("a", "aaaa", 2);//var q:MovieClip = attachMovie("a", "q", 2);//createEmptyMovieClip("q", 2);
aaaa._height = aaa._height = 80;
aaa._x = 400;
// ball
attachMovie("a", "aaaaa", 3);//var b:MovieClip = attachMovie("a", "b", 3);//createEmptyMovieClip("b", 3);
function onEnterFrame()
{
aaaa._y = _ymouse;
aaa._y = 400-_ymouse;
// bounce top or bottom border
with(aaaaa)
{
_x += a;
_y += aa;
if (_y<10 || _y>400)
aa = -aa;
// bounce player1 or left border
// or player 2 or right border
if ((hitTest(aaaa))
|| (hitTest(aaa)))
a = -a;
// someone scored
if (-10>_x || _x>400) {
_x = 200;
_y = 200;
a = -a;
}
}
};
*EDIT*
I managed to drop it to a further 349 bytes! This was done by dropping it to AS1.0 (had to be done unfortunatly) and removing all the initialization code (apart from var a = 10, aa = 10). Removing the initialization code meant we had no instances in our code so I placed 3 instances on the stage named them aaa, aaaa and aaaaa (paddle, other paddle and ball) and then because we were using AS1.0 I obviously had to place the code on the instances themselves. Ive not bothered to update the swf or fla files, I think pretty much everyone has lost interest at this point. Still it proves utilizing the IDE stage has its uses!
Here is the screenshot: http://www.devslash.com/wp-content/smallpong.jpg
Here is the fla: http://www.devslash.com/wp-content/smallpong1.fla
and here is the final file: http://www.devslash.com/wp-content/smallpong1.swf








Nice, but swapping the mouse between players each turn is tough :)
Kristof, do you think that if you add a score to it, the size will increase dramatically?
Anyway, its amazing that you can write that in 510bytes!
Rick said this on July 15th, 2008 at 8:54 am