-
Notifications
You must be signed in to change notification settings - Fork 1
/
game2.js
99 lines (99 loc) · 2.43 KB
/
game2.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
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
let obstacleArray = [];
let redArray = [];
class Hero {
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.height = height;
this.width = width;
}
}
/////////////////////////////
class Obstacle {
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.directionX = 2 * Math.round(Math.random()) - 1;
this.directionY = 1;
}
moveDownForever() {
setInterval(() => {
this.y += 1 * Math.random() * this.directionY;
this.x += 1 * Math.random() * this.directionX;
if (this.x >= 340) {
this.directionX = -1;
}
if (this.x <= -10) {
this.directionX = 1;
}
if (this.y > 340) {
this.directionY = -1;
}
if (this.y < 0) {
this.directionY = 1;
}
theGame.collisionDetect(theGame.theHero.x, theGame.theHero.y);
}, 2);
}
}
///////////////////////////////////////////////
class Game {
constructor() {
this.theHero = new Hero(140, 160, 50, 50);
this.score = 30;
this.numberOfBalls = 0;
this.gameOver = false;
}
writeScore() {
this.score -= 0.02;
if (this.score <= 0) {
document.getElementById(
"score1"
).innerHTML = `<span id="score1">You won!</span>`;
this.score = 0;
return;
}
document.getElementById(
"score1"
).innerHTML = `<span id="score1">Time: ${Math.round(this.score)}</span>`;
}
spawnObstacle() {
let rX = Math.floor(Math.random() * 325);
let rY = Math.floor(Math.random() * 1);
let rWidth = 60;
let rHeight = 40;
let newObstacle = new Obstacle(rX, rY, rWidth, rHeight);
this.numberOfBalls++;
obstacleArray.push(newObstacle);
newObstacle.moveDownForever();
}
moveHero(futureX, futureY) {
if (
futureX + this.theHero.width <= 400 &&
futureX >= 0 &&
futureY + this.theHero.height <= 400 &&
futureY + 0.2 * this.theHero.height >= 0
) {
this.theHero.x = futureX;
this.theHero.y = futureY;
}
if (futureX + this.theHero.width >= 380) {
this.theHero.x = futureX;
this.theHero.x -= 50;
}
}
collisionDetect(x, y) {
obstacleArray.forEach((obstacle, j) => {
if (
x + this.theHero.width >= obstacle.x &&
x <= obstacle.x + obstacle.width &&
y >= obstacle.y &&
y <= obstacle.y + obstacle.height
) {
this.gameOver = true;
}
});
}
}