-
Notifications
You must be signed in to change notification settings - Fork 1
/
game3.js
101 lines (97 loc) · 2.41 KB
/
game3.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
100
101
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() {
console.log("test");
let x = setInterval(() => {
let increment = 3;
this.y = this.y + increment;
let possibleX =
this.x +
this.directionX *
Math.random() *
Math.pow(Math.random() * increment, Math.random() * increment);
if (possibleX >= 340 && this.y < 400 && this.y >= 275) {
this.x = 300;
increment *= 1;
} else if (possibleX >= 340) {
this.x = 340 * Math.random();
increment *= 1;
} else if (possibleX <= -10) {
this.x = 340 * Math.random();
increment *= 1;
} else {
this.x = possibleX;
}
if (this.y > 400) {
spawn = false;
clearInterval(x);
}
theGame.collisionDetect(theGame.theHero.x, theGame.theHero.y);
}, 2);
}
}
///////////////////////////////////////////////
class Game {
constructor() {
this.theHero = new Hero(150, 340, 50, 50);
this.score = 0;
this.numberOfBalls = 0;
this.gameOver = false;
}
writeScore() {
document.getElementById("score1").innerHTML = `${this.score}`;
}
spawnObstacle() {
//spawn=true;
let rX = 175;
let rY = 0;
let rWidth = 80;
let rHeight = 60;
let newObstacle = new Obstacle(rX, rY, rWidth, rHeight);
this.numberOfBalls++;
obstacleArray.push(newObstacle);
newObstacle.moveDownForever();
}
moveHero(futureX, futureY) {
if (
futureX + this.theHero.width <= 380 &&
futureX >= 0 &&
futureY + this.theHero.height <= 400 &&
futureY + 0.2 * this.theHero.height >= 350
) {
this.theHero.x = futureX;
this.theHero.y = futureY;
}
}
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.score += 10;
obstacleArray.splice(j, 1);
}
});
}
}