-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
306 lines (306 loc) · 5.67 KB
/
main.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
function lockPiece () {
for (let y8 = 0; y8 <= currentPiece.length - 1; y8++) {
for (let x8 = 0; x8 <= currentPiece[y8].length - 1; x8++) {
if (currentPiece[y8][x8] == 1) {
grid[currentY + y8][currentX + x8] = 1
}
}
}
}
function rotatePieceRight () {
let newPiece: number[][] = []
for (let x4 = 0; x4 <= currentPiece[0].length - 1; x4++) {
newPiece.push([])
for (let index = 0; index < currentPiece.length; index++) {
newPiece[x4].push(0)
}
}
for (let y5 = 0; y5 <= currentPiece.length - 1; y5++) {
for (let x5 = 0; x5 <= currentPiece[y5].length - 1; x5++) {
newPiece[x5][currentPiece.length - y5 - 1] = currentPiece[y5][x5]
}
}
if (canMove(0, 0)) {
clearPiece()
currentPiece = newPiece
drawPiece()
}
}
function spawnNewPiece () {
currentPiece = tetris[Math.randomRange(0, tetris.length - 1)]
currentX = 1
currentY = 0
if (!(canMove(0, 0))) {
gameOver = true
basic.showString("Game Over")
} else {
drawPiece()
}
}
// 0 = no rotation, 1 = right, -1 = left
function drawPiece () {
for (let y = 0; y <= currentPiece.length - 1; y++) {
for (let x = 0; x <= currentPiece[y].length - 1; x++) {
if (currentPiece[y][x] == 1) {
led.plot(currentX + x, currentY + y)
}
}
}
}
function clearLines () {
for (let y9 = 0; y9 <= 4; y9++) {
fullLine = true
for (let x9 = 0; x9 <= 4; x9++) {
if (grid[y9][x9] == 0) {
fullLine = false
break;
}
}
if (fullLine) {
for (let row = y9; row > 0; row--) {
for (let col = 0; col < 5; col++) {
grid[row][col] = grid[row - 1][col]
}
}
for (let col2 = 0; col2 <= 4; col2++) {
grid[0][col2] = 0
}
}
}
}
input.onButtonPressed(Button.A, function () {
movePiece(-1, 0)
})
function playSong () {
for (let row2 of songGrid) {
for (let note of row2) {
if (notes[note] != undefined) {
music.playTone(notes[note], music.beat(BeatFraction.Quarter))
// Adjust timing between notes as needed
basic.pause(200)
}
}
}
}
function clearPiece () {
for (let y2 = 0; y2 <= currentPiece.length - 1; y2++) {
for (let x2 = 0; x2 <= currentPiece[y2].length - 1; x2++) {
if (currentPiece[y2][x2] == 1) {
led.unplot(currentX + x2, currentY + y2)
}
}
}
}
function rotatePieceLeft () {
let newPiece2: number[][] = []
for (let x6 = 0; x6 <= currentPiece[0].length - 1; x6++) {
newPiece2.push([])
for (let index = 0; index < currentPiece.length; index++) {
newPiece2[x6].push(0)
}
}
for (let y7 = 0; y7 <= currentPiece.length - 1; y7++) {
for (let x7 = 0; x7 <= currentPiece[y7].length - 1; x7++) {
newPiece2[currentPiece[y7].length - x7 - 1][y7] = currentPiece[y7][x7]
}
}
if (canMove(0, 0)) {
clearPiece()
currentPiece = newPiece2
drawPiece()
}
}
input.onButtonPressed(Button.AB, function () {
rotatePiece()
})
input.onButtonPressed(Button.B, function () {
movePiece(1, 0)
})
function canMove (dx: number, dy: number) {
for (let y3 = 0; y3 <= currentPiece.length - 1; y3++) {
for (let x3 = 0; x3 <= currentPiece[y3].length - 1; x3++) {
if (currentPiece[y3][x3] == 1) {
newX = currentX + x3 + dx
newY = currentY + y3 + dy
if (newX < 0 || newX >= 5 || newY < 0 || newY >= 5 || grid[newY][newX] == 1) {
return false
}
}
}
}
return true
}
function rotatePiece () {
if (rotationDirection == 1) {
rotatePieceRight()
} else if (rotationDirection == -1) {
rotatePieceLeft()
}
// Reset rotation direction after rotation
rotationDirection = 0
}
function movePiece (dx: number, dy: number) {
if (canMove(dx, dy)) {
clearPiece()
currentX += dx
currentY += dy
drawPiece()
} else if (dy != 0) {
lockPiece()
clearLines()
spawnNewPiece()
}
}
let newY = 0
let newX = 0
let fullLine = false
let gameOver = false
let currentY = 0
let songGrid: string[][] = []
let currentX = 0
let currentPiece: number[][] = []
let grid: number[][] = []
let tetris: number[][][] = []
// 0 = no rotation, 1 = right, -1 = left
let rotationDirection = 0
// Z piece
// S piece
// O piece
// I piece
// L piece
tetris = [
[[1, 1, 0], [0, 1, 1]],
[[0, 1, 1], [1, 1, 0]],
[[1, 1], [1, 1]],
[[
1,
1,
1,
1
]],
[[1, 1, 1], [1, 0, 0]]
]
grid = [
[
0,
0,
0,
0,
0
],
[
0,
0,
0,
0,
0
],
[
0,
0,
0,
0,
0
],
[
0,
0,
0,
0,
0
],
[
0,
0,
0,
0,
0
]
]
currentPiece = tetris[Math.randomRange(0, tetris.length - 1)]
currentX = 1
let interval = 1000
let notes: { [key: string]: number } = {
'A': Note.A,
'B': Note.B,
'C': Note.C,
'D': Note.D,
'E': Note.E,
'F': Note.F,
'G': Note.G
}
songGrid = [
[
"E",
"B",
"C",
"D"
],
[
"E",
"D",
"C",
"B"
],
[
"A",
"A",
"C",
"E"
],
[
"E",
"D",
"C",
"B"
],
[
"A",
"B",
"C",
"D"
],
[
"E",
"C",
"A",
"A"
],
[
"D",
"F",
"A",
"D"
],
[
"G",
"F",
"E",
"C"
],
[
"E",
"E",
"G",
"B"
],
[
"A",
"G",
"F",
"E"
]
]
basic.forever(function () {
if (!(gameOver)) {
basic.pause(interval)
movePiece(0, 1)
}
})
basic.forever(function () {
if (!(gameOver)) {
playSong()
// Adjust the pause to match the length of the custom theme
basic.pause(8000)
}
})