d=document;v=d.getElementById('c');w=220;h=400;v.width=w;v.height=h;v.style.border='1px solid';cx=v.getContext('2d');cx.r=cx.fillRect;k=0;s=5;z=20;g='gray';r='red';b='black';function sp(){bxr=Math.random()*169;cb=new Array(70);px=99;py=370;bx=bxr;by=180;bxd=1;byd=1;for(i=0;i<7;i++){for(j=0;j<9;j++){cb[j+i*10]=[j*z+z,i*z+z,1];}}}
gp=function(){dc(0,0,w,h,'#fff');for(i=0;i<7;i++){for(j=0;j<9;j++){ct=cb[j+i*10];if(ct[2]==1){if(by+z>ct[1]&&by<ct[1]+z&&bx+z>ct[0]&&bx<ct[0]+z){ct[2]=0;dc(ct[0],ct[1],z,z,g,r);if(k!=38){byd*=-1;by+=s*2*byd;}}else
dc(ct[0],ct[1],z,z,g,b);}}}
bx+=s*bxd;by+=s*byd;if(bx<0||bx>w-z)bxd*=-1;if(by<0){byd*=-1;by+=s*byd}
if(by>h)sp();if(by+z>py&&by<py&&bx+z>px&&bx<px+(z*3)){byd*=-1;by+=s*2*byd;}
dc(bx,by,z,z,r,g);if(k==39){px+=s;if(px>w-z*3)px-=s;}if(k==37){px-=s;if(px<0)px+=s;}
dc(px,py,z*3,z,g,r);};sp();setInterval(gp,30);function dc(x,y,w,h,c1,c2){cx.fillStyle=c1;cx.r(x-1,y-1,w+2,h+2);cx.fillStyle=c2;cx.r(x,y,w,h);}
d.onkeydown=function(e){k=e.keyCode;}
d.onkeyup=function(e){k=0;}
// TAKE OVER THAT CANVAS
d=document;
v = d.getElementById('c');
w = 220; h = 400;
v.width = w;
v.height = h;
v.style.border = '1px solid';
cx = v.getContext('2d');
cx.r=cx.fillRect;
k=0; //KEYPRESSED
s=5; //STEP SIZE (FOR BALL AND PLAYER)
z=20; //CUBES HEIGHT/WIDTH, PLAYER HEIGHT
//MMM.. COLORS
g = 'gray';
r = 'red';
b = 'black';
//SETUP FUNCTION
function sp(){
//SET RANDOM BALL X POSITION
bxr=Math.random()*169;
//SET INITIAL POSITIONS
cb = new Array( 70 ); //BLOCKS (9x5)
px=99;py=370; //PLAYER X, Y
bx=bxr;by=180;bxd=1;byd=1; //BALL X,Y
//FILL ARRAY BLOCKS, ENABLE ALL
for(i=0;i<7;i++){
for(j=0;j<9;j++){
cb[j+i*10] = [j*z +z,i*z +z,1] ; // FILL X, Y, ENABLE=1
}
}
}
//GAMELOOP
gp = function(){
// CLEAR SCREEN
dc(0,0,w,h,'#fff');
// UPDATE&DRAW CUBES
for(i=0;i<7;i++){
for(j=0;j<9;j++){
ct = cb[j+i*10];
if (ct[2] == 1){
// UPS!, DID I HIT YOU WITH MY BALL?
if ( by+z>ct[1] && by<ct[1]+z && bx+z>ct[0] && bx<ct[0]+z ){
ct[2]=0;
dc( ct[0] , ct[1], z,z, g, r);
if (k!=38){
byd *= -1;
by+=s*2*byd;
}
} else
dc( ct[0] , ct[1], z,z, g, b);
}
}
}
// UPDATE&DRAW BALL
bx+=s*bxd;
by+=s*byd;
if(bx<0 || bx>w-z) bxd*=-1; // WALL COLLITION
if(by<0){ byd*=-1; by+=s*byd} // CEILING COLLITION
if(by>h) sp(); // LOST BALL
if( by+z>py && by<py && bx+z>px && bx<px+(z*3)){ //ON PLAYER COLLITION
byd*=-1;
by+=s*2*byd;
}
dc(bx,by,z,z,r,g);
// UPDATE&DRAW PLAYER
if (k==39){
px += s;
if(px>w-z*3)px-=s;
} if (k==37){
px -= s;
if(px<0)px+=s;
}
dc(px,py,z*3,z,g,r);
};
sp();
setInterval(gp,30);
//DRAW CUBES!
function dc(x,y,w,h,c1,c2) {
cx.fillStyle= c1;
cx.r(x-1,y-1,w+2,h+2);
cx.fillStyle= c2;
cx.r(x,y,w,h);
}
//KEYBOARD HOOKS
d.onkeydown = function(e){
k=e.keyCode;
}
d.onkeyup = function(e){
k=0;
}