-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
69 lines (58 loc) · 1.35 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var gameArea = document.getElementById("gameArea");
var ctx = gameArea.getContext("2d");
//for position of mouse
var border = gameArea.getBoundingClientRect();
var pos = {x: 100, y:100};
var i = 0;
var run = setInterval(update, 10)
function update(){
ctx.clearRect(0, 0, 640, 480);
follow(player, pos, 15, 5);
player.update();
enemy.update();
if (i > 100) {
follow(enemy, player, 0, 3);
//losing
if (enemy.x < player.x + 30 && player.x < enemy.x + 30 &&
enemy.y < player.y + 30 && player.y < enemy.y + 30)
{
window.alert("your score:" + i);
i = 0;
player.x = 100;
player.y = 100;
enemy.x = 100;
enemy.y = 100;
pos = {x: 100, y:100};
}
}
i += 1;
}
function component(width, height, color){
this.x = 100;
this.y = 100;
this.update = function () {
square = ctx;
square.fillStyle = color;
square.fillRect(this.x, this.y, width, height);
}
}
var player = new component(30, 30, "red");
var enemy = new component(30, 30, "green");
function showCoords(evt){
return{
x: evt.clientX - border.left,
y: evt.clientY - border.top
}
}
function follow(object, target, dpos, spd) {
if (object.x > target.x - dpos) {
object.x -= spd;
}else {
object.x += spd;
}
if (object.y > target.y - dpos) {
object.y -= spd;
}else {
object.y += spd;
}
}