-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
252 lines (222 loc) · 5.84 KB
/
script.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// retrieve data from local storage
const retrieveHighScore = () => {
const retrievedScore = localStorage.getItem("highScore");
return localStorage.getItem("highScore") ?
JSON.parse(retrievedScore).score :
0;
};
// game varibales
const ctxBg = "#000";
const width = canvas.width;
const height = canvas.height;
let isNewGame = true;
let countScoreInterval;
let pulsatingText;
let jumpAnimation;
let gameAnimation;
let isJumping;
let dy;
// road
const roadY = (height * 3) / 4;
const roadColor = "#fff";
//dino
const dinoImg = document.getElementById("dinoImg");
const dinoWidth = 50;
const dinoHeight = 50;
const dinoX = 50;
let dinoColor = "#00f";
let dinoY = roadY - dinoHeight;
// obstacles
const obsImg1 = new Image();
const obsImg2 = new Image();
obsImg1.src = "./Assets/obstacle1.png";
obsImg2.src = "./Assets/obstacle3.png";
const obsWidth = 30;
const obsHeight = 50;
const obsColor = "#f00";
let obsX = width;
let obsY = roadY - obsHeight;
let obsDx;
// score
let score = 0;
let dScore = 1;
let highScore = retrieveHighScore();
let isGameOver = false;
const liveScoreX = 20;
const liveScoreY = 30;
const highScoreX = width - 20;
const highScoreY = 30;
// Calculates score and obstacle speed
const counter = () => {
countScoreInterval = setInterval(() => {
score += dScore;
dScore += 0.002 * dScore;
obsDx += 0.002 * obsDx;
if (score >= highScore) {
highScore = score;
}
}, 100);
};
// Pulsating Effect
const beginPulse = () => {
let pulseColor = "#fff";
// renders Score Text
if (!isNewGame) {
ctx.fillStyle = pulseColor;
ctx.font = "16px Monospace";
ctx.textAlign = "center";
ctx.fillText(`SCORE : ${Math.floor(score)}`, width / 2, height / 4);
ctx.fillText(
`HIGHSCORE : ${Math.floor(highScore)}`,
width / 2,
height / 4 + 20
);
}
pulsatingText = setInterval(() => {
// Cover
ctx.fillStyle = ctxBg;
ctx.fillRect(width / 5, height / 3, (width * 3) / 5, 35);
// changes pulse color
pulseColor = pulseColor === "#fff" ? "#000" : "#fff";
// renders text
const text = isNewGame ? "START" : "RESTART";
ctx.fillStyle = pulseColor;
ctx.font = "italic 20px Monospace";
ctx.textAlign = "center";
ctx.fillText(`PRESS SPACEBAR TO ${text}`, width / 2, height / 3 + 25);
}, 250);
};
// Algorithm to make dino jump
const jump = () => {
dinoY -= dy;
dy -= 1;
if (dinoY >= roadY - dinoHeight) {
cancelAnimationFrame(jumpAnimation);
isJumping = false;
dy = 15;
return;
}
jumpAnimation = requestAnimationFrame(jump);
};
// Obstacle velocity
const obsVelo = () => {
obsX -= obsDx;
if (obsX + obsWidth <= 0) {
obsX = width;
}
};
// detect collision
const checkCollision = () => {
//
if (dinoX < obsX + obsWidth &&
dinoX + dinoWidth > obsX &&
dinoY < obsY + obsHeight &&
dinoY + dinoHeight > obsY) {
return true;
}
return false;
};
// paints the game on canvas
const renderCanvas = () => {
// console.log("RENDER CANVAS");
ctx.clearRect(0, 0, width, height);
// canvas background
ctx.fillStyle = ctxBg;
ctx.fillRect(0, 0, width, height);
// road
ctx.beginPath();
ctx.moveTo(0, roadY);
ctx.lineTo(width, roadY);
ctx.strokeStyle = roadColor;
ctx.stroke();
// dino Character
ctx.fillStyle = dinoColor;
ctx.drawImage(dinoImg, dinoX, dinoY, dinoWidth, dinoHeight);
// ctx.fillRect(dinoX, dinoY, dinoWidth, dinoHeight);
// score
ctx.fillStyle = "#fff";
ctx.font = "14px Monospace";
ctx.textAlign = "left";
ctx.fillText(`SCORE : ${Math.floor(score)}`, liveScoreX, liveScoreY);
ctx.font = "14px Monospace";
ctx.textAlign = "right";
ctx.fillText(`HIGHSCORE: ${Math.floor(highScore)}`, highScoreX, highScoreY);
// obstacles
ctx.fillStyle = obsColor;
ctx.drawImage(obsImg1, obsX, obsY, obsWidth, obsHeight);
// ctx.drawImage(obsImg2, obsX + 100, obsY, obsWidth * 2, obsHeight);
// ctx.fillRect(obsX, obsY, obsWidth, obsHeight);
};
// Save to localStorage
const saveToStorage = () => {
if (score < highScore) {
return;
}
// Getting preferred date format
let dateEl = new Date();
const time = dateEl.toLocaleString("en-IN", { timeZone: "Asia/Calcutta" });
// console.log(dateStr);
score = Math.floor(score);
// Making Local Storage
const highScoreObj = { time, score };
localStorage.setItem("highScore", JSON.stringify(highScoreObj));
console.log("Saved");
};
// Listening to jump buttons
window.addEventListener("keydown", (e) => {
if (isJumping) {
return;
}
if ((isGameOver || isNewGame) && e.code === "Space") {
isNewGame = false;
isGameOver = false;
init();
return;
}
isJumping = true;
jump();
});
const resetValues = () => {
// initial values
dy = 15;
isJumping = false;
dinoY = roadY - dinoHeight;
obsX = width;
obsDx = 5;
score = 0;
dScore = 1;
clearInterval(pulsatingText);
};
const firstScreen = () => {
renderCanvas();
beginPulse();
};
// Initialize game
const init = () => {
resetValues();
renderGame();
counter();
};
// Renders game
const renderGame = () => {
renderCanvas();
obsVelo();
if (checkCollision()) {
cancelAnimationFrame(gameAnimation);
terminate();
return;
}
gameAnimation = requestAnimationFrame(renderGame);
};
const terminate = () => {
clearInterval(countScoreInterval);
saveToStorage();
retrieveHighScore();
beginPulse();
isJumping = false;
isGameOver = true;
};
// Paint Game board Initially
firstScreen();