-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
85 lines (77 loc) · 2.48 KB
/
game.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
class Game {
constructor(player, gameArea, coin, obstacle) {
this.player = player;
this.gameArea = gameArea;
this.coin = coin;
this.score = 0;
this.obstacle = obstacle;
}
startGame() {
(function () {
var requestAnimationFrame =
window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
})();
this.gameArea.start();
this.player.makePlayer();
this.coin.makeCoin();
this.obstacle.makePlayer();
this.score = 0;
}
collisionCheck() {
if (
this.player.x < this.coin.x + this.coin.radius * 2 &&
this.player.x + this.player.width > this.coin.x &&
this.player.y < this.coin.y + this.coin.radius * 2 &&
this.player.y + this.player.height > this.coin.y
) {
// collision detected!
//console.log("Game -> // collision detected!");
this.coin.moveCoin();
this.score++;
//console.log(this.score);
}
if (
this.player.x < this.obstacle.x + this.obstacle.width &&
this.player.x + this.player.width > this.obstacle.x &&
this.player.y < this.obstacle.y + this.obstacle.width &&
this.player.y + this.player.height > this.obstacle.y
) {
// collision detected!
//console.log("Game -> // collision detected!");
this.endGame();
//console.log("dead");
}
}
updateGame() {
let ctx = document.querySelector('canvas').getContext('2d');
ctx.clearRect(0, 0, 480, 200); // added in
myCoin.updateCoin();
myPlayer.update();
myObstacle.updateObstacle();
myObstacle.moveObstacle();
this.collisionCheck();
this.gameArea.updateScore(this.score);
requestAnimationFrame(() => this.updateGame());
}
endGame() {
this.player.color = 'white';
this.score = 0;
// h1 = document.querySelector("h1")
// h1.innerText = "Game Over! Refresh to Restart"
this.gameArea.endGameMessage();
cancelAnimationFrame();
console.log(score);
}
}
let myPlayer = new Player(30, 30, 'purple', 200, 120, 3, 0, 0, 0.93, 0.1); //////////added friction and gravity values
let myGameArea = new GameArea();
let myCoin = new Coin(100, 75, 5, 0, 2 * Math.PI, 'orange');
let myObstacle = new Obstacle(30, 30, 'red', 0, 90, 3, 0, 0, 1, 0);
let myGame = new Game(myPlayer, myGameArea, myCoin, myObstacle);
myGame.startGame();
myGame.updateGame();
/////////////////////////////