-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbadCircle.js
More file actions
61 lines (53 loc) · 1.92 KB
/
badCircle.js
File metadata and controls
61 lines (53 loc) · 1.92 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
import { getRandomSpawn, getRandomNegMovement } from './helper';
class Circle {
constructor(ctx, canvas, player) {
this.ctx = ctx;
this.player = player;
this.canvas = canvas;
this.dx = getRandomNegMovement();
this.dy = getRandomNegMovement();
this.parameters = {
x: this.setInitialX(),
y: this.setInitialY(),
radius: 10
}
}
setInitialX() {
let initX = getRandomSpawn(600, 120);
if (initX === this.player.parameters.x + this.player.parameters.radius + 10 || initX === this.player.parameters.x - this.player.parameters.radius + 10) {
return initX = getRandomSpawn(600, 120);
} else {
return initX;
}
}
setInitialY() {
let initY = getRandomSpawn(600, 120);
if (initY === this.player.parameters.y + this.player.parameters.radius + 10 || initY === this.player.parameters.y - this.player.parameters.radius + 10) {
return initY = getRandomSpawn(600, 120);
} else {
return initY;
}
}
drawCircle() {
let { x, y, radius } = this.parameters;
this.ctx.beginPath();
this.ctx.arc(x, y, radius, 0, Math.PI*2);
this.ctx.fillStyle = "green";
this.ctx.fill();
this.ctx.closePath();
}
move() {
if (this.parameters.x + this.dx > this.canvas.width - this.parameters.radius || this.parameters.x + this.dx < this.parameters.radius && this.dx !== 0) {
this.dx = -this.dx;
} else if (this.parameters.y + this.dy > this.canvas.height - this.parameters.radius || this.parameters.y + this.dy < this.parameters.radius && this.dy !== 0) {
this.dy = -this.dy;
}
this.parameters.x += this.dx;
this.parameters.y += this.dy;
}
collision() {
this.dy = -this.dy + 1;
this.dx = -this.dx + 1;
}
}
export default Circle;