-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.html
More file actions
260 lines (242 loc) · 9.31 KB
/
example2.html
File metadata and controls
260 lines (242 loc) · 9.31 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Learn & Explore – Quiz-Gated Metroidvania Web Game</title>
<style>
/* Basic resets and layout styling */
body { margin: 0; padding: 0; font-family: Arial, sans-serif; background: #222; color: #fff; }
#game-container { margin: 0 auto; display: block; background: #000; }
/* Quiz modal styling */
#quiz-modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
display: flex;
align-items: center;
justify-content: center;
z-index: 100;
}
#quiz-modal.hidden { display: none; }
#quiz-content {
background: #fff;
color: #000;
padding: 20px;
border-radius: 8px;
width: 80%;
max-width: 500px;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
}
.question { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; }
#submit-quiz {
display: inline-block;
padding: 10px 20px;
margin-top: 10px;
background: #007BFF;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="game-container"></div>
<div id="quiz-modal" class="hidden">
<div id="quiz-content">
<div id="question-container"></div>
<button id="submit-quiz">Submit Answers</button>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/phaser@3/dist/phaser.js"></script>
<script>
window.onload = function() {
// Phaser configuration
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'game-container',
physics: {
default: 'arcade',
arcade: { gravity: { y: 500 }, debug: false }
},
scene: { preload: preload, create: create, update: update }
};
const game = new Phaser.Game(config);
// Game state
let player, cursors, dashKey, fireKey;
let orbsGroup, fireGateGroup, iceGateGroup, wallGateGroup, fireProjectiles;
let abilities = { fire: false, ice: false, doubleJump: false };
let jumpCount = 0;
// Quiz state
const questionPool = {
fire: [
{ q: "What is H2O commonly known as?", opts: ["Water", "Hydrogen", "Oxygen", "Helium"], ans: 0 },
{ q: "What planet is known as the Red Planet?", opts: ["Earth", "Mars", "Jupiter", "Venus"], ans: 1 },
{ q: "What gas do plants inhale?", opts: ["Oxygen", "Nitrogen", "Carbon Dioxide", "Helium"], ans: 2 }
],
ice: [
{ q: "2 + 2 × 2 = ?", opts: ["6", "8", "4", "2"], ans: 0 },
{ q: "What is the square root of 16?", opts: ["2", "4", "8", "16"], ans: 1 },
{ q: "5^2 = ?", opts: ["10", "15", "25", "20"], ans: 2 }
],
doubleJump: [
{ q: "Who was first U.S. president?", opts: ["Abraham Lincoln", "Benjamin Franklin", "George Washington", "Thomas Jefferson"], ans: 2 },
{ q: "In what year did WW2 end?", opts: ["1940", "1945", "1950", "1939"], ans: 1 },
{ q: "Who wrote 'Romeo and Juliet'?", opts: ["Shakespeare", "Dickens", "Austen", "Tolstoy"], ans: 0 }
]
};
let currentQuiz = null;
function preload() {
this.load.image('tiles', 'assets/tiles.png');
this.load.tilemapTiledJSON('map', 'assets/map.json');
this.load.spritesheet('player', 'assets/player.png', { frameWidth: 32, frameHeight: 48 });
this.load.image('orb', 'assets/orb.png');
this.load.image('fireGate', 'assets/fire_gate.png');
this.load.image('iceGate', 'assets/ice_gate.png');
this.load.image('wallGate', 'assets/wall_gate.png');
this.load.image('fireProj', 'assets/fire_proj.png');
}
function create() {
// Build map
const map = this.make.tilemap({ key: 'map' });
const tiles = map.addTilesetImage('Tileset', 'tiles');
const ground = map.createLayer('Ground', tiles, 0, 0);
ground.setCollisionByProperty({ collides: true });
// Gate groups
fireGateGroup = this.physics.add.staticGroup();
iceGateGroup = this.physics.add.staticGroup();
wallGateGroup = this.physics.add.staticGroup();
// Orbs
orbsGroup = this.physics.add.group();
map.getObjectLayer('Objects').objects.forEach(obj => {
const x = obj.x, y = obj.y - obj.height;
if (obj.type === 'orb') {
let orb = orbsGroup.create(x, y, 'orb');
orb.ability = obj.properties.find(p => p.name === 'ability').value;
}
if (obj.type === 'gate') {
switch(obj.name) {
case 'fireGate':
fireGateGroup.create(x, y, 'fireGate');
break;
case 'iceGate':
iceGateGroup.create(x, y, 'iceGate');
break;
case 'wallGate':
wallGateGroup.create(x, y, 'wallGate');
break;
}
}
});
// Player
player = this.physics.add.sprite(100, 450, 'player');
player.setCollideWorldBounds(true);
this.physics.add.collider(player, ground);
this.physics.add.collider(player, fireGateGroup);
this.physics.add.collider(player, iceGateGroup);
this.physics.add.collider(player, wallGateGroup);
this.physics.add.overlap(player, orbsGroup, collectOrb, null, this);
// Abilities inputs
cursors = this.input.keyboard.createCursorKeys();
dashKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SHIFT);
fireKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.F);
fireProjectiles = this.physics.add.group();
this.physics.add.overlap(fireProjectiles, fireGateGroup, (proj, gate) => {
gate.destroy();
proj.destroy();
});
}
function update() {
// Horizontal movement
if (cursors.left.isDown) player.setVelocityX(-160);
else if (cursors.right.isDown) player.setVelocityX(160);
else player.setVelocityX(0);
// Jump & Double Jump
if (Phaser.Input.Keyboard.JustDown(cursors.up)) {
if (player.body.onFloor()) {
player.setVelocityY(-330);
jumpCount = 1;
} else if (abilities.doubleJump && jumpCount < 2) {
player.setVelocityY(-330);
jumpCount++;
}
}
if (player.body.onFloor()) jumpCount = 0;
// Ice Dash
if (abilities.ice && Phaser.Input.Keyboard.JustDown(dashKey)) {
const dir = cursors.left.isDown ? -1 : (cursors.right.isDown ? 1 : 0);
if (dir !== 0) player.setVelocityX(400 * dir);
}
// Fire Blast
if (abilities.fire && Phaser.Input.Keyboard.JustDown(fireKey)) {
const proj = fireProjectiles.create(player.x + (player.flipX ? -20 : 20), player.y, 'fireProj');
proj.setVelocityX(player.flipX ? -300 : 300);
proj.body.allowGravity = false;
}
}
function collectOrb(player, orb) {
// Store current scene for later resume
window.activeScene = this;
this.physics.pause();
showQuiz(orb.ability, orb);
}
function showQuiz(type, orb) {
const container = document.getElementById('question-container');
const pool = [...questionPool[type]];
// Randomly choose 3 questions
const chosen = [];
for (let i = 0; i < 3; i++) {
const idx = Math.floor(Math.random() * pool.length);
chosen.push(pool.splice(idx, 1)[0]);
}
currentQuiz = { type, orb, questions: chosen };
// Render quiz questions
container.innerHTML = '';
chosen.forEach((q, i) => {
const div = document.createElement('div');
div.className = 'question';
div.innerHTML = `<p>${i + 1}. ${q.q}</p>`;
q.opts.forEach((opt, j) => {
const id = `q${i}_opt${j}`;
div.innerHTML += `<label><input type='radio' name='q${i}' value='${j}' id='${id}'> ${opt}</label>`;
});
container.appendChild(div);
});
document.getElementById('quiz-modal').classList.remove('hidden');
document.getElementById('submit-quiz').onclick = submitQuiz;
}
function submitQuiz() {
const { type, orb, questions } = currentQuiz;
let correct = 0;
questions.forEach((q, i) => {
const sel = document.querySelector(`input[name='q${i}']:checked`);
if (sel && parseInt(sel.value) === q.ans) correct++;
});
document.getElementById('quiz-modal').classList.add('hidden');
if (correct === questions.length) {
unlockAbility(type);
orb.destroy();
alert('Correct! Ability unlocked: ' + type);
} else {
alert(`You got ${correct}/${questions.length}. Try again later.`);
}
window.activeScene.physics.resume();
currentQuiz = null;
}
function unlockAbility(type) {
abilities[type] = true;
if (type === 'fire') fireGateGroup.clear(true, true);
if (type === 'ice') iceGateGroup.clear(true, true);
if (type === 'doubleJump') wallGateGroup.clear(true, true);
}
};
</script>
</body>
</html>