Remake of Tron's light cycles game. Left and right arrow key to control blue player. Don't hit the walls or another player.
var s=c.width=c.height=256;var m=s/2;var l=64;a.fillRect(0,0,c.width,c.height);var d,px={};var p=[new P(l,m,1,0,0),new P(s-l,m,-1,0,1),new P(s-m,l,0,1,2),new P(s-m,s-l,0,-1,3)];setInterval(function(){d=a.getImageData(0,0,s,s);for(var e=0;e<p.length;e++){var b=p[e];b.u(d);if(e>0){if(Math.random()<0.01||b.a()){if(Math.random()<0.5){b.r()}else{b.l()}}}}a.putImageData(d,0,0)},33);function gp(b,e){if(!px[b]){px[b]=[]}if(e==0||e==s||b==0||b==s){return 1}return px[b][e]}function P(b,i,h,f,g){var e=this;e.x=b;e.y=i;e.sx=h;e.sy=f;e.c=g;e.u=function(k){if(!px[e.x]){px[e.x]=[]}px[e.x][e.y]=1;g=[[0,128,255],[255,0,0],[0,255,0],[255,255,0]][e.c];e.x+=e.sx;e.y+=e.sy;if(gp(e.x,e.y)){e.sx=0;e.sy=0}if(e.sx+e.sy){var j=(e.x+e.y*s)*4;k.data[j]=g[0];k.data[j+1]=g[1];k.data[j+2]=g[2]}};e.r=function(){nx=-e.sy;ny=e.sx;e.sx=nx;e.sy=ny};e.l=function(){nx=e.sy;ny=-e.sx;e.sx=nx;e.sy=ny};e.a=function(){ax=e.x+e.sx*8;ay=e.y+e.sy*8;return gp(ax,ay)}}document.onkeydown=function(b){if(b.keyCode==39){p[0].r()}if(b.keyCode==37){p[0].l()}};
dmFyIHM9Yy53aWR0aD1jLmhlaWdodD0yNTY7dmFyIG09cy8yO3ZhciBsPTY0O2EuZmlsbFJlY3QoMCwwLGMud2lkdGgsYy5oZWlnaHQpO3ZhciBkLHB4PXt9O3ZhciBwPVtuZXcgUChsLG0sMSwwLDApLG5ldyBQKHMtbCxtLC0xLDAsMSksbmV3IFAocy1tLGwsMCwxLDIpLG5ldyBQKHMtbSxzLWwsMCwtMSwzKV07c2V0SW50ZXJ2YWwoZnVuY3Rpb24oKXtkPWEuZ2V0SW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i5rZXlDb2RlPT0zNyl7cFswXS5sKCl9fTs=
var s = c.width = c.height = 256; //Size
var m = s / 2; //Half size
var l = 64; //Small unit to position players
a.fillRect(0, 0, c.width, c.height); //Black background
var d, //Pixel data
px = {}; //Collision data
var p = [ //Players
new P(l, m, 1, 0, 0),
new P(s - l, m, -1, 0, 1),
new P(s - m, l, 0, 1, 2),
new P(s - m, s - l, 0, -1, 3)
];
setInterval(function() {
d = a.getImageData(0, 0, s, s); //Get pixels
for(var i = 0; i < p.length; i++) {
var v = p[i];
v.u(d); //Update player
if (i > 0) { //For NPCs
if (Math.random() < 0.01 || v.a()) //If wall ahead or random chance
if (Math.random() < 0.5)
v.r(); //Turn right
else
v.l(); //Turn left
}
}
a.putImageData(d, 0, 0); //Set pixels
}, 33);
function gp(x, y) { //Check for collision
if (!px[x]) //Create 2nd dimmension if doesn't exist
px[x] = []; //Creat if not
if (y == 0 || y == s || x == 0 || x == s) //Exterior walls
return 1;
return px[x][y];
}
function P(x, y, sx, sy, c) { //Player
var t = this;
t.x = x; //x pos
t.y = y; //y pos
t.sx = sx; //x speed
t.sy = sy; //y speed
t.c = c; //color
t.u = function(d) { //update
if (!px[t.x]) //Create 2nd dimmension if doesn't exist
px[t.x] = []; //Creat if not
px[t.x][t.y] = 1; //Set collision
c = [[0, 128, 255],[255, 0, 0],[0, 255, 0],[255, 255, 0]][t.c]; //Colors
//Move
t.x += t.sx;
t.y += t.sy;
//If hit something stop moving
if (gp(t.x, t.y)) {
t.sx = 0;
t.sy = 0;
}
//Set pixel data
if (t.sx + t.sy) {
var i = (t.x + t.y * s) * 4;
d.data[i] = c[0]; //red
d.data[i + 1] = c[1]; //green
d.data[i + 2] = c[2]; //blue
}
}
//Turn right
t.r = function() {
nx = -t.sy;
ny = t.sx;
t.sx = nx;
t.sy = ny;
}
//Turn left
t.l = function() {
nx = t.sy;
ny = -t.sx;
t.sx = nx;
t.sy = ny;
}
//Check for collisions ahead
t.a = function() {
ax = t.x + t.sx * 8;
ay = t.y + t.sy * 8;
return gp(ax, ay);
}
}
//Player controls
document.onkeydown = function(e) {
if (e.keyCode == 39) p[0].r(); //Right key
if (e.keyCode == 37) p[0].l(); //Left key
}