-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script1.js
123 lines (108 loc) · 4.38 KB
/
Script1.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
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
document.addEventListener('DOMContentLoaded', () => {
const theGame = () => {
STARTBUTTON.removeEventListener("click", theGame)
const properties = {
userInput: 0,
computerInput: 1,
lives: 3,
score: 0,
level: 1,
showBunnySpeed: 3000,
hideBunnySpeed: 2200,
clearIntervalStamp: null
}
const displayState = () => {
const scoreDisplay = document.getElementById("score");
const levelDisplay = document.getElementById("level");
const livesDisplay = document.getElementById("hearts");
scoreDisplay.textContent = "SCORE: " + properties.score;
levelDisplay.textContent = `LEVEL: ${properties.level}/3`;
scoreDisplay.style.background = "white";
levelDisplay.style.background = "white";
scoreDisplay.style.color = "red";
levelDisplay.style.color = "red";
livesDisplay.setAttribute("src", `img/${properties.lives}.jpg`)
};
displayState()
const resetGrid = () => {
properties.userInput = 0;
for (let i = 1; i < 10; i++) {
document.getElementById(i).setAttribute("src", "img/default.gif");
document.getElementById(i).style.border = "3px solid blue";
};
};
const listenToUserInput = () => {
for (let i = 1; i < 10; i++) {
document.getElementById(i).addEventListener("click", (function () {
properties.userInput = i;
document.getElementById(properties.userInput).style.border = "3px solid red";
}));
}
};
const generateComputerInput = () => {
properties.computerInput = (Math.floor((Math.random() * 9) + 1));
document.getElementById(properties.computerInput).setAttribute("src", `img/speed${properties.level}.gif`);
};
const checkMatch = () => {
properties.clearIntervalStamp = setTimeout(function () {
if (properties.computerInput === properties.userInput) {
properties.score += 1;
document.getElementById(properties.computerInput).setAttribute("src", "img/hit.gif");
}
else {
properties.lives -= 1;
document.getElementById(properties.computerInput).setAttribute("src", "img/miss.gif");
};
}, properties.hideBunnySpeed);
};
const gameOver = () => {
if (properties.lives === 0) {
STARTBUTTON.addEventListener("click", theGame);
for (let i = 1; i < 10; i++) {
document.getElementById(i).setAttribute("src", `img/L${i}.jpg`);
};
properties.lives = 3;
properties.score = 0;
properties.level = 1;
properties.showBunnySpeed = 3000;
properties.hideBunnySpeed = 2200;
STARTBUTTON.setAttribute("src", "img/restart.png");
clearInterval(generator);
clearInterval(properties.clearIntervalStamp);
}
};
const levelUp = () => {
if (properties.score > 9) {
properties.level = 2;
properties.showBunnySpeed = 1800;
properties.hideBunnySpeed = 1000;
return
}
if (properties.score > 19) {
properties.level = 3;
properties.showBunnySpeed = 1375;
properties.hideBunnySpeed = 575;
};
};
const won = () => {
if (properties.score === 30) {
clearInterval(generator);
for (let i = 1; i < 10; i++) {
document.getElementById(i).setAttribute("src", `img/W${i}.jpg`);
};
};
}
const generator = setInterval(function () {
displayState();
resetGrid();
listenToUserInput();
generateComputerInput();
checkMatch();
levelUp();
gameOver();
won();
}, properties.showBunnySpeed);
};
const STARTBUTTON = document.getElementById("hearts");
STARTBUTTON.addEventListener("click", theGame);
});