-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyMatchingGame.html
90 lines (79 loc) · 3.07 KB
/
MyMatchingGame.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
<!DOCTYPE html>
<html>
<head>
<title>Week 3 Game</title>
<meta name="author" content="Joe Perez">
<style>
img {
position: absolute
/* height: 50px
width: 50px */
}
div {
position: absolute
}
#rightSide {
left: 500px;
height: 500px;
border-left: 1px solid black
}
#leftSide {
left: 0px;
height: 500px;
}
</style>
</head>
<body onload="generateFaces()">
<h1>Matching Game</h1>
<h2 id="theLevel"></h2> <!-- NEW -->
<p>Click on the extra smile face on the left.</p>
<div id="leftSide"></div>
<div id="rightSide"></div>
<script>
var theBody = document.getElementsByTagName("body")[0];
var numberOfFaces = 5;
var numberOfLevels = 4; // NEW
var theLevel = 0;
var theLeftSide = document.getElementById("leftSide");
function generateFaces() {
theLevel += 1; // NEW
var theLevelElement = document.getElementById('theLevel'); //NEW
theLevelElement.innerHTML = "Round " + theLevel; //NEW
for (i = 0; i < numberOfFaces; i++) {
var randomx = Math.floor(Math.random() * 400);
var randomy = Math.floor(Math.random() * 400);
var smiley = document.createElement("img");
smiley.setAttribute('src', 'smile.png');
smiley.setAttribute('style', 'top:' + randomy + 'px;' + 'left:' + randomx + 'px');
document.getElementById("leftSide").appendChild(smiley);
}
var theRightSide = document.getElementById('rightSide');
var leftSideImages = document.getElementById('leftSide').cloneNode(true);
leftSideImages.removeChild(leftSideImages.lastChild);
document.getElementById('rightSide').appendChild(leftSideImages);
var nExtra = 2; // NEW
theLeftSide.lastChild.onclick = function nextLevel(event) {
event.stopPropagation();
removeItems("leftSide"); // CHANGE
removeItems("rightSide"); // CHANGE
// debugger;
numberOfFaces += nExtra; // CHANGE
generateFaces();
};
function removeItems(nodeName) {
// debugger;
console.log("remove" + nodeName);
var theNode = document.getElementById(nodeName);
while (theNode.firstChild) {
theNode.removeChild(theNode.firstChild);
}
}
theBody.onclick = function gameOver() {
alert("Game Over!");
theBody.onclick = null;
theLeftSide.lastChild.onclick = null;
};
}
</script>
</body>
</html>