tangled
alpha
login
or
join now
thecoded.prof
/
CMU
0
fork
atom
CMU Coding Bootcamp
0
fork
atom
overview
issues
pulls
pipelines
feat(html): dino
thecoded.prof
4 months ago
0997b9b6
e973da63
verified
This commit was signed with the committer's
known signature
.
thecoded.prof
SSH Key Fingerprint:
SHA256:ePn0u8NlJyz3J4Zl9MHOYW3f4XKoi5K1I4j53bwpG0U=
+311
-2
5 changed files
expand all
collapse all
unified
split
html
css
dino.css
dino.html
index.html
js
dino.js
script.js
+42
html/css/dino.css
···
1
1
+
body {
2
2
+
margin: 0;
3
3
+
padding: 0;
4
4
+
display: flex;
5
5
+
justify-content: center;
6
6
+
align-items: center;
7
7
+
min-height: 100vh;
8
8
+
background-color: #f7f7f7;
9
9
+
font-family: Arial, sans-serif;
10
10
+
}
11
11
+
12
12
+
.game-container {
13
13
+
position: relative;
14
14
+
}
15
15
+
16
16
+
#gameCanvas {
17
17
+
border: 2px solid #535353;
18
18
+
background-color: #fff;
19
19
+
display: block;
20
20
+
}
21
21
+
22
22
+
#score {
23
23
+
position: absolute;
24
24
+
top: 10px;
25
25
+
right: 10px;
26
26
+
font-size: 20px;
27
27
+
font-weight: bold;
28
28
+
color: #535353;
29
29
+
}
30
30
+
31
31
+
#gameOver {
32
32
+
position: absolute;
33
33
+
width: 100%;
34
34
+
text-align: center;
35
35
+
top: 50%;
36
36
+
left: 50%;
37
37
+
transform: translate(-50%, -50%);
38
38
+
font-size: 24px;
39
39
+
font-weight: bold;
40
40
+
color: #535353;
41
41
+
display: none;
42
42
+
}
+19
html/dino.html
···
1
1
+
<!doctype html>
2
2
+
<html lang="en">
3
3
+
<head>
4
4
+
<meta charset="UTF-8" />
5
5
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
6
+
<title>Dino Game</title>
7
7
+
<link rel="stylesheet" href="css/dino.css" />
8
8
+
</head>
9
9
+
<body>
10
10
+
<div class="game-container">
11
11
+
<canvas id="gameCanvas" width="800" height="600"></canvas>
12
12
+
<div id="score">Score: 0</div>
13
13
+
<div id="gameOver">
14
14
+
Game Over! Press <kbd>Space</kbd> to Restart
15
15
+
</div>
16
16
+
</div>
17
17
+
<script src="js/dino.js"></script>
18
18
+
</body>
19
19
+
</html>
+1
-1
html/index.html
···
19
19
gap: 1rem;
20
20
"
21
21
>
22
22
-
<div class="card" onclick="openProject('temp')"></div>
22
22
+
<div class="card" onclick="openProject('dino')">Dino</div>
23
23
<div class="card" onclick="openProject('temp')"></div>
24
24
<div class="card" onclick="openProject('temp')"></div>
25
25
</div>
+246
html/js/dino.js
···
1
1
+
const canvas = document.getElementById("gameCanvas");
2
2
+
const ctx = canvas.getContext("2d");
3
3
+
const scoreElement = document.getElementById("score");
4
4
+
const gameOverElement = document.getElementById("gameOver");
5
5
+
6
6
+
let score = 0;
7
7
+
let gameSpeed = 3;
8
8
+
let isGameOver = false;
9
9
+
let frameCount = 0;
10
10
+
let nextObstacleFrame = 10;
11
11
+
12
12
+
const dino = {
13
13
+
x: 50,
14
14
+
y: 150,
15
15
+
width: 40,
16
16
+
height: 50,
17
17
+
dy: 0,
18
18
+
gravity: 0.3,
19
19
+
jumpPower: -8,
20
20
+
isJumping: false,
21
21
+
isDucking: false,
22
22
+
23
23
+
draw() {
24
24
+
ctx.fillStyle = "#8aacdf";
25
25
+
ctx.fillRect(this.x, this.y, this.width, this.height);
26
26
+
},
27
27
+
28
28
+
update() {
29
29
+
if (this.isDucking) {
30
30
+
this.y = 170;
31
31
+
this.height = 30;
32
32
+
} else {
33
33
+
this.height = 50;
34
34
+
}
35
35
+
36
36
+
if (this.isJumping) {
37
37
+
this.dy += this.gravity;
38
38
+
this.y += this.dy;
39
39
+
40
40
+
if (this.y >= 150) {
41
41
+
this.y = 150;
42
42
+
this.isJumping = false;
43
43
+
this.dy = 0;
44
44
+
}
45
45
+
}
46
46
+
},
47
47
+
48
48
+
duck() {
49
49
+
if (!this.isJumping && !isGameOver) {
50
50
+
this.isDucking = true;
51
51
+
} else if (this.isJumping && !isGameOver) {
52
52
+
this.dy += this.gravity * 10;
53
53
+
this.y += this.dy;
54
54
+
}
55
55
+
},
56
56
+
57
57
+
jump() {
58
58
+
if (!this.isJumping && !isGameOver) {
59
59
+
this.isJumping = true;
60
60
+
this.dy = this.jumpPower;
61
61
+
}
62
62
+
},
63
63
+
};
64
64
+
65
65
+
const obstacles = [];
66
66
+
67
67
+
class Obstacle {
68
68
+
constructor() {
69
69
+
this.x = canvas.width;
70
70
+
const type = Math.random();
71
71
+
if (type < 0.5) {
72
72
+
this.width = 20;
73
73
+
this.height = 40;
74
74
+
this.y = 160;
75
75
+
} else if (type < 0.8) {
76
76
+
this.width = 20;
77
77
+
this.height = 25;
78
78
+
this.y = 175;
79
79
+
} else {
80
80
+
this.width = 40;
81
81
+
this.height = 32;
82
82
+
this.y = 168;
83
83
+
}
84
84
+
}
85
85
+
86
86
+
draw() {
87
87
+
ctx.fillStyle = "#40a02b";
88
88
+
ctx.fillRect(this.x, this.y, this.width, this.height);
89
89
+
}
90
90
+
91
91
+
update() {
92
92
+
this.x -= gameSpeed;
93
93
+
}
94
94
+
}
95
95
+
96
96
+
class BirdObstacle {
97
97
+
constructor() {
98
98
+
this.x = canvas.width;
99
99
+
this.width = 30;
100
100
+
this.height = 20;
101
101
+
const heightType = Math.floor(Math.random() * 3);
102
102
+
if (heightType === 0) {
103
103
+
this.y = 170;
104
104
+
} else if (heightType === 1) {
105
105
+
this.y = 150;
106
106
+
} else {
107
107
+
this.y = 130;
108
108
+
}
109
109
+
}
110
110
+
111
111
+
draw() {
112
112
+
ctx.fillStyle = "#dd7878";
113
113
+
ctx.fillRect(this.x, this.y, this.width, this.height);
114
114
+
}
115
115
+
116
116
+
update() {
117
117
+
this.x -= gameSpeed;
118
118
+
}
119
119
+
}
120
120
+
121
121
+
function checkCollision(dino, obstacle) {
122
122
+
return (
123
123
+
dino.x < obstacle.x + obstacle.width &&
124
124
+
dino.x + dino.width > obstacle.x &&
125
125
+
dino.y < obstacle.y + obstacle.height &&
126
126
+
dino.y + dino.height > obstacle.y
127
127
+
);
128
128
+
}
129
129
+
130
130
+
function spawnObstacle() {
131
131
+
if (frameCount >= nextObstacleFrame) {
132
132
+
const type = Math.random();
133
133
+
if (type < 0.9) {
134
134
+
obstacles.push(new Obstacle());
135
135
+
} else {
136
136
+
obstacles.push(new BirdObstacle());
137
137
+
}
138
138
+
139
139
+
const baseMin = 80;
140
140
+
const baseMax = 140;
141
141
+
142
142
+
const speedFactor = 3 / gameSpeed;
143
143
+
144
144
+
const minInterval = Math.floor(baseMin * speedFactor);
145
145
+
const maxInterval = Math.floor(baseMax * speedFactor);
146
146
+
nextObstacleFrame =
147
147
+
frameCount +
148
148
+
Math.floor(Math.random() * (maxInterval - minInterval) + minInterval);
149
149
+
}
150
150
+
}
151
151
+
152
152
+
function updateScore() {
153
153
+
if (!isGameOver) {
154
154
+
score++;
155
155
+
scoreElement.textContent = `Score: ${Math.floor(score / 10)}`;
156
156
+
157
157
+
// Increase difficulty
158
158
+
if (score % 500 === 0) {
159
159
+
gameSpeed += 0.5;
160
160
+
}
161
161
+
}
162
162
+
}
163
163
+
164
164
+
function gameLoop() {
165
165
+
if (isGameOver) return;
166
166
+
167
167
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
168
168
+
169
169
+
ctx.strokeStyle = "#535353";
170
170
+
ctx.beginPath();
171
171
+
ctx.moveTo(0, 200);
172
172
+
ctx.lineTo(canvas.width, 200);
173
173
+
ctx.stroke();
174
174
+
175
175
+
dino.update();
176
176
+
dino.draw();
177
177
+
178
178
+
spawnObstacle();
179
179
+
180
180
+
for (let i = obstacles.length - 1; i >= 0; i--) {
181
181
+
obstacles[i].update();
182
182
+
obstacles[i].draw();
183
183
+
184
184
+
if (checkCollision(dino, obstacles[i])) {
185
185
+
isGameOver = true;
186
186
+
gameOverElement.style.display = "block";
187
187
+
}
188
188
+
189
189
+
if (obstacles[i].x + obstacles[i].width < 0) {
190
190
+
obstacles.splice(i, 1);
191
191
+
}
192
192
+
}
193
193
+
194
194
+
updateScore();
195
195
+
frameCount++;
196
196
+
197
197
+
requestAnimationFrame(gameLoop);
198
198
+
}
199
199
+
200
200
+
function resetGame() {
201
201
+
score = 0;
202
202
+
gameSpeed = 3;
203
203
+
isGameOver = false;
204
204
+
frameCount = 0;
205
205
+
obstacles.length = 0;
206
206
+
dino.y = 150;
207
207
+
dino.dy = 0;
208
208
+
dino.isJumping = false;
209
209
+
gameOverElement.style.display = "none";
210
210
+
nextObstacleFrame = 10;
211
211
+
gameLoop();
212
212
+
}
213
213
+
214
214
+
document.addEventListener("keydown", (e) => {
215
215
+
e.preventDefault();
216
216
+
switch (e.code) {
217
217
+
case "ArrowUp":
218
218
+
case "Space": {
219
219
+
if (isGameOver) {
220
220
+
resetGame();
221
221
+
} else {
222
222
+
dino.jump();
223
223
+
}
224
224
+
break;
225
225
+
}
226
226
+
case "ArrowDown": {
227
227
+
dino.duck();
228
228
+
break;
229
229
+
}
230
230
+
default:
231
231
+
}
232
232
+
});
233
233
+
234
234
+
document.addEventListener("keyup", (e) => {
235
235
+
switch (e.code) {
236
236
+
case "ArrowDown": {
237
237
+
e.preventDefault();
238
238
+
dino.isDucking = false;
239
239
+
if (!dino.isJumping) {
240
240
+
dino.y = 150;
241
241
+
}
242
242
+
}
243
243
+
}
244
244
+
});
245
245
+
246
246
+
gameLoop();
+3
-1
html/js/script.js
···
1
1
-
const projects = {};
1
1
+
const projects = {
2
2
+
dino: "dino.html",
3
3
+
};
2
4
3
5
const openProject = (project) => {
4
6
if (Object.keys(projects).includes(project)) {