var c;
var ctx;
var enemynum;
var enemycounter;

var FPS;
var player;
var enemies = []; 

function Initialise()
{
  if(c == null)
  {
    c=document.getElementById("canvas");
    ctx=c.getContext("2d");
    enemynum = 0;
    enemycounter=100;
    FPS = 60;
    canvas.addEventListener('mousemove', MouseMove, false);
    setInterval(function() {
    update();
    draw();
    }, 1000/FPS);  
  }
    player = {
    color: "#00A",
    x: 220,
    y: 270,
    width: 32,
    height: 32,
    draw: function() {
    ctx.fillStyle = this.color;
    ctx.fillRect(this.x, this.y, this.width, this.height);
  }
};
}
  


function draw(){
ctx.clearRect(0, 0, 400, 400);
player.draw();
for(var i = 0; i<enemynum; i++)
  {
    enemies[i].y+=2;
    enemies[i].draw();
    collisions(i);  // Check collisions with current enemy.
  }
}


function collisions(i){
   
  var e = enemies[i];
  var p = player
  
  // Top Left
  
  if(p.x<e.hole && (p.y<(e.y+e.height)&&p.y>e.y))
  {
     document.getElementById("testout").innerHTML=String("TL Collide");
  }
  
  // Top Right
  
  if((p.x+p.width)>(e.hole+p.width+e.extragap) && ((p.y)<(e.y+e.height)&&p.y>e.y))
  {
     document.getElementById("testout").innerHTML=String("TR Collide");
  }  
  
  // Bottom Left
  
  if(p.x<e.hole && ((p.y+p.height)<(e.y+e.height)&&(p.y+p.height)>e.y))
  {
     document.getElementById("testout").innerHTML=String("BL Collide");
  }
  
  // Bottom Right
  
  if((p.x+p.width)>(e.hole+p.width+e.extragap) && ((p.y+p.height)<(e.y+e.height)&&(p.y+p.height)>e.y))
  {
     document.getElementById("testout").innerHTML=String("BR Collide");
  }
}



function update(){
  // Add only 4 enemies to the screen
  if(enemynum<4)
  {
    enemycounter++;
    if(enemycounter > 50)
    // Spacing of enemies
    {
      enemycounter = 0;
      enemynum++;
    }  
  }
  
  for(var i = 0; i<enemynum; i++)
  {
    enemies.push({
    color: "#0F0",
    x: 0,
    y: -20,
    width: 400,
    height: 20,
    extragap: 20,
    hole: Math.floor(Math.random()*380),
    draw: function() {
      if(this.y>400)
      {
        this.hole = Math.floor(Math.random()*this.width-20);
        this.y=-this.height;
      }
      ctx.fillStyle = this.color;
      ctx.fillRect(this.x, this.y, this.hole, this.height);
      ctx.fillRect(this.hole+player.width+this.extragap, this.y, this.width, this.height);
      }
    });
  }
}


function MouseMove(e){

player.x = e.clientX-canvas.offsetLeft-player.height/2;
document.getElementById("testout").innerHTML=String("");
player.y = e.clientY-canvas.offsetTop-player.height/2+$(document).scrollTop(); 


}

@@ -40,16 +40,27 @@
 counter;
+%0Avar score;
 %0A%0Avar FP
@@ -118,16 +118,29 @@
 ise()%0A%7B%0A
+  score = 0;%0A
   if(c =
@@ -1046,32 +1046,49 @@
 (%22TL Collide%22);%0A
+     Finalise();%0A
   %7D%0A  %0A  // Top 
@@ -1255,16 +1255,33 @@
 lide%22);%0A
+     Finalise();%0A
   %7D  %0A  
@@ -1445,24 +1445,41 @@
  Collide%22);%0A
+     Finalise();%0A
   %7D%0A  %0A  // 
@@ -1673,16 +1673,33 @@
 lide%22);%0A
+     Finalise();%0A
   %7D%0A%7D%0A%0A%0A
@@ -1718,16 +1718,30 @@
 date()%7B%0A
+  %0A  score++;%0A
   // Add
@@ -2610,10 +2610,13 @@
 ing(
-%22%22
+score
 );%0Ap
@@ -2695,11 +2695,79 @@
 op(); %0A%0A
+%7D%0A%0Afunction Finalise()%7B%0A  %0A  alert(%22You lost, but scored %22 + score);
 %0A%7D%0A

Update games.js