-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
103 lines (87 loc) · 2.54 KB
/
script.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
/* const state = {
viwe: {
square:document.querySelectorAll(".square"),
enemy: document.querySelectorAll(".enemy"),
timeLeft: document.querySelectorAll("#time-left"),
score: document.querySelectorAll("#score"),
},
values:{
timerId: null,
gameVelocity: 1000
},
};
function randomSquare() {
state.view.squares.forEach((square)=>{
square.classList.remove("enemy");
});
let randomNumber = Math.floor(Math.random() * 9);
let randomSquare = state.view.squares[randomNumber];
randomSquare.classList.add("enemy");
}
function moveEnemy() {
state.values.timerId = setInterval(randomSquare, state.values.gameVelocity);
}
function addListenerHitBox() {
state.view.squares.forEach((square)=> {});
};
function initalize() {
randomSquare();
};
initalize(); */
const state = {
view: {
squares: document.querySelectorAll(".square"),
enemy: document.querySelector(".enemy"),
timeLeft: document.querySelector("#time-left"),
score: document.querySelector("#score"),
},
values: {
gameVelocity: 1000,
hitPosition: 0,
result: 0,
curretTime: 60,
},
actions: {
timerId: setInterval(randomSquare, 1000),
countDownTimerId: setInterval(countDown, 1000),
},
};
function countDown() {
state.values.curretTime--;
state.view.timeLeft.textContent = state.values.curretTime;
if (state.values.curretTime <= 0) {
clearInterval(state.actions.countDownTimerId);
clearInterval(state.actions.timerId);
alert("Game Over! O seu resultado foi: " + state.values.result);
}
}
function playSound(audioName) {
let audio = new Audio(`./src/audios/${audioName}.m4a`);
audio.volume = 0.2;
audio.play();
}
function randomSquare() {
state.view.squares.forEach((square) => {
square.classList.remove("enemy");
});
let randomNumber = Math.floor(Math.random() * 9);
let randomSquare = state.view.squares[randomNumber];
randomSquare.classList.add("enemy");
state.values.hitPosition = randomSquare.id;
}
function addListenerHitBox() {
state.view.squares.forEach((square) => {
square.addEventListener("mousedown", () => {
if (square.id === state.values.hitPosition) {
state.values.result++;
state.view.score.textContent = state.values.result;
state.values.hitPosition = null;
playSound("hit");
}
});
});
}
function initialize() {
addListenerHitBox();
}
initialize();