-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17d5c86
commit a4eecef
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
let currMoleTile; // Declaring currMoleTile globally | ||
let currPlantTile; // Declaring currPlantTile globally | ||
let score = 0; | ||
let gameOver = false; | ||
|
||
window.onload = function() { | ||
setGame(); | ||
}; | ||
|
||
////making fuunction for setting game field | ||
function setGame() { | ||
//set up the grid for game board in html | ||
for(let i = 0; i < 9; i++) { ////i g0es from 0 to 8, stops at 9 | ||
// making div <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, 2000); ////2000 milisonds = 2 seconds me setmole ko call krega | ||
setInterval(setPlant, 3000); | ||
}; | ||
|
||
function getRandomTile() { | ||
//math.random ---> (0-1) * 9 = (0-9) --> round down to (0-8) integers | ||
let num = Math.floor(Math.random() * 9); | ||
return num.toString(); | ||
}; | ||
|
||
function setMole() { | ||
|
||
if (currMoleTile) { | ||
currMoleTile.innerHTML = ""; | ||
} | ||
|
||
let mole = document.createElement("img"); | ||
mole.src = "./images/mole.png"; | ||
|
||
let num = getRandomTile(); | ||
if (currPlantTile && currPlantTile.id == num) { | ||
return; | ||
} | ||
currMoleTile = document.getElementById(num); | ||
currMoleTile.appendChild(mole); | ||
}; | ||
|
||
function setPlant() { | ||
if (currPlantTile) { | ||
currPlantTile.innerHTML = ""; | ||
} | ||
|
||
let plant = document.createElement("img"); | ||
plant.src = "./images/piranha-plant.png"; | ||
let num = getRandomTile(); | ||
if (currMoleTile && currMoleTile.id == num) { | ||
return; | ||
} | ||
currPlantTile = document.getElementById(num); | ||
currPlantTile.appendChild(plant); | ||
}; | ||
|
||
function selectTile() { | ||
if (this == currMoleTile) { | ||
score +=10; | ||
document.getElementById("score").innerText = score.toString(); | ||
} | ||
else if (this == currPlantTile) { | ||
document.getElementById("score").innerText = "GAME OVER: " + score.toString(); | ||
gameOver = true; | ||
} | ||
} |
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters