-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
204 lines (179 loc) · 5.53 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import Player from "./Player.js";
import Ground from "./Ground.js";
import CactusController from "./CactusController.js";
import Score from "./Score.js";
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
const GAME_SPEED_START = 1;
const GAME_SPEED_INCREMENT = 0.00001;
const GAME_WIDTH = 800;
const GAME_HEIGHT = 200;
const PLAYER_WIDTH = 88 / 1.5;
const PLAYER_HEIGHT = 94 / 1.5;
const MAX_JUMP_HEIGHT = GAME_HEIGHT;
const MIN_JUMP_HEIGHT = 150;
const GROUND_WIDTH = 2400;
const GROUND_HEIGHT = 24;
const GROUND_AND_CACTUS_SPEED = 0.5;
const CACTI_CONFIG = [
{ width: 48 / 1.5, height: 100 / 1.5, image: "img/cactus_1.png" },
{ width: 98 / 1.5, height: 100 / 1.5, image: "img/cactus_2.png" },
{ width: 68 / 1.5, height: 70 / 1.5, image: "img/cactus_3.png" },
];
//Game Objects
let player = null;
let ground = null;
let cactiController = null;
let score = null;
let scaleRatio = null;
let previousTime = null;
let gameSpeed = GAME_SPEED_START;
let gameOver = false;
let hasAddedEventListenersForRestart = false;
let waitingToStart = true;
// Create all Sprites
function createSprites() {
const playerWidthInGame = PLAYER_WIDTH * scaleRatio;
const playerHeightInGame = PLAYER_HEIGHT * scaleRatio;
const minJumpHeightInGame = MIN_JUMP_HEIGHT * scaleRatio;
const maxJumpHeightInGame = MAX_JUMP_HEIGHT * scaleRatio;
const groundWidthInGame = GROUND_WIDTH * scaleRatio;
const groundHeightInGame = GROUND_HEIGHT * scaleRatio;
player = new Player(
ctx,
playerWidthInGame,
playerHeightInGame,
minJumpHeightInGame,
maxJumpHeightInGame,
scaleRatio
);
ground = new Ground(
ctx,
groundWidthInGame,
groundHeightInGame,
GROUND_AND_CACTUS_SPEED,
scaleRatio
);
const cactiImages = CACTI_CONFIG.map((cactus) => {
const image = new Image();
image.src = cactus.image;
return {
image: image,
width: cactus.width * scaleRatio,
height: cactus.height * scaleRatio,
};
});
cactiController = new CactusController(
ctx,
cactiImages,
scaleRatio,
GROUND_AND_CACTUS_SPEED
);
score = new Score(ctx, scaleRatio);
}
function setScreen() {
scaleRatio = getScaleRatio();
canvas.width = GAME_WIDTH * scaleRatio;
canvas.height = GAME_HEIGHT * scaleRatio;
createSprites();
}
setScreen();
window.addEventListener("resize", () => setTimeout(setScreen, 200));
if (screen.orientation) {
screen.orientation.addEventListener("change", setScreen);
}
function getScaleRatio() {
const screenHeight = Math.min(
window.innerHeight,
document.documentElement.clientHeight
);
const screenWidth = Math.min(
window.innerWidth,
document.documentElement.clientWidth
);
// Window is wider than the game width
if (screenWidth / screenHeight < GAME_WIDTH / GAME_HEIGHT) {
return screenWidth / GAME_WIDTH;
} else {
return screenHeight / GAME_HEIGHT;
}
}
function showGameOver() {
const fontSize = 72 * scaleRatio;
ctx.font = `${fontSize}px Verdana`;
ctx.fillStyle = "grey";
const x = canvas.width / 4.5;
const y = canvas.height / 2;
ctx.fillText("GAME OVER!", x, y);
}
function setupGameReset() {
if (!hasAddedEventListenersForRestart) {
hasAddedEventListenersForRestart = true;
setTimeout(() => {
window.addEventListener("keyup", reset, { once: true });
window.addEventListener("touchstart", reset, { once: true });
}, 1000);
}
}
function reset() {
hasAddedEventListenersForRestart = false;
gameOver = false;
waitingToStart = false;
ground.reset();
cactiController.reset();
score.reset();
gameSpeed = GAME_SPEED_START;
}
function showStartGameText() {
const fontSize = 40 * scaleRatio;
ctx.font = `${fontSize}px Verdana`;
ctx.fillStyle = "grey";
const x = canvas.width / 14;
const y = canvas.height / 2;
ctx.fillText("Tap Screen or Press Space To Start", x, y);
}
function updateGameSpeed(frameTimeDelta) {
gameSpeed += frameTimeDelta * GAME_SPEED_INCREMENT;
}
function clearScreen() {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function gameLoop(currentTime) {
if (previousTime === null) {
previousTime = currentTime;
requestAnimationFrame(gameLoop);
return;
}
const frameTimeDelta = currentTime - previousTime;
previousTime = currentTime;
clearScreen();
if (!gameOver && !waitingToStart) {
//Update game objects
ground.update(gameSpeed, frameTimeDelta);
cactiController.update(gameSpeed, frameTimeDelta);
player.update(gameSpeed, frameTimeDelta);
score.update(frameTimeDelta);
updateGameSpeed(frameTimeDelta);
}
if (!gameOver && cactiController.collideWith(player)) {
gameOver = true;
setupGameReset();
score.setHighScore();
}
//Draw game objects
ground.draw();
cactiController.draw();
player.draw();
score.draw();
if (gameOver) {
showGameOver();
}
if (waitingToStart) {
showStartGameText();
}
requestAnimationFrame(gameLoop);
}
requestAnimationFrame(gameLoop);
window.addEventListener("keyup", reset, { once: true });
window.addEventListener("touchstart", reset, { once: true });