-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleChromeDinosaurGame.html
269 lines (234 loc) · 6.63 KB
/
googleChromeDinosaurGame.html
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<!DOCTYPE html>
<html>
<head>
<title>Dinosaur Game</title>
<style>
canvas {
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
<script>
let dino;
let obstacles = [];
let score = 0;
let highScore = 0;
let gameSpeed = 6;
let isGameOver = false;
let gamePaused = false;
let scoreboard = [];
function setup() {
createCanvas(800, 300);
resetGame();
// Fetch the scoreboard data from the Val Town API
const apiUrl =
"https://api.val.town/v1/run/rodrigotello.dinoGameScoreboard";
fetch(apiUrl)
.then((response) => response.json())
.then((scoreboardData) => {
// Store the scoreboard data in the variable
scoreboard = scoreboardData.scores || [];
})
.catch((error) => {
console.error("Error fetching scoreboard data:", error);
});
}
function resetGame() {
dino = new Dino();
obstacles = [];
score = 0;
gameSpeed = 6;
isGameOver = false;
gamePaused = false;
}
function draw() {
if (isGameOver) {
showGameOver();
return;
}
if (gamePaused) {
showGamePaused();
return;
}
background(255);
// Generate new obstacles
if (frameCount % 60 === 0) {
let obstacle = new Obstacle();
obstacles.push(obstacle);
}
// Update and display obstacles
for (let i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
obstacles[i].show();
// Check collision with dino
if (obstacles[i].collidesWith(dino)) {
gameOver();
break;
}
// Remove obstacles that are offscreen
if (obstacles[i].offscreen()) {
obstacles.splice(i, 1);
score++;
gameSpeed += 0.2;
}
}
// Update and display dino
dino.update();
dino.show();
// Display score
textAlign(RIGHT);
textFont("Courier New");
textSize(20);
fill(0);
text("Score: " + score, width - 20, 30);
text("High Score: " + highScore, width - 20, 60);
console.log("Current Score:", score);
}
function keyPressed() {
if (keyCode === 32) {
if (isGameOver) {
resetGame();
} else {
gamePaused = !gamePaused;
}
}
if (keyCode === UP_ARROW) {
dino.jump();
}
}
function gameOver() {
isGameOver = true;
let name = prompt("Enter your name (max 16 characters, no spaces):");
// Update the local scoreboard variable
scoreboard.push({ name, score });
// Call the function to update the scoreboard on the Val Town API
updateScoreboard(name, score);
}
function updateScoreboard(name, score) {
const apiUrl =
"https://api.val.town/v1/run/rodrigotello.updateDinoScoreboard";
fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
args: [
{
name,
score,
time: Date.now(),
},
],
}),
})
.then((response) => response.json())
.then((result) => {
console.log("Scoreboard updated:", result);
// Update the scoreboard variable with the fetched data
scoreboard = result;
})
.catch((error) => {
console.error("Error updating scoreboard:", error);
});
}
function showGameOver() {
background(255);
textAlign(CENTER);
textFont("Courier New");
textSize(30);
fill(0);
text("☠️ Game Over", width / 2, height / 2 - 30);
textSize(20);
text("Press SPACE to play again", width / 2, height / 2 + 10);
// Fetch the latest scoreboard data
const apiUrl =
"https://api.val.town/v1/run/rodrigotello.dinoGameScoreboard";
// Sort the scoreboard by score in descending order
scoreboard.sort((a, b) => b.score - a.score);
// Display scoreboard
textAlign(LEFT);
textSize(16);
fill(0);
let yPos = height / 2 + 60;
for (let i = 0; i < scoreboard.length; i++) {
let entry = scoreboard[i];
text(entry.name + ": " + entry.score, width / 2 - 40, yPos);
yPos += 20;
}
}
function showGamePaused() {
background(255);
textAlign(CENTER);
textFont("Courier New");
textSize(30);
fill(0);
text("⏸️ Game Paused", width / 2, height / 2 - 30);
textSize(20);
text("Press SPACE to resume", width / 2, height / 2 + 10);
}
class Dino {
constructor() {
this.x = 50;
this.y = height - 50;
this.width = 40;
this.height = 50;
this.velocityY = 0;
this.gravity = 1;
this.jumpForce = 15;
}
jump() {
if (this.y === height - this.height) {
this.velocityY = -this.jumpForce;
}
}
update() {
this.velocityY += this.gravity;
this.y += this.velocityY;
if (this.y > height - this.height) {
this.y = height - this.height;
this.velocityY = 0;
}
}
show() {
fill(0);
rect(this.x, this.y, this.width, this.height);
}
}
class Obstacle {
constructor() {
this.x = width;
this.width = 20;
this.height = random(20, 80);
this.velocityX = -gameSpeed;
}
update() {
this.x += this.velocityX;
}
show() {
fill(0);
rect(this.x, height - this.height, this.width, this.height);
}
collidesWith(dino) {
if (
this.x < dino.x + dino.width &&
this.x + this.width > dino.x &&
height - this.height < dino.y + dino.height &&
height - this.height + this.height > dino.y
) {
return true;
}
return false;
}
offscreen() {
return this.x + this.width < 0;
}
}
// Run the game
new p5();
</script>
</body>
</html>