-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfallingblocks.ts
330 lines (296 loc) · 9.61 KB
/
fallingblocks.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// this file was derived from
/**
* Minos
* Built on
* MakeCode Arcade JavaScript Template v. 2.2
* Last update: 05 Jun 2019 ak
* (C) 2019 Robo Technical Group LLC
MIT License
Copyright 2019 Robo Technical Group LLC.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Game modes
enum GameMode {
Animating,
Main,
NotReady,
Processing,
Over
}
interface Pixel {
x: number
y: number
}
interface PolyominoSprite {
index: number
sprite: Sprite
}
const AUTODROP_INTERVAL: number = 25 // milliseconds
const DROP_INTERVAL_INITIAL: number = 1000 // milliseconds
const LEVEL_TIMER_FACTOR: number = 0.85
const LINES_PER_LEVEL: number = 5
const GRID_SIZE_NEXT_POLY: number = 6
const SCORE_PER_LINE: number = 10
const SCORE_PER_POLY: number = 1
const STARTING_LEVELS: number[] = [1, 5, 10]
let autoDrop: boolean = false
let currPoly: ActivePolyomino = null
let nextPoly: number = -1;
let fullRows: number[] = [] // set of completed rows, to be eliminated
let dropInterval: number = 0
let gameMode: GameMode = GameMode.NotReady
let linesCleared: number = 0
let nextAnimate: number = 0
let nextLevel: number = 0
let numFlips: number = 0
// TODO: problems
// - blocks stop falling
// - glitch at bottom
// - next block not showing
function startFallingBlocks(): void {
gameMode = GameMode.NotReady
initGame()
startNextPoly()
updateScreen()
gameMode = GameMode.Main;
initHandlers();
screen.show();
}
function runtime() {
return control.millis();
}
function initHandlers() {
forever(function () {
switch (gameMode) {
case GameMode.Animating:
if (runtime() >= nextAnimate) {
flipScreens()
}
break
case GameMode.Main:
if (nextPoly > -1 && runtime() >= currPoly.nextDrop) {
currPoly.change.row = 1
updateScreen()
}
break
}
})
// AB = rotate
input.onButtonPressed(Button.AB, function () {
switch (gameMode) {
case GameMode.Main:
let poly: Polyomino = gameShapes[currPoly.index]
currPoly.orientation++
if (currPoly.orientation >= poly.blocks.length) {
currPoly.orientation = 0
}
updateScreen()
break
}
})
// shake = fall quickly
input.onGesture(Gesture.Shake, function () {
switch (gameMode) {
case GameMode.Main:
autoDrop = true
currPoly.change.row = 1
updateScreen()
break
}
})
// move left
input.onButtonPressed(Button.B, function () {
switch (gameMode) {
case GameMode.Main:
currPoly.change.column = -1
updateScreen()
break
}
})
// move right
input.onButtonPressed(Button.A, function () {
switch (gameMode) {
case GameMode.Main:
currPoly.change.column = 1
updateScreen()
break
}
})
}
function animateClearLines(): void {
drawGameState()
numFlips = 0
nextAnimate = runtime() + dropInterval / 2
gameMode = GameMode.Animating
}
function clearLines(): void {
fullRows = []
for (let rowNum: number = ROWS - 1; rowNum >= 0; rowNum--) {
let row: number[] = gameState[rowNum]
let count: number = 0
for (let cell of row) {
if (cell > -1) {
count++
}
}
if (count >= COLUMNS) {
fullRows.push(rowNum)
}
}
if (fullRows.length > 0) {
// Clear the full rows for the animation
for (let row of fullRows) {
for (let col: number = 0; col < COLUMNS; col++) {
setBlock(row, col, -1)
}
}
animateClearLines()
}
}
function flipScreens(): void {
nextAnimate = runtime() + dropInterval / 2
numFlips++
if (numFlips === 3) {
shiftLines()
updateNextPolySprite()
gameMode = GameMode.Main
}
screen.show();
}
function initGame(): void {
gameShapes = TETROMINOES;
initVars()
initGameState()
}
function initVars(): void {
fullRows = []
linesCleared = 0
nextLevel = LINES_PER_LEVEL
autoDrop = false
// info.setScore(0)
let startLevel: number = STARTING_LEVELS[0]
dropInterval = Math.round(DROP_INTERVAL_INITIAL * LEVEL_TIMER_FACTOR ** (startLevel - 1))
// info.setLife(startLevel)
}
function levelUp(): void {
nextLevel += LINES_PER_LEVEL
dropInterval = Math.floor(dropInterval * LEVEL_TIMER_FACTOR)
// info.changeLifeBy(1)
}
function shiftLines(): void {
linesCleared += fullRows.length
// info.changeScoreBy(SCORE_PER_LINE * 2 ** (fullRows.length - 1))
let shiftAmount: number = 1
for (let index: number = 0; index < fullRows.length; index++) {
let startRow: number = fullRows[index] - 1
let endRow: number =
index === fullRows.length - 1
? 0
: fullRows[index + 1] + 1
for (let row: number = startRow; row >= endRow; row--) {
copyLine(row, row + shiftAmount)
}
shiftAmount++
}
if (linesCleared >= nextLevel) {
levelUp()
}
}
function startNextPoly(): void {
autoDrop = false
if (currPoly) { // the current one is done moving
setPoly(currPoly) // place in state permanently
clearLines() // and clear any completed lines
} else {
// Start of game
nextPoly = Math.randomRange(0, gameShapes.length - 1)
}
let newPoly: Polyomino = gameShapes[nextPoly]
currPoly = {
change: { column: 0, row: 0 },
index: nextPoly,
location: {
column: Math.floor((COLUMNS - newPoly.blocks[0][0].length) / 2),
row: 0 - newPoly.blocks[0].length // starts above screen
},
nextDrop: 0,
orientation: 0
}
nextPoly = Math.randomRange(0, gameShapes.length - 1)
// If we're clearing lines, update the Next Poly sprite later
if (gameMode !== GameMode.Animating) {
updateNextPolySprite()
}
}
function updateNextPolySprite(): void {
let poly: Polyomino = gameShapes[nextPoly]
for(let c=12;c<16;c++)
for(let r=0;r<SCREEN_HEIGHT;r++)
screen.setPixel(c, r, 0);
drawPoly(poly, 4, 11 +
Math.floor((6 - poly.blocks[0][0].length) / 2));
}
function updateScreen(): void {
gameMode = GameMode.Processing
// Update the next drop time if we are moving the poly down
if (currPoly.change.row > 0) {
if (autoDrop) {
currPoly.nextDrop = runtime() + AUTODROP_INTERVAL
} else {
currPoly.nextDrop = runtime() + dropInterval
}
}
// update the location of the current poly
if (currPoly.change.row !== 0 || currPoly.change.column !== 0) {
currPoly.location.column += currPoly.change.column
currPoly.location.row += currPoly.change.row
}
// test to see if the poly can be moved as requested
if (setPoly(currPoly, false, true)) {
setPoly(currPoly); // put the current polyomino into game state
drawGameState(); // update the grid on the screen
setPoly(currPoly, true); // remove current polyomino from game state
} else {
// Cannot accommodate requested change
// Undo change
if (currPoly.change.row !== 0 || currPoly.change.column !== 0) {
currPoly.location.column -= currPoly.change.column
currPoly.location.row -= currPoly.change.row
} else {
// Orientation change requested; reset
let poly: Polyomino = gameShapes[currPoly.index]
currPoly.orientation--
if (currPoly.orientation < 0) {
currPoly.orientation = poly.blocks.length - 1
}
}
if (currPoly.change.row > 0 && currPoly.change.column === 0) {
// Attempted to drop poly but could not
if (currPoly.location.row < 0) {
gameMode = GameMode.Over;
screen.setAll(toRGB(Color.Red));
} else {
// Set it and then create a new poly
// info.changeScoreBy(SCORE_PER_POLY)
startNextPoly()
}
}
}
currPoly.change.column = 0;
currPoly.change.row = 0;
// If we're clearing lines, then do not switch back to main game mode
if (gameMode === GameMode.Processing) {
gameMode = GameMode.Main
}
screen.show();
}