Skip to content

Instantly share code, notes, and snippets.

@angusgrant
Created November 11, 2022 22:55
Show Gist options
  • Save angusgrant/02aa4c8a509b865c15e5bdaf926d6c8e to your computer and use it in GitHub Desktop.
Save angusgrant/02aa4c8a509b865c15e5bdaf926d6c8e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Monsters! Game</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
body {
margin: 0 auto;
max-width: 40em;
width: 88%;
}
/**
* A simple grid layout
*/
.row {
display: grid;
grid-template-columns: auto auto auto;
text-align: center;
}
.grid {
min-height: 6em;
padding: 1em;
}
/**
* Make sure images scale
*/
img {
height: auto;
max-width: 100%;
}
</style>
</head>
<body>
<h1>Monsters! Game</h1>
<div id="app"></div>
<footer>
<hr>
<p class="text-small text-muted">Icons by <a href="https://thenounproject.com/term/door/311732/">Jamie Dickinson</a>, <a href="https://thenounproject.com/term/monster/184225/">Nicky Knicky</a>, <a href="https://thenounproject.com/term/monster/1510400/">Alvaro Cabrera</a>, <a href="https://thenounproject.com/term/monster/28460/">Eliricon</a>, <a href="https://thenounproject.com/term/monster/82823/">April Yang</a>, <a href="https://thenounproject.com/term/monster/1062009/">tk66</a>, <a href="https://thenounproject.com/term/monster/24990/">Alex WaZa</a>, <a href="https://thenounproject.com/term/monster/37212/">Husein Aziz</a>, <a href="https://thenounproject.com/term/monster/2236082">iconcheese</a>, and <a href="https://thenounproject.com/term/socks/38451/">Yazmin Alanis</a>.</p>
</footer>
<script>
// The monsters and socks
//use a self executing function to hide variables from global scope
let countClick = '0'
let monsters = [
{
name: 'monster1',
alt: 'A yellow monster with a curly nose'
},
{
name: 'monster2',
alt: 'A yellow monster with a wide head, one eye, and an underbite'
},
{
name: 'monster3',
alt: 'A green monster with eyes on stalks and a mouth at the top of its head'
},
{
name: 'monster4',
alt: 'A red monster with horns, four eyes, and no legs'
},
{
name: 'monster5',
alt: 'A green monster with three horns on each side of its head, one eye, and a sad look on its face'
},
{
name: 'monster6',
alt: 'A green, triangle-shaped monster with sharp teeth, walking upside-down on its hands'
},
{
name: 'monster7',
alt: 'A purple monster with a single, sad looking eye and tentacles for arms'
},
{
name: 'monster8',
alt: 'A purple, oval-shaped monster with one eye and no arms or legs'
},
{
name: 'monster9',
alt: 'A blue, insect-like monster, with bug eyes, three body sections, and a pair of wings'
},
{
name: 'monster10',
alt: 'A blue monster with lopsided eyes on stalks and long, sharp teeth'
},
{
name: 'monster11',
alt: 'A furry gray monster with long arms and a happy face'
},
{
name: 'sock',
alt: 'A pair of athletic socks'
}
];
// Get the #app element
let app = document.querySelector('#app');
document.addEventListener('click', function(e){
e.preventDefault();
if (event.target.matches('img') || event.target.matches('.toggleimg')){
let link = event.target.closest('.toggleimg');
//check if link exists if not then must be the revealed image.
if (link){
countClick++;
let image = link.querySelector('img');
//remove class to disable onclick behaviour
link.classList.remove('toggleimg');
//add aria pressed to tell screen readers the button is clicked
link.setAttribute('aria-pressed', 'true');
//decode the monster parts to reveal the monster
let decodedImgAlt = atob(image.dataset.alt);
let decodedImgSrc = atob(image.dataset.src);
var revealedImg = `<img src="${decodedImgSrc}" alt="Revealed image of ${decodedImgAlt}" >`
link.outerHTML = revealedImg;
if (decodedImgSrc === "sock.svg") {
//give time for image to load I would like to find a better way than a timeout for this!
setTimeout(function () {
app.innerHTML ='<h2 role="alert" tabindex="-1">Game Over you found the socks. Please try again!</h2><button onclick="loadGame()">Reload Game</button>';
app.querySelector('h2').focus();
}, 200);
} else if(countClick === 11) {
//give time for image to load I would like to find a better way than a timeout for this!
setTimeout(function () {
app.innerHTML ='<h2 role="alert" tabindex="-1">Congratulations you win! You found all the monsters and no socks!</h2><button onclick="loadGame()">Have another go!</button>';
app.querySelector('h2').focus();
}, 200);
}
}
}
});
/**
* Randomly shuffle an array
* https://stackoverflow.com/a/2450976/1293256
* @param {Array} array The array to shuffle
* @return {String} The first item in the shuffled array
*/
function shuffle (array) {
let currentIndex = array.length;
let temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function loadGame(){
//reset click counter to 0
countClick = '0'
// Shuffle the monsters array
shuffle(monsters);
// Inject the monsters into the DOM
app.innerHTML =
`<p>Click or Press on a door to find all the monsters. If you find a pair of socks before you find all the monsters you lose!<p>
<div class="row">
${monsters.map(function (monster, index) {
return `
<div class="grid" aria-live="polite">
<a href="#" role="button" title="click on door ${index + 1} to reveal the monster or sock" class="toggleimg">
<img src="door.svg" alt="door ${index + 1}" data-alt="${btoa(monster.alt)}" data-src="${btoa(`${monster.name}.svg`)}">
</a>
</div>`;
}).join('')}
</div>`;
};
//loadGame on load
loadGame();
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment