pouët.net

pong1k by Mr.doob [web]

//
// Pong1k (1,022bytes game)
// by Mr.doob
//
// If you want to increase the level of difficulty,
// make the browser window smaller :P
//
// inspired by monoPong-1k
// http://pouet.net/prod.php?which=48976
//

stage.align = "TL";
stage.scaleMode = "noScale";

// speed
var v = {x:-10,y:10};

// player 1
var p1 = new Sprite();
addChild(p1);

// player 2
var p2 = new Sprite();
addChild(p2);

// ball
var b = new Sprite();
addChild(b);

// drawing the squares
d( p1, 40, 80);
d( p2, 40, 80);
d( b, 20, 20);

addEventListener("enterFrame",l);
	
// magic loop
function l(e)
{
	b.x += v.x;
	b.y += v.y;
	
	p2.x = stage.stageWidth;
	
	p1.y = mouseY;
	p2.y = stage.stageHeight - mouseY;
	
	if (b.y < 10 || b.y > stage.stageHeight - 10)
		v.y = -v.y;
	
	if ( ( b.hitTestObject(p1) && b.x > 20 ) || ( b.hitTestObject(p2) && b.x < stage.stageWidth - 20 ) )
		v.x = -v.x;
		
	if (b.x < -10 || b.x > stage.stageWidth + 10)
	{
		b.x = stage.stageWidth * .5;
		b.y = stage.stageHeight * .5;
		v.x = -v.x;
	}
}

// square drawing function
function d(t,w,h)
{
	t.graphics.beginFill( 0xFFFFFF, 1 );
	t.graphics.drawRect( -w*.5, -h*.5, w, h );
	t.graphics.endFill();
}