Skip to content

Instantly share code, notes, and snippets.

@localdevm
Created June 1, 2016 10:51
Show Gist options
  • Save localdevm/3c0bc90cdc97259018bf15634575d253 to your computer and use it in GitHub Desktop.
Save localdevm/3c0bc90cdc97259018bf15634575d253 to your computer and use it in GitHub Desktop.
var canvas;
var ctx;
function heks(x,y)
{
this.x = x;
this.y = y;
this.Teken = function(ctx)
{
var img = new Image();
img.src = "heks.png";
ctx.drawImage(img,this.x,this.y);
}
}
function ster(x,y)
{
this.x = x;
this.y = y;
var vy = 1;
this.vy = vy;
this.Teken = function(ctx)
{
var img = new Image();
img.src = "ster.png";
ctx.drawImage(img,this.x,this.y);
}
}
var score = 0;
var heks1 = new heks(10,300);
var sterren = [];
for( var i = 0; i < 10; i++){
sterren[i]= new ster(i *100,((i*10) + 50));
};
$(function(){
canvas = document.getElementById("myCanvas");
ctx = canvas.getContext("2d");
$("body").mousemove(function (arg)
{
heks1.y = arg.pageY -60;
heks1.x = arg.pageX -120;
if(heks1.x > 850)
heks1.x = 850;
if (heks1.x < -10)
heks1.x = -10;
if(heks1.y > 400)
heks1.y = 400;
if(heks1.y < 300)
heks1.y = 300;
})
Animate();
});
function Animate(){
//clear
ctx.clearRect(0,0,1000,500);
//update
for (var i=0;i<sterren.length; i++){
sterren[i].y += sterren[i].vy;
var dx = ((sterren[i].x + 50) / 2) - ((heks1.x + 110) / 2);
console.log(heks1.y)
var dy = ((sterren[i].y + 50) / 2) - ((heks1.y + 100) / 2);
var afstand = Math.sqrt((dx*dx) + (dy*dy));
var straalSter = 10;
var straalHeks = 25;
if(afstand < (straalSter + straalHeks)){
sterren[i].y = - 50;
sterren[i].vy += 0.75;
score += 1;
//$("h2").add(score);
$("h2").clear;
$("h2").append(score);
};
if(score == 100){
alert("Congrats you won!");
}
};
//draw
heks1.Teken(ctx);
for( var i = 0; i < 10; i++){
sterren[i].Teken(ctx);
};
setTimeout(Animate,20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment