-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatching-game.html
162 lines (135 loc) · 4.25 KB
/
matching-game.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Matching Game</title>
<style>
img {
position: absolute;
}
header {
display: flex;
flex-direction: column;
flex-flow: column;
}
div {
position: absolute;
width: 500px;
height: 500px;
}
#main {
display: flex;
flex-direction: row;
}
div {
display: flex;
flex-direction: row;
}
body {
display: flex;
flex-direction: column;
}
footer {
display: flex;
justify-content: center;
align-items: center;
flex-direction: row;
}
#rightSide {
left: 500px;
border-left: 1px solid;
}
button {
margin: 10px;
color: purple;
font-weight: bold;
font-size: 1.2em;
padding: 10px;
border: 2px solid purple;
border-radius: 10px;
background-color: white;
}
button:hover {
background-color: purple;
color: white;
cursor: pointer;
}
#reload {
flex: 1;
display: inline-block;
justify-content: center;
align-items: bottom;
}
</style>
</head>
<body>
<header id="header">
<div>
<h1>Matching Game</h1>
</div>
<div>
<p>Click on the extra smiling face on the left.</p>
</div>
</header>
<main id="main">
<div id="leftSide"></div>
<div id="rightSide"></div>
</main>
<footer id="footer">
<div id="reload">
<button onclick="window.location.reload()">Restart</button>
</div>
</footer>
<script>
window.addEventListener('load', loadGame);
function loadGame() {
let numberOfFaces = 5;
const theLeftSide = document.getElementById('leftSide');
const theRightSide = document.getElementById('rightSide');
let level = 0;
generateFaces();
function generateFaces() {
for (let i = 0; i < numberOfFaces; i++) {
const face = document.createElement('img');
face.src = 'images/smile.png';
let randomTop = Math.floor(Math.random() * 400) + 1;
let randomLeft = Math.floor(Math.random() * 400) + 1;
//console.log(randomTop);
//console.log(randomLeft);
face.style.top = randomTop + 'px';
face.style.left = randomLeft + 'px';
theLeftSide.appendChild(face);
}
const leftSideImages = theLeftSide.cloneNode(true);
/* while (theLeftSide.leftSideImages) {*/
leftSideImages.removeChild(leftSideImages.lastChild);
/* } */
theRightSide.appendChild(leftSideImages);
theLeftSide.lastChild.addEventListener('click', nextLevel);
theLeftSide.addEventListener('click', gameOver);
// document.body.addEventListener('click', gameOver);
}
function nextLevel(event) {
event.stopPropagation();
numberOfFaces += 5;
while (theLeftSide.firstChild) {
theLeftSide.removeChild(theLeftSide.firstChild);
}
while (theRightSide.firstChild) {
theRightSide.removeChild(theRightSide.firstChild);
}
level += 1;
generateFaces();
}
function gameOver() {
alert('Game Over! \n\nYou made it to level ' + level + '!');
document.body.removeEventListener('click', gameOver);
theLeftSide.lastChild.removeEventListener('click', nextLevel);
}
function reload() {
window.addEventListener('load', generateFaces);
}
}
</script>
</body>
</html>