-
Notifications
You must be signed in to change notification settings - Fork 0
/
mole.js
95 lines (82 loc) · 2.54 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
document.addEventListener('DOMContentLoaded', function() {
const cursor = document.querySelector('.cursor');
window.addEventListener('mousemove', e => {
cursor.style.top = e.pageY + 'px';
cursor.style.left = e.pageX + 'px';
});
window.addEventListener('mousedown', () => {
cursor.classList.add('active');
});
window.addEventListener('mouseup', () => {
cursor.classList.remove('active');
});
});
let currMoleTile;
let currPlantTile;
let score = 0;
let gameOver = false;
window.onload = function() {
setGame();
}
function setGame() {
//set up the grid in html
for (let i = 0; i < 9; i++) { //i goes from 0 to 8, stops at 9
//<div id="0-8"></div>
let tile = document.createElement("div");
tile.id = i.toString();
tile.addEventListener("click", selectTile);
document.getElementById("board").appendChild(tile);
}
setInterval(setMole, 910); // 1000 miliseconds = 1 second, every 1 second call setMole
setInterval(setPlant, 2000); // 2000 miliseconds = 2 seconds, every 2 second call setPlant
}
function getRandomTile() {
//math.random --> 0-1 --> (0-1) * 9 = (0-9) --> round down to (0-8) integers
let num = Math.floor(Math.random() * 9);
return num.toString();
}
function setMole() {
if (gameOver) {
return;
}
if (currMoleTile) {
currMoleTile.innerHTML = "";
}
let mole = document.createElement("img");
mole.src = "./thanos.png";
let num = getRandomTile();
if (currPlantTile && currPlantTile.id == num) {
return;
}
currMoleTile = document.getElementById(num);
currMoleTile.appendChild(mole);
}
function setPlant() {
if (gameOver) {
return;
}
if (currPlantTile) {
currPlantTile.innerHTML = "";
}
/*let plant = document.createElement("img");
plant.src = "./thanos.png";*/
let num = getRandomTile();
if (currMoleTile && currMoleTile.id == num) {
return;
}
currPlantTile = document.getElementById(num);
currPlantTile.appendChild(plant);
}
function selectTile() {
if (gameOver) {
return;
}
if (this == currMoleTile) {
score += 10;
document.getElementById("score").innerText = score.toString(); //update score html
}
else if (this == currPlantTile) {
document.getElementById("score").innerText = "GAME OVER: " + score.toString(); //update score html
gameOver = true;
}
}