rodrigotello-denogamehtml.express.val.run
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
import { dinoGameScoreboard } from "https://esm.town/v/rodrigotello/dinoGameScoreboard";
export let denoGameHTML = (req, res) =>
res.send(`<!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 = ${
JSON.stringify(dinoGameScoreboard)
};
function setup() {
createCanvas(800, 300);
resetGame();
}
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 {
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023