-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
143 lines (132 loc) · 5.11 KB
/
app.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
window.onload = () => {
const gameContainer = document.getElementById('game-container');
const livesContainer = document.getElementById('lives-container');
const scoreContainer = document.getElementById('score-container');
const gameContainerWidth = gameContainer.clientWidth;
const gameContainerHeight = gameContainer.clientHeight;
const settings = {
rows: 3,
columns: 3,
lives: 3,
score: 0,
interval: 3000
};
const elements = [];
const gridNumber = settings.rows * settings.columns;
const gridFragment = document.createDocumentFragment();
const livesFragment = document.createDocumentFragment();
for (let i = 0; i < gridNumber; i++) {
const gridElement = document.createElement('div');
gridElement.classList.add('grid');
gridElement.style.height = ((gameContainerWidth / 3) - 20) + 'px';
gridFragment.appendChild(gridElement);
}
gameContainer.append(gridFragment);
const grids = Array.from(document.querySelectorAll('.grid'));
let lastClickedZenon = 0;
let lastRandom = 0;
let randomIndex = 0;
const getRandomNumber = number => {
let randomNumber = Math.floor(Math.random() * number);
// if (lastRandom !== randomNumber) {
// lastRandom = randomNumber;
// return lastRandom;
// } else {
// getRandomNumber(number);
// }
lastRandom = randomNumber;
return randomNumber;
};
const zenon = (width, height, color, index, multiply) => {
const fragment = document.createDocumentFragment();
const zenek = document.createElement('div');
zenek.classList.add('zenek');
zenek.dataset.index = index;
zenek.style.background = color;
zenek.style.height = height + 'px';
zenek.style.width = width + 'px';
return fragment.appendChild(zenek);
}
let clicked = false;
const renderElement = (index) => {
const element = zenon(grids[0].offsetWidth / 2, grids[0].offsetHeight / 2, 'white', index);
grids[index].append(element);
}
setInterval(() => {
if (!clicked) {
for (let el = 0; el < elements.length; el++) {
grids[elements[el]].innerHTML = '';
settings.lives--;
settings.score--;
scoreContainer.innerText = settings.score;
clicked = false;
rerenderLives(settings.lives);
elements[el] = getRandomNumber(gridNumber);
renderElement(elements[el]);
}
} else {
clicked = false;
}
}, settings.interval);
const rerenderLives = (lives) => {
livesContainer.innerHTML = '';
for (let i = 0; i < lives; i++) {
const liveElement = document.createElement('div');
liveElement.classList.add('live');
livesFragment.appendChild(liveElement);
}
livesContainer.append(livesFragment);
}
let lastAddedScore = 0;
const addOneMore = () => {
if (settings.score === 2 && lastAddedScore === 0) {
lastAddedScore = settings.score;
elements.push(getRandomNumber(gridNumber));
// for (let e = 0; e < elements.length; e++) {
// renderElement(elements[e]);
// }
}
if (settings.score === (lastAddedScore * (lastAddedScore / 5)) && lastAddedScore > 0 && settings.rows >= elements.length) {
lastAddedScore = settings.score;
elements.push(getRandomNumber(gridNumber));
for (let e = 0; e < elements.length; e++) {
renderElement(elements[e]);
}
}
console.log(settings.score, (lastAddedScore * (lastAddedScore / 5)))
}
addEventListener('click', event => {
const target = event.target;
console.log([target]);
if (target.classList.contains('zenek')) {
settings.score++;
scoreContainer.innerText = settings.score;
clicked = true;
addOneMore();
// rerenderLives(settings.lives);
target.parentElement.innerHTML = '';
for (let el = 0; el < elements.length; el++) {
lastClickedZenon = elements[el];
elements[el] = getRandomNumber(gridNumber);
renderElement(elements[el]);
// grids[elements[el]].innerHTML = '';
// settings.score++;
// scoreContainer.innerText = settings.score;
// clicked = true;
// rerenderLives(settings.lives);
// elements[el] = getRandomNumber(gridNumber);
// renderElement(elements[el]);
}
console.log(lastClickedZenon)
} else {
settings.lives--;
settings.score--;
scoreContainer.innerText = settings.score;
clicked = true;
rerenderLives(settings.lives);
}
}, true);
elements.push(getRandomNumber(gridNumber));
renderElement(elements[0]);
rerenderLives(settings.lives);
}