-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdino.html
More file actions
221 lines (194 loc) · 6.66 KB
/
Copy pathdino.html
File metadata and controls
221 lines (194 loc) · 6.66 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dino</title>
<style>
*, *::before, *::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
}
body {
background: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-family: "Courier New", Courier, monospace;
color: #535353;
}
#game {
display: block;
border: 1px solid #000;
}
#caption {
margin-top: 8px;
font-size: 11px;
letter-spacing: 0.05em;
color: #535353;
text-transform: lowercase;
}
</style>
</head>
<body>
<canvas id="game" width="800" height="200"></canvas>
<div id="caption">press space to jump</div>
<script>
(function() {
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = false;
const GRAVITY = 0.6;
const JUMP_VEL = -12;
const GROUND_Y = 180;
const DINO_X = 50;
const DINO_SIZE = 44;
const DINO_START_Y = GROUND_Y - DINO_SIZE;
const OBSTACLE_W = 20;
const OBSTACLE_H = 40;
const OBSTACLE_Y = GROUND_Y - OBSTACLE_H;
const INITIAL_SPEED = 6;
const MAX_SPEED = 12;
const SPEED_STEP_SCORE = 500;
const FONT_COLOR = '#535353';
let dino = { x: DINO_X, y: DINO_START_Y, vy: 0, width: DINO_SIZE, height: DINO_SIZE, grounded: true };
let obstacles = [];
let spawnTimer = 0;
let nextSpawnDelay = 1500; // ms
let score = 0;
let highScore = 0;
let gameSpeed = INITIAL_SPEED;
let state = 'playing'; // 'playing' | 'over'
let lastTimestamp = performance.now();
function reset() {
dino = { x: DINO_X, y: DINO_START_Y, vy: 0, width: DINO_SIZE, height: DINO_SIZE, grounded: true };
obstacles = [];
spawnTimer = 0;
nextSpawnDelay = 1500;
score = 0;
gameSpeed = INITIAL_SPEED;
state = 'playing';
}
function spawnObstacle() {
obstacles.push({ x: canvas.width, y: OBSTACLE_Y, width: OBSTACLE_W, height: OBSTACLE_H });
}
function rectOverlap(a, b) {
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}
function update(dt) {
if (state === 'playing') {
// Dino physics
if (!dino.grounded) {
dino.vy += GRAVITY;
dino.y += dino.vy;
if (dino.y >= DINO_START_Y) {
dino.y = DINO_START_Y;
dino.vy = 0;
dino.grounded = true;
}
}
// Move obstacles
obstacles.forEach(o => o.x -= gameSpeed);
obstacles = obstacles.filter(o => o.x + o.width > -20);
// Spawn logic
spawnTimer += dt;
if (spawnTimer >= nextSpawnDelay) {
spawnObstacle();
spawnTimer = 0;
nextSpawnDelay = 800 + Math.random() * 800; // 800‑1600 ms
}
// Collision detection
for (let o of obstacles) {
if (rectOverlap(dino, o)) {
state = 'over';
break;
}
}
// Scoring and speed
score++;
if (score > highScore) highScore = score;
if (score % SPEED_STEP_SCORE === 0 && gameSpeed < MAX_SPEED) {
gameSpeed++;
}
}
}
function render() {
// Background
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Ground line
ctx.fillStyle = FONT_COLOR;
ctx.fillRect(0, GROUND_Y, canvas.width, 1);
// Dino
ctx.fillStyle = FONT_COLOR;
ctx.fillRect(dino.x, dino.y, dino.width, dino.height);
// Eye dot
ctx.fillRect(dino.x + 30, dino.y + 10, 4, 4);
// Obstacles
obstacles.forEach(o => {
ctx.fillRect(o.x, o.y, o.width, o.height);
});
// HUD (top‑right)
const hiStr = String(highScore).padStart(5, '0');
const curStr = String(score).padStart(5, '0');
const hudText = `HI ${hiStr} ${curStr}`;
ctx.font = '14px monospace';
ctx.textBaseline = 'top';
ctx.fillStyle = FONT_COLOR;
const hudWidth = ctx.measureText(hudText).width;
ctx.fillText(hudText, canvas.width - hudWidth - 10, 10);
// Game over overlay
if (state === 'over') {
ctx.fillStyle = FONT_COLOR;
ctx.font = '24px monospace';
const overMsg = 'GAME OVER';
const overW = ctx.measureText(overMsg).width;
ctx.fillText(overMsg, (canvas.width - overW) / 2, canvas.height / 2 - 20);
ctx.font = '14px monospace';
const subMsg = 'press R to restart';
const subW = ctx.measureText(subMsg).width;
ctx.fillText(subMsg, (canvas.width - subW) / 2, canvas.height / 2 + 10);
}
}
function loop(timestamp) {
const dt = timestamp - lastTimestamp;
lastTimestamp = timestamp;
update(dt);
render();
requestAnimationFrame(loop);
}
// Input handling
window.addEventListener('keydown', function (e) {
if (e.code === 'Space' || e.code === 'ArrowUp') {
if (state === 'playing' && dino.grounded) {
dino.vy = JUMP_VEL;
dino.grounded = false;
}
e.preventDefault();
} else if (e.code === 'KeyR') {
if (state === 'over') {
reset();
}
}
});
canvas.addEventListener('touchstart', function (e) {
if (state === 'playing' && dino.grounded) {
dino.vy = JUMP_VEL;
dino.grounded = false;
}
e.preventDefault();
}, { passive: false });
requestAnimationFrame(loop);
})();
</script>
</body>
</html>