<canvas id="gc" width="600" height="600"></canvas>
<script>
window.onload=function() {
canv=document.getElementById("gc");
ctx=canv.getContext("2d");
document.addEventListener("keydown",keyPush);
setInterval(game,1000/10);
setInterval(superblocks,2000);
setInterval(obstaclemove,4550);
}
px=py=13;
gs=20;
tc=30;
ax=Math.floor(Math.random()*tc);
ay=Math.floor(Math.random()*tc);
apx=Math.floor(Math.random()*tc);
apy=Math.floor(Math.random()*tc);
porx=Math.floor(Math.random()*tc);
pory=Math.floor(Math.random()*tc);
sx=Math.floor(Math.random()*tc);
sy=Math.floor(Math.random()*tc);
ox=Math.floor(Math.random()*tc);
oy=Math.floor(Math.random()*tc);
obx=Math.floor(Math.random()*tc);
oby=Math.floor(Math.random()*tc);
obsx=Math.floor(Math.random()*tc);
obsy=Math.floor(Math.random()*tc);
obstx=Math.floor(Math.random()*tc);
obsty=Math.floor(Math.random()*tc);
score=0;
highscore=0;
scoreten=false;
xv=yv=0;
trail=[];
tail = 4;
function game() {
px+=xv;
py+=yv;
if(px<0) {
tail = 4;
xv=0;yv=0;
px=13;py=13;
if(score>highscore) {
highscore=score;
}
}
if(px>tc-1) {
tail = 4;
xv=0;yv=0;
px=13;py=13;
if(score>highscore) {
highscore=score;
}
}
if(py<0) {
tail = 4;
xv=0;yv=0;
px=13;py=13;
if(score>highscore) {
highscore=score;
}
}
if(py>tc-1) {
tail = 4;
xv=0;yv=0;
px=13;py=13;
if(score>highscore) {
highscore=score;
}
}
document.getElementById("score").innerHTML ="Score: " + score;
document.getElementById("score").style.font = "bold 25px arial";
document.getElementById("highscore").innerHTML ="Highscore: " + highscore;
document.getElementById("highscore").style.font = "bold 20px arial";
ctx.fillStyle="green";
ctx.fillRect(0,0,canv.width,canv.height);
ctx.fillStyle="lime";
for(var i=0;i<trail.length;i++) {
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
if(trail[i].x==px && trail[i].y==py) {
tail = 4;
xv=0;yv=0;
px=13;py=13;
if(score>highscore) {
highscore=score;
}
}
}
trail.push({x:px,y:py});
while(trail.length>tail) {
trail.shift();
}
if(ax==px && ay==py) {
tail++;
score++;
ax=Math.floor(Math.random()*tc);
ay=Math.floor(Math.random()*tc);
}
if(apx==px && apy==py) {
tail++;
score++;
apx=Math.floor(Math.random()*tc);
apy=Math.floor(Math.random()*tc);
}
ctx.fillStyle="red";
ctx.fillRect(ax*gs,ay*gs,gs-2,gs-2);
ctx.fillStyle="red";
ctx.fillRect(apx*gs,apy*gs,gs-2,gs-2);
if (px==porx && py==pory) {
px=Math.floor(Math.random()*tc);
py=Math.floor(Math.random()*tc);
porx=Math.floor(Math.random()*tc);
pory=Math.floor(Math.random()*tc);
}
ctx.fillStyle="white";
ctx.fillRect(porx*gs,pory*gs,gs-2,gs-2);
ctx.fillStyle="darkslategray";
ctx.fillRect(ox*gs,oy*gs,gs-2,gs-2);
ctx.fillStyle="darkslategray";
ctx.fillRect(obx*gs,oby*gs,gs-2,gs-2);
ctx.fillStyle="darkslategray";
ctx.fillRect(obstx*gs,obsty*gs,gs-2,gs-2);
ctx.fillStyle="darkslategray";
ctx.fillRect(obsx*gs,obsy*gs,gs-2,gs-2);
ctx.fillStyle="gold";
ctx.fillRect(sx*gs,sy*gs,gs-2,gs-2);
if(px==sx && py==sy) {
tail++;
tail++;
score++;
score++;
sx=Math.floor(Math.random()*tc);
sy=Math.floor(Math.random()*tc);
}
if(px==ox && py==oy) {
tail = 4;
xv=0;yv=0;
px=py=13;
if(score>highscore) {
highscore=score;
}
}
if(px==obsx && py==obsy) {
tail = 4;
xv=0;yv=0;
px=py=13;
if(score>highscore) {
highscore=score;
}
}
if(px==obx && py==oby) {
tail = 4;
xv=0;yv=0;
px=py=13;
if(score>highscore) {
highscore=score;
}
}
if(px==obstx && py==obsty) {
tail = 4;
xv=0;yv=0;
px=py=13;
if(score>highscore) {
highscore=score;
}
}
document.getElementById("credits").innerHTML ="Programmed by: Dario Casciato";
document.getElementById("credits").style.font = "10px arial"
}
function keyPush(evt) {
switch(evt.keyCode) {
case 37:
if(xv==0 && yv==0) {
score=0;
}
xv=-1;yv=0;
break;
case 38:
if(xv==0 && yv==0) {
score=0;
}
xv=0;yv=-1;
break;
case 39:
if(xv==0 && yv==0) {
score=0;
}
xv=1;yv=0;
break;
case 40:
if(xv==0 && yv==0) {
score=0;
}
xv=0;yv=1;
break;
case 65:
tail++;
score++;
break;
}
}
function superblocks() {
sx=Math.floor(Math.random()*tc);
sy=Math.floor(Math.random()*tc);
}
function obstaclemove() {
obstchoice=Math.floor(Math.random() * 4);
obstachoice=obstchoice=Math.floor(Math.random() * 4);
if(obstchoice==0) {
if(obstachoice==0) {
ox++;
}
if(obstachoice==1) {
ox--;
}
if(obstachoice==2) {
oy++;
}
if(obstachoice==3) {
oy--;
}
}
if(obstchoice==1) {
if(obstachoice==0) {
obx++;
}
if(obstachoice==1) {
obx--;
}
if(obstachoice==2) {
oby++;
}
if(obstachoice==3) {
oby--;
}
}
if(obstchoice==2) {
if(obstachoice==0) {
obsx++;
}
if(obstachoice==1) {
obsx--;
}
if(obstachoice==2) {
obsy++;
}
if(obstachoice==3) {
obsy--;
}
}
if(obstchoice==3) {
if(obstachoice==0) {
obstx++;
}
if(obstachoice==1) {
obstx--;
}
if(obstachoice==2) {
obsty++;
}
if(obstachoice==3) {
obsty--;
}
}
}
</script>
<p id="score"></p>
<p id="highscore"></p>
<p id="credits"></p>