-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmole.js
74 lines (66 loc) · 1.75 KB
/
mole.js
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
let currSebastianTile;
let currundertakerTile;
let score = 0;
let gameOver = false;
window.onload = function() {
setGame();
}
function setGame() {
for (let i = 0; i < 9; i++) {
let tile = document.createElement("div");
tile.id = i.toString();
tile.addEventListener("click", selectTile);
document.getElementById("board").appendChild(tile);
}
setInterval(setMole, 1000);
setInterval(setPlant, 2000);
}
function getRandomTile() {
let num = Math.floor(Math.random() * 9);
return num.toString();
}
function setMole() {
if (gameOver) {
return;
}
if (currSebastianTile) {
currSebastianTile.innerHTML = "";
}
let seba = document.createElement("img");
seba.src = "./Sebastian.png";
let num = getRandomTile();
if (currundertakerTile && currundertakerTile.id == num) {
return;
}
currSebastianTile = document.getElementById(num);
currSebastianTile.appendChild(seba);
}
function setPlant() {
if (gameOver) {
return;
}
if (currundertakerTile) {
currundertakerTile.innerHTML = "";
}
let un = document.createElement("img");
un.src = "./undertaker.png";
let num = getRandomTile();
if (currSebastianTile && currSebastianTile.id == num) {
return;
}
currundertakerTile = document.getElementById(num);
currundertakerTile.appendChild(un);
}
function selectTile() {
if (gameOver) {
return;
}
if (this == currSebastianTile) {
score += 10;
document.getElementById("score").innerText = score.toString();
}
else if (this == currundertakerTile) {
document.getElementById("score").innerText = "GAME OVER: " + score.toString();
gameOver = true;
}
}