parentNode.org

The building blocks of a solid frontend.

The Worlds smallest Flash Pong Game

Posted in Flash by Matthew Lloyd on the February 10th, 2008

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…

Here is my version:

//
// Pong.4k (393 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 v:Number = -10;
var w:Number = 10;

// player 1
var p:MovieClip = attachMovie("a", "p", 1);//createEmptyMovieClip("p", 1);

// player 2
var q:MovieClip = attachMovie("a", "q", 2);//createEmptyMovieClip("q", 2);

p._height = q._height = 80;

// ball
var b:MovieClip = attachMovie("a", "b", 3);//createEmptyMovieClip("b", 3);

// magic loop
function onEnterFrame() {

  q._x = 550;

  p._y = _ymouse;
  q._y = 400-_ymouse;

  // bounce top or bottom border
  with(b)
  {
	  _x += v;
	  _y += w;
	  if (_y<10 || _y>390)
		w = -w;

	  // bounce player1 or left border
	  // or player 2 or right border
	  if ((hitTest(p))
	  || (hitTest(q)))
		v = -v;

	  // someone scored
	  if (-10>_x || _x>540) {
		_x = 275;
		_y = 200;
		v = -v;
	  }
  }
};

Here is the screenshot: http://www.devslash.com/wp-content/smallpong.jpg
Here is the fla: http://www.devslash.com/wp-content/smallpong.fla
and here is the final file: http://www.devslash.com/wp-content/smallpong.swf

Leave a Reply