Web Games Development · Media & quiz logic · ~150 minutes
<audio> / <video> and multiple <source> fallbacks.play(), currentTime, muted.visibility and display.createElement and appendChild.Create a folder named Week6 and copy the media materials (hail_to_the_chief.mp3,
hail_to_the_chief.ogg, sfire3.*) into it. Build the quiz below, writing the code shown.
Save and refresh after each change. Tick the checkpoint.
Week6\presidentsClick.html
<!DOCTYPE html>
<html lang="en">
<head><title>Ordering Quiz with Rewards</title></head>
<body>
<audio id="ruffles">
<source src="hail_to_the_chief.mp3" type="audio/mpeg" />
<source src="hail_to_the_chief.ogg" type="audio/ogg" />
</audio>
<video id="vid" width="50%" muted>
<source src="sfire3.webmvp8.webm" type="video/webm" />
<source src="sfire3.mp4" />
</video>
<h1>Order the Presidents</h1>
Your order: <div id="answer"></div>
Result: <div id="results"></div>
<script>
// TODO: add the facts array and the game variables
// TODO: add setupgame(), pickelement() and checkorder()
function init() {
// TODO: set up the game
}
</script>
</body>
</html>
var facts = [
["George Washington", false],
["John Adams", false]
];
var nq = 4;
var slots = [];
var res, ans, song, v;
var answertext = "";
setupgame() to build each box with createElement and appendChild.
function setupgame() {
slots = [];
answertext = "";
var mx = 20, my = 200;
for (var i = 0; i < nq; i++) {
var c;
do { c = Math.floor(Math.random() * facts.length); } while (facts[c][1]);
facts[c][1] = true;
var uniqueid = "p" + c;
var d = document.createElement('pres');
d.innerHTML = "<div class='thing' id='" + uniqueid + "'>placeholder</div>";
document.body.appendChild(d);
var thingelem = document.getElementById(uniqueid);
thingelem.textContent = String(i + 1) + ": " + facts[c][0];
thingelem.style.top = String(my) + "px";
thingelem.style.left = String(mx) + "px";
thingelem.addEventListener('click', pickelement);
my += 50;
}
}
pickelement() to record each pick.
function pickelement(ev) {
var answert = this.textContent.substring(0, 1);
var positionn = Number(this.id.substring(1));
answertext = answertext + answert + " ";
ans.innerHTML = answertext;
this.style.backgroundColor = "gold";
this.removeEventListener('click', pickelement);
slots.push(positionn);
if (slots.length == nq) { checkorder(); }
}
checkorder() to validate the order and reward a correct answer.
function checkorder() {
var ok = true;
for (var i = 0; i < nq - 1; i++) {
if (slots[i] > slots[i + 1]) { ok = false; break; }
}
if (ok) {
res.innerHTML = "CORRECT";
song.currentTime = 4;
song.play();
v.style.visibility = "visible";
v.style.display = "block";
v.play();
} else {
res.innerHTML = "WRONG";
}
}
init() to start the quiz.
res = document.getElementById("results");
ans = document.getElementById("answer");
song = document.getElementById("ruffles");
v = document.getElementById("vid");
setupgame();
| Criterion | Points | Score |
|---|---|---|
| Audio and video embedded with multiple sources | 3 | |
| Playback controlled from JavaScript | 3 | |
| Elements created with createElement / appendChild | 3 | |
| Ordering validated correctly | 3 | |
| Reward (sound + video) shown on success | 2 | |
| Runs with no console errors | 1 | |
| Total | 15 |
<source> format provided.currentTime before replaying.Progress and quiz scores are also tracked on the Week 6 lab page in the class website.