AB.drawRunControls = false;
var rek;
var enemies = [];
var play = false;
var score = 0;
var compte;
function setup()
{
createCanvas(800,800);
colorMode(HSB);
rek = new Player(400,700);
enemies.push(new Enemy());
enemies.push(new Enemy());
enemies.push(new Enemy());
}
function compter()
{
score += 1;
compte = setTimeout(compter, 1000);
$("#user_span1").html("Score " + score);
}
function draw()
{
background(10);
if(play)
{
rek.updatePos();
for (i=0 ; i<enemies.length; i++)
{
enemies[i].update();
enemies[i].show();
if(rek.collision(enemies[i]))
{
rek.life--;
enemies[i].reset();
}
}
if (rek.life === 0)
{
play = false;
clearTimeout(compte);
$("#user_span1").html("Score final : " + score);
}
}
rek.draw();
}
function Player(x,y)
{
this.x = x;
this.y = y;
this.life = 10;
this.collision = function(e)
{
return (dist(this.x, this.y, e.x, e.y) < 30);
}
this.draw = function()
{
fill(0,0,0);
rect(this.x-15,this.y-15,30,30);
fill(0,255,250);
rect(this.x-15, this.y+15 - this.life*3, 30, this.life*3);
}
this.updatePos = function()
{
this.x = constrain(mouseX, 15, width - 15);
this.y = constrain(mouseY, 15, height - 15);
}
}
function mousePressed()
{
if(mouseX<rek.x + 15 && mouseX>rek.x - 15 && mouseY <rek.y + 15 && mouseY > rek.y - 15 && play === false)
{
play = true;
compter();
}
}
function update()
{
rek.x += 0.1;
}
function Enemy()
{
this.direction = floor(random(4));
if (this.direction === 0)
{
this.x = random(width);
this.y = random(height, height + 100);
}
if (this.direction === 1)
{
this.x = random(-100,0);
this.y = random(height);
}
if (this.direction === 2)
{
this.x = random(width);
this.y = random(-100, 0);
}
if (this.direction === 3)
{
this.x = random(width, width + 100);
this.y = random(height);
}
this.vel = random(2,6);
this.reset = function() {
this.direction = floor(random(4));
if (this.direction === 0)
{
this.x = random(width);
this.y = random(height, height + 100);
}
if (this.direction === 1)
{
this.x = random(-100,0);
this.y = random(height);
}
if (this.direction === 2)
{
this.x = random(width);
this.y = random(-100, 0);
}
if (this.direction === 3)
{
this.x = random(width, width + 100);
this.y = random(height);
}
this.vel = random(3,7);
if (random(1) < 0.3) enemies.push(new Enemy());
};
this.update = function()
{
if (this.direction === 0)
{
this.y -= this.vel;
if(this.y < -50)
{
this.reset();
}
}
if (this.direction === 1)
{
this.x += this.vel;
if(this.x > width + 50)
{
this.reset();
}
}
if (this.direction === 2)
{
this.y += this.vel;
if(this.y > height+50)
{
this.reset();
}
}
if (this.direction === 3)
{
this.x -= this.vel;
if(this.x < -50)
{
this.reset();
}
}
}
this.show = function()
{
fill(noise(this.x/500,this.y/500)*255,250,250);
ellipse(this.x,this.y,30,30);
}
}