-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaser.js
More file actions
171 lines (147 loc) · 4.11 KB
/
chaser.js
File metadata and controls
171 lines (147 loc) · 4.11 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const progressBar = document.querySelector("#health");
const ammoBar = document.querySelector("#ammo");
let frameNumber = 0;
function startGame() {
if (progressBar.value === 0 || ammoBar.value === 100) {
progressBar.value = 100;
ammoBar.value = 50;
Object.assign(player, { x: canvas.width / 2, y: canvas.height / 2 });
requestAnimationFrame(drawScene);
}
}
function distanceBetween(sprite1, sprite2) {
return Math.hypot(sprite1.x - sprite2.x, sprite1.y - sprite2.y);
}
function lengthBetween(sprite1, legislation) {
return Math.hypot(sprite1.x - legislation.x, sprite1.y - legislation.y);
}
function randomPoint() {
console.log(frameNumber);
legislation.x = Math.floor(550 * Math.random());
legislation.y = Math.floor(550 * Math.random());
}
function haveCollided(sprite1, sprite2) {
return distanceBetween(sprite1, sprite2) < sprite1.radius + sprite2.radius;
}
function gainPoints(sprite1, legislation) {
return (
lengthBetween(sprite1, legislation) < sprite1.radius + legislation.radius
);
}
class Sprite {
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
ctx.stroke();
}
}
class Legislation {
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.rect(
this.x - this.size / 2,
this.y - this.size / 2,
this.size,
this.size
);
ctx.fill();
ctx.stroke();
}
constructor(x, y, size, color) {
Object.assign(this, { x, y, size, color });
this.radius = size;
}
}
let legislation = new Legislation(
canvas.height / 4,
canvas.width / 3,
25,
"rgba(0, 127, 255, 0.87)"
);
class Player extends Sprite {
constructor(x, y, radius, color, speed) {
super();
Object.assign(this, { x, y, radius, color, speed });
}
}
let player = new Player(250, 150, 15, "rgba(127, 255, 0, 0.87)", 0.07);
class Enemy extends Sprite {
constructor(x, y, radius, color, speed) {
super();
Object.assign(this, { x, y, radius, color, speed });
}
}
let enemies = [
new Enemy(300, 200, 20, "rgb(255, 5, 5)", 0.02),
new Enemy(300, 250, 17, "rgb(255, 130, 5)", 0.01),
new Enemy(300, 180, 22, "rgb(255, 255, 5)", 0.002)
];
let mouse = { x: 0, y: 0 };
document.body.addEventListener("mousemove", updateMouse);
function updateMouse(event) {
const { left, top } = canvas.getBoundingClientRect();
mouse.x = event.clientX - left;
mouse.y = event.clientY - top;
}
function moveToward(leader, follower, speed) {
follower.x += (leader.x - follower.x) * speed;
follower.y += (leader.y - follower.y) * speed;
}
function updateScene() {
moveToward(mouse, player, player.speed);
enemies.forEach(enemy => moveToward(player, enemy, enemy.speed));
enemies.forEach(enemy => {
if (haveCollided(enemy, player)) {
progressBar.value -= 1;
}
});
if (gainPoints(legislation, player)) {
ammoBar.value += 2;
}
}
function clearBackground() {
ctx.fillStyle = "rgba(173, 91, 255, 0.7)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function drawStartScreen() {
ctx.fillStyle = "lightgreen";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = "Bungee";
ctx.textAllign = "center";
ctx.fillText("click to start", canvas.width / 2, canvas.height / 2);
canvas.addEventListener("click", startGame);
}
function drawScene() {
clearBackground();
player.draw();
legislation.draw();
enemies.forEach(enemy => enemy.draw());
updateScene();
frameNumber += 1;
if (frameNumber % 100 < 1) {
randomPoint();
}
if (ammoBar.value === 100) {
ctx.font = "20px Bungee";
ctx.fillStyle = "blue";
ctx.fillText("You won! Click to restart.", 30, canvas.height / 2);
} else if (progressBar.value <= 0) {
ctx.font = "20px Bungee";
ctx.fillStyle = "blue";
ctx.fillText(
"Game over. Click to restart.",
canvas.width / 4,
canvas.height / 2
);
} else {
requestAnimationFrame(drawScene);
}
}
canvas.addEventListener("click", startGame);
requestAnimationFrame(drawStartScreen);
requestAnimationFrame(drawScene);