-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameExample.html
More file actions
288 lines (269 loc) · 8.2 KB
/
gameExample.html
File metadata and controls
288 lines (269 loc) · 8.2 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>2D Game Engine</title>
<style>
body {
margin: 0;
padding: 20px;
background: #111;
color: #fff;
font-family: system-ui, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
h1 {
margin-bottom: 10px;
color: #0170ff;
}
#gameContainer {
position: relative;
margin-bottom: 20px;
}
canvas {
display: block;
background: #222;
border: 3px solid #0170ff;
cursor: none;
}
#info {
text-align: center;
max-width: 800px;
line-height: 1.6;
color: #aaa;
font-size: 14px;
}
.controls {
margin-top: 15px;
padding: 15px;
background: #1a1a1a;
border: 1px solid #444;
border-radius: 4px;
}
.controls h3 {
margin-top: 0;
color: #0170ff;
}
.controls p {
margin: 5px 0;
}
.key {
background: #333;
padding: 2px 6px;
border-radius: 3px;
font-family: monospace;
}
</style>
</head>
<body>
<h1>🎮 2D Game Engine</h1>
<div id="gameContainer">
<canvas id="gameCanvas"></canvas>
</div>
<div id="info">
<p id="score">Score: 0</p>
<div class="controls">
<h3>How to Play</h3>
<p>
Move your player (tan rune ᛉ) to collect cultural memes (green
dollars) and avoid enemies (red stars).
</p>
<p>
<span class="key">←</span> <span class="key">→</span>
<span class="key">↑</span> <span class="key">↓</span> or
<span class="key">WASD</span> to move
</p>
<p>Collect cultural memes to win! Collide with enemies to lose.</p>
</div>
</div>
<script src="gameEngine.js"></script>
<script>
// Create game engine
const engine = new GameEngine("gameCanvas", 800, 600);
// Game state
let score = 0;
let gameOver = false;
let won = false;
const WIN_SCORE = 10;
// Create player (Algiz rune ᛉ)
const player = engine.addSprite(
new Sprite(engine.width / 2 - 16, engine.height / 2, 32, 32, "#D2B48C"),
);
player.tag = "player";
player.speed = 300; // pixels per second
// Render player as Algiz rune (ᛉ)
player.renderShape = "rune";
// Create coins
function spawnCoin() {
const coin = engine.addSprite(
new Sprite(
Math.random() * (engine.width - 20),
Math.random() * (engine.height - 20),
20,
20,
"#00ff00",
),
);
coin.tag = "meme";
coin.renderShape = "dollar";
return coin;
}
// Create enemies
function spawnEnemy() {
const enemy = engine.addSprite(
new Sprite(
Math.random() * (engine.width - 24),
Math.random() * (engine.height - 24),
24,
24,
"#ff0101",
),
);
enemy.tag = "enemy";
enemy.speed = 150 + Math.random() * 100;
890;
enemy.dirX = Math.random() > 0.5 ? 1 : -1;
enemy.dirY = Math.random() > 0.5 ? 1 : -1;
enemy.renderShape = "star";
return enemy;
}
// Initial setup
for (let i = 0; i < 5; i++) spawnCoin();
for (let i = 0; i < 3; i++) spawnEnemy();
// Override update to add game logic
const originalUpdate = engine.update.bind(engine);
engine.update = function (dt) {
if (gameOver || won) return;
// Player input
if (engine.isKeyPressed("arrowleft") || engine.isKeyPressed("a")) {
player.vel.x = -player.speed;
} else if (
engine.isKeyPressed("arrowright") ||
engine.isKeyPressed("d")
) {
player.vel.x = player.speed;
} else {
player.vel.x = 0;
}
if (engine.isKeyPressed("arrowup") || engine.isKeyPressed("w")) {
player.vel.y = -player.speed;
} else if (
engine.isKeyPressed("arrowdown") ||
engine.isKeyPressed("s")
) {
player.vel.y = player.speed;
} else {
player.vel.y = 0;
}
// Update sprites
originalUpdate(dt);
// Clamp player to bounds
player.pos.x = Math.max(
0,
Math.min(player.pos.x, engine.width - player.width),
);
player.pos.y = Math.max(
0,
Math.min(player.pos.y, engine.height - player.height),
);
// Update enemies (simple AI: bounce around)
const enemies = engine.findSpritesWithTag("enemy");
for (const enemy of enemies) {
enemy.vel.x = enemy.dirX * enemy.speed;
enemy.vel.y = enemy.dirY * enemy.speed;
// Bounce off walls
if (enemy.pos.x <= 0 || enemy.pos.x + enemy.width >= engine.width) {
enemy.dirX *= -1;
}
if (enemy.pos.y <= 0 || enemy.pos.y + enemy.height >= engine.height) {
enemy.dirY *= -1;
}
}
// Collision: player vs coins
const coins = engine.findSpritesWithTag("meme");
for (const coin of coins) {
if (player.collidesWith(coin)) {
engine.removeSprite(coin);
score++;
document.getElementById("score").textContent = `Score: ${score}`;
if (score >= WIN_SCORE) {
won = true;
document.getElementById("score").textContent =
`🎉 You Won! Score: ${score}`;
} else {
spawnCoin();
}
}
}
// Collision: player vs enemies
for (const enemy of enemies) {
if (player.collidesWith(enemy)) {
gameOver = true;
document.getElementById("score").textContent =
`💀 Game Over! Final Score: ${score}`;
}
}
};
// Override render to add debug info
const originalRender = engine.render.bind(engine);
engine.render = function () {
originalRender();
// Draw text overlay
this.ctx.fillStyle = "#fff";
this.ctx.font = "bold 16px system-ui";
this.ctx.fillText(`Score: ${score}/${WIN_SCORE}`, 20, 30);
this.ctx.font = "normal 12px system-ui";
this.ctx.fillText(
`Memes: ${engine.findSpritesWithTag("meme").length}`,
20,
50,
);
this.ctx.fillText(
`Enemies: ${engine.findSpritesWithTag("enemy").length}`,
20,
70,
);
if (gameOver) {
this.ctx.fillStyle = "rgba(0,0,0,0.5)";
this.ctx.fillRect(0, 0, this.width, this.height);
this.ctx.fillStyle = "#ff0101";
this.ctx.font = "bold 48px system-ui";
this.ctx.textAlign = "center";
this.ctx.fillText("GAME OVER", this.width / 2, this.height / 2);
this.ctx.fillStyle = "#fff";
this.ctx.font = "normal 20px system-ui";
this.ctx.fillText(
"Refresh to play again",
this.width / 2,
this.height / 2 + 40,
);
} else if (won) {
this.ctx.fillStyle = "rgba(0,0,0,0.5)";
this.ctx.fillRect(0, 0, this.width, this.height);
this.ctx.fillStyle = "#00ff00";
this.ctx.font = "bold 48px system-ui";
this.ctx.textAlign = "center";
// Wrap text across multiple lines
const line1 = "YOU WON!!";
const line2 = "ALL MEMES ARE YOURS!";
this.ctx.fillText(line1, this.width / 2, this.height / 2 - 30);
this.ctx.fillText(line2, this.width / 2, this.height / 2 + 20);
this.ctx.fillStyle = "#fff";
this.ctx.font = "normal 20px system-ui";
this.ctx.fillText(
"Refresh to play again",
this.width / 2,
this.height / 2 + 60,
);
}
};
// Start the game
engine.start();
</script>
</body>
</html>