-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
164 lines (118 loc) · 4.28 KB
/
app.js
File metadata and controls
164 lines (118 loc) · 4.28 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
let currentRowNumber = 1;
let currentRow = document.getElementById(`row${currentRowNumber}`);
let chainLength = 4;
let displacement = 0; //displacement is where the chain of glowing blocks starts. Even though it is 0, the chain starts in the middle at the 4th block from the left.
let direction = 'right';
let score = 0;
let scoreDisplay = document.getElementById('score');
let scoreSound = document.getElementById('scoreSound');
let overhangSound = document.getElementById('overhang');
//I initialize overhang here so I can access it later.
let overhang;
//This function adds the chain of glowing blocks on the current row.
function addGlowingBlocks(startingIndex) {
let start = 4 + startingIndex;
let spaces = currentRow.querySelectorAll('.space');
for (let i = 0; i < 12; i++) {
if (i >= start && i <= start + chainLength - 1) {
spaces[i].classList.add('glow');
}
}
}
//This function moves the chain of blocks by one displacement to the right or left depending on if it reached the end.
function moveChain(begin) {
let chain = currentRow.querySelectorAll('.glow');
//If the chain has reached the right wall, change direction to left.
if (displacement >= 8 - chainLength) direction = 'left';
//If the chain has reached the left wall, change direction to right.
if (displacement <= -4) direction = 'right';
if (direction == 'right') displacement += 1;
if (direction == 'left') displacement -= 1;
chain.forEach(block => block.style.transform = `translateX(${(displacement - (lowerRowIndex || 0)) * 100}%)`);
}
let timeBetweenTicks = 500;
let move = setInterval(moveChain, timeBetweenTicks);
//lowerRowIndex is the ending displacement of the row below.
let lowerRowIndex;
//trimChain trims the overhanging blocks in the current chain.
function trimChain() {
//overhangSide specifies which side the chain of blocks hangs over.
let overhangSide;
if(displacement < lowerRowIndex) {
overhangSide = 'left';
chainLength = chainLength - (lowerRowIndex - displacement);
}
if (displacement > lowerRowIndex) {
overhangSide = 'right';
chainLength = chainLength - (displacement - lowerRowIndex);
}
//spaces is the list of all the spaces in the current row
let spaces = currentRow.querySelectorAll('.space');
//start and pos go from 0 to 11 to match the index of spaces
let start = 4 + (displacement); //start is the index where the current row's chain starts
let pos = 4 + (lowerRowIndex); //pos is the index the lower row's chain starts
overhang = start - pos || 0;
if (overhangSide == 'left') {
for (let i = 0; i < spaces.length; i++) {
if (i < pos - overhang) {
spaces[i].classList.remove('glow');
}
}
lowerRowIndex = displacement - overhang;
} else if (overhangSide == 'right') {
for (let i = 0; i < spaces.length; i++) {
if (i >= pos + chainLength) {
spaces[i].classList.remove('glow');
}
}
lowerRowIndex = displacement;
} else {
lowerRowIndex = displacement;
}
}
function incrementScore() {
score++;
scoreDisplay.innerHTML = `Score: ${score}`;
if(overhang != 0) {
overhangSound.play();
} else {
scoreSound.play();
}
}
function checkGameOver() {
if (chainLength <= 0) {
let restart = confirm('Game over! Would you like to play again?');
if (restart) location.reload();
}
}
//collapseTop brings the chain down to the bottom when it reaches the top.
function collapseTop() {
currentRowNumber = 1;
currentRow = document.getElementById(`row${currentRowNumber}`);
let highlighted = document.querySelectorAll('.glow');
highlighted.forEach(block => block.classList.remove('glow'));
addGlowingBlocks(lowerRowIndex);
}
//stack starts a new chain on the next row.
function stack() {
if (currentRowNumber == 12) collapseTop();
currentRowNumber++;
currentRow = document.getElementById(`row${currentRowNumber}`);
addGlowingBlocks(lowerRowIndex);
timeBetweenTicks = timeBetweenTicks * 0.85;
clearInterval(move);
move = setInterval(moveChain, timeBetweenTicks);
}
document.addEventListener('click', (event) => {
trimChain();
checkGameOver();
incrementScore();
stack();
});
document.addEventListener('keyup', event => {
if (event.key != ' ') return;
trimChain();
checkGameOver();
incrementScore();
stack();
} )