forked from irmen/prog8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtehtriz.p8
643 lines (581 loc) · 19.1 KB
/
tehtriz.p8
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
; TehTriz - a Tetris clone.
;
; features:
; holding area
; wall kick rotations
; shows next piece
; staged speed increase
; some simple sound effects
%target c64
%import syslib
%import textio
%import test_stack
main {
const ubyte boardOffsetX = 14
const ubyte boardOffsetY = 3
const ubyte boardWidth = 10
const ubyte boardHeight = 20
const ubyte startXpos = boardOffsetX + 3
const ubyte startYpos = boardOffsetY - 2
uword lines
uword score
ubyte xpos
ubyte ypos
ubyte nextBlock
ubyte speedlevel
ubyte holding
ubyte holdingAllowed
sub start() {
c64.disable_runstop_and_charsetswitch()
;@(650) = 128 ; set all keys to repeat
sound.init()
newGame()
drawBoard()
gameOver()
newgame:
newGame()
drawBoard()
spawnNextBlock()
waitkey:
if c64.TIME_LO>=(60-4*speedlevel) {
c64.TIME_LO = 0
drawBlock(xpos, ypos, 32) ; hide block
if blocklogic.noCollision(xpos, ypos+1) {
; slowly move the block down
ypos++
drawBlock(xpos, ypos, 160) ; show block on new position
} else {
; block can't move further down!
; check if the game area is full, if not, spawn the next block at the top.
if blocklogic.isGameOver(xpos, ypos) {
gameOver()
goto newgame
} else {
sound.blockrotate()
checkForLines()
spawnNextBlock()
score++
}
}
drawScore()
; txt.plot(0,0)
; test_stack.test()
}
ubyte key=c64.GETIN()
if key==0 goto waitkey
keypress(key)
goto waitkey
}
sub move_left() {
drawBlock(xpos, ypos, 32)
if blocklogic.noCollision(xpos-1, ypos) {
xpos--
}
drawBlock(xpos, ypos, 160)
}
sub move_right() {
drawBlock(xpos, ypos, 32)
if blocklogic.noCollision(xpos+1, ypos) {
xpos++
}
drawBlock(xpos, ypos, 160)
}
sub move_down_faster() {
drawBlock(xpos, ypos, 32)
if blocklogic.noCollision(xpos, ypos+1) {
ypos++
}
drawBlock(xpos, ypos, 160)
}
sub drop_down_immediately() {
drawBlock(xpos, ypos, 32)
ubyte dropypos
for dropypos in ypos+1 to boardOffsetY+boardHeight-1 {
if not blocklogic.noCollision(xpos, dropypos) {
dropypos-- ; the furthest down that still fits
break
}
}
if dropypos>ypos {
ypos = dropypos
sound.blockdrop()
drawBlock(xpos, ypos, 160)
checkForLines()
spawnNextBlock()
score++
drawScore()
}
}
sub keypress(ubyte key) {
when key {
157, ',' -> move_left()
29, '/' -> move_right()
17, '.' -> move_down_faster()
145, ' ' -> drop_down_immediately()
'z' -> {
; no joystick equivalent (there is only 1 fire button)
; rotate counter clockwise
drawBlock(xpos, ypos, 32)
if blocklogic.canRotateCCW(xpos, ypos) {
blocklogic.rotateCCW()
sound.blockrotate()
}
else if blocklogic.canRotateCCW(xpos-1, ypos) {
xpos--
blocklogic.rotateCCW()
sound.blockrotate()
}
else if blocklogic.canRotateCCW(xpos+1, ypos) {
xpos++
blocklogic.rotateCCW()
sound.blockrotate()
}
drawBlock(xpos, ypos, 160)
}
'x' -> {
; rotate clockwise
drawBlock(xpos, ypos, 32)
if blocklogic.canRotateCW(xpos, ypos) {
blocklogic.rotateCW()
sound.blockrotate()
}
else if blocklogic.canRotateCW(xpos-1, ypos) {
xpos--
blocklogic.rotateCW()
sound.blockrotate()
}
else if blocklogic.canRotateCW(xpos+1, ypos) {
xpos++
blocklogic.rotateCW()
sound.blockrotate()
}
drawBlock(xpos, ypos, 160)
}
'c' -> {
; hold
if holdingAllowed {
sound.swapping()
if holding<7 {
drawBlock(xpos, ypos, 32)
ubyte newholding = blocklogic.currentBlockNum
swapBlock(holding)
holding = newholding
holdingAllowed = false
} else {
holding = blocklogic.currentBlockNum
drawBlock(xpos, ypos, 32)
spawnNextBlock()
}
drawHoldBlock()
}
}
}
}
sub checkForLines() {
; check if line(s) are full -> flash/clear line(s) + add score + move rest down
ubyte[boardHeight] complete_lines
ubyte num_lines=0
ubyte linepos
sys.memset(complete_lines, len(complete_lines), 0)
for linepos in boardOffsetY to boardOffsetY+boardHeight-1 {
if blocklogic.isLineFull(linepos) {
complete_lines[num_lines]=linepos
num_lines++
ubyte x
for x in boardOffsetX to boardOffsetX+boardWidth-1
txt.setcc(x, linepos, 160, 1)
}
}
if num_lines {
if num_lines>3
sound.lineclear_big()
else
sound.lineclear()
sys.wait(20) ; slight delay to flash the line
for linepos in complete_lines
if linepos and blocklogic.isLineFull(linepos)
blocklogic.collapse(linepos)
lines += num_lines
uword[] scores = [10, 25, 50, 100] ; can never clear more than 4 lines at once
score += scores[num_lines-1]
speedlevel = 1+lsb(lines/10)
drawScore()
}
}
sub gameOver() {
sound.gameover()
txt.plot(7, 7)
c64.CHROUT('U')
txt.print("────────────────────────")
c64.CHROUT('I')
txt.plot(7, 8)
txt.print("│*** g a m e o v e r ***│")
txt.plot(7, 9)
c64.CHROUT('J')
txt.print("────────────────────────")
c64.CHROUT('K')
txt.plot(7, 18)
c64.CHROUT('U')
txt.print("────────────────────────")
c64.CHROUT('I')
txt.plot(7, 19)
txt.print("│ f1 for new game │")
txt.plot(7, 20)
c64.CHROUT('J')
txt.print("────────────────────────")
c64.CHROUT('K')
ubyte key = 0
while key!=133 {
; endless loop until user presses F1 to restart the game
key = c64.GETIN()
}
}
sub newGame() {
lines = 0
score = 0
xpos = startXpos
ypos = startYpos
speedlevel = 1
nextBlock = rnd() % 7
holding = 255
holdingAllowed = true
}
sub swapBlock(ubyte newblock) {
c64.TIME_LO = 0
blocklogic.newCurrentBlock(newblock)
xpos = startXpos
ypos = startYpos
drawBlock(xpos, ypos, 160)
}
sub spawnNextBlock() {
swapBlock(nextBlock)
nextBlock = (rnd() + c64.RASTER) % 7
drawNextBlock()
holdingAllowed = true
}
sub drawBoard() {
txt.clear_screen()
txt.color(7)
txt.plot(1,1)
txt.print("* tehtriz *")
txt.color(5)
txt.plot(6,4)
txt.print("hold:")
txt.plot(2,22)
txt.print("speed: ")
txt.plot(28,3)
txt.print("next:")
txt.plot(28,10)
txt.print("lines:")
txt.plot(28,14)
txt.print("score:")
txt.color(12)
txt.plot(27,18)
txt.print("controls:")
txt.color(11)
txt.plot(28,19)
txt.print(",/ move")
txt.plot(28,20)
txt.print("zx rotate")
txt.plot(29,21)
txt.print(". descend")
txt.plot(27,22)
txt.print("spc drop")
txt.plot(29,23)
txt.print("c hold")
txt.setcc(boardOffsetX-1, boardOffsetY-2, 255, 0) ; invisible barrier
txt.setcc(boardOffsetX-1, boardOffsetY-3, 255, 0) ; invisible barrier
txt.setcc(boardOffsetX+boardWidth, boardOffsetY-2, 255, 0) ; invisible barrier
txt.setcc(boardOffsetX+boardWidth, boardOffsetY-3, 255, 0) ; invisible barrier
txt.setcc(boardOffsetX-1, boardOffsetY-1, 108, 12)
txt.setcc(boardOffsetX+boardWidth, boardOffsetY-1, 123, 12)
txt.setcc(boardOffsetX+boardWidth, boardOffsetY-1, 123, 12)
txt.setcc(boardOffsetX-1, boardOffsetY+boardHeight, 124, 12)
txt.setcc(boardOffsetX+boardWidth, boardOffsetY+boardHeight, 126, 12)
ubyte i
for i in boardOffsetX+boardWidth-1 downto boardOffsetX {
txt.setcc(i, boardOffsetY-3, 255, 0) ; invisible barrier
txt.setcc(i, boardOffsetY+boardHeight, 69, 11)
}
for i in boardOffsetY+boardHeight-1 downto boardOffsetY {
txt.setcc(boardOffsetX-1, i, 89, 11)
txt.setcc(boardOffsetX+boardWidth, i, 84, 11)
}
ubyte[] colors = [6,8,7,5,4]
for i in len(colors)-1 downto 0 {
ubyte x
for x in 5 downto 0 {
txt.setcc(6+x-i, 11+2*i, 102, colors[i])
}
}
drawScore()
}
sub drawScore() {
txt.color(1)
txt.plot(30,11)
txt.print_uw(lines)
txt.plot(30,15)
txt.print_uw(score)
txt.plot(9,22)
txt.print_ub(speedlevel)
}
sub drawNextBlock() {
const ubyte nextBlockXpos = 29
const ubyte nextBlockYpos = 5
ubyte x
for x in nextBlockXpos+3 downto nextBlockXpos {
txt.setcc(x, nextBlockYpos, ' ', 0)
txt.setcc(x, nextBlockYpos+1, ' ', 0)
}
; reuse the normal block draw routine (because we can't manipulate array pointers yet)
ubyte prev = blocklogic.currentBlockNum
blocklogic.newCurrentBlock(nextBlock)
drawBlock(nextBlockXpos, nextBlockYpos, 160)
blocklogic.newCurrentBlock(prev)
}
sub drawHoldBlock() {
const ubyte holdBlockXpos = 7
const ubyte holdBlockYpos = 6
ubyte x
for x in holdBlockXpos+3 downto holdBlockXpos {
txt.setcc(x, holdBlockYpos, '@', 0)
txt.setcc(x, holdBlockYpos+1, '@', 0)
}
if holding < 7 {
; reuse the normal block draw routine (because we can't manipulate array pointers yet)
ubyte prev = blocklogic.currentBlockNum
blocklogic.newCurrentBlock(holding)
drawBlock(holdBlockXpos, holdBlockYpos, 160)
blocklogic.newCurrentBlock(prev)
}
}
sub drawBlock(ubyte x, ubyte y, ubyte character) {
ubyte @zp i
for i in 15 downto 0 {
ubyte @zp c=blocklogic.currentBlock[i]
if c
txt.setcc((i&3)+x, (i/4)+y, character, c)
}
}
}
blocklogic {
ubyte currentBlockNum
ubyte[16] currentBlock
ubyte[16] rotated
; the 7 tetrominos
ubyte[] blockI = [0,0,0,0, ; cyan ; note: special rotation (around matrix center)
3,3,3,3,
0,0,0,0,
0,0,0,0]
ubyte[] blockJ = [6,0,0,0, ; blue
6,6,6,0,
0,0,0,0,
0,0,0,0]
ubyte[] blockL = [0,0,8,0, ; orange
8,8,8,0,
0,0,0,0,
0,0,0,0]
ubyte[] blockO = [0,7,7,0, ; yellow ; note: no rotation (square)
0,7,7,0,
0,0,0,0,
0,0,0,0]
ubyte[] blockS = [0,5,5,0, ; green
5,5,0,0,
0,0,0,0,
0,0,0,0]
ubyte[] blockT = [0,4,0,0, ; purple
4,4,4,0,
0,0,0,0,
0,0,0,0]
ubyte[] blockZ = [2,2,0,0, ; red
0,2,2,0,
0,0,0,0,
0,0,0,0]
uword[] blocks = [&blockI, &blockJ, &blockL, &blockO, &blockS, &blockT, &blockZ]
sub newCurrentBlock(ubyte block) {
currentBlockNum = block
sys.memcopy(blocks[block], currentBlock, len(currentBlock))
}
sub rotateCW() {
; rotates the current block clockwise.
if currentBlockNum==0 {
; the 'I' block rotates a 4x4 matrix around the center
rotated[0] = currentBlock[12]
rotated[1] = currentBlock[8]
rotated[2] = currentBlock[4]
rotated[3] = currentBlock[0]
rotated[4] = currentBlock[13]
rotated[5] = currentBlock[9]
rotated[6] = currentBlock[5]
rotated[7] = currentBlock[1]
rotated[8] = currentBlock[14]
rotated[9] = currentBlock[10]
rotated[10] = currentBlock[6]
rotated[11] = currentBlock[2]
rotated[12] = currentBlock[15]
rotated[13] = currentBlock[11]
rotated[14] = currentBlock[7]
rotated[15] = currentBlock[3]
sys.memcopy(rotated, currentBlock, len(currentBlock))
}
else if currentBlockNum!=3 {
; rotate all blocks (except 3, the square) around their center square in a 3x3 matrix
sys.memset(rotated, len(rotated), 0)
rotated[0] = currentBlock[8]
rotated[1] = currentBlock[4]
rotated[2] = currentBlock[0]
rotated[4] = currentBlock[9]
rotated[5] = currentBlock[5]
rotated[6] = currentBlock[1]
rotated[8] = currentBlock[10]
rotated[9] = currentBlock[6]
rotated[10] = currentBlock[2]
sys.memcopy(rotated, currentBlock, len(currentBlock))
}
}
sub rotateCCW() {
; rotates the current block counterclockwise.
if currentBlockNum==0 {
; the 'I' block rotates a 4x4 matrix around the center
rotated[0] = currentBlock[3]
rotated[1] = currentBlock[7]
rotated[2] = currentBlock[11]
rotated[3] = currentBlock[15]
rotated[4] = currentBlock[2]
rotated[5] = currentBlock[6]
rotated[6] = currentBlock[10]
rotated[7] = currentBlock[14]
rotated[8] = currentBlock[1]
rotated[9] = currentBlock[5]
rotated[10] = currentBlock[9]
rotated[11] = currentBlock[13]
rotated[12] = currentBlock[0]
rotated[13] = currentBlock[4]
rotated[14] = currentBlock[8]
rotated[15] = currentBlock[12]
sys.memcopy(rotated, currentBlock, len(currentBlock))
}
else if currentBlockNum!=3 {
; rotate all blocks (except 3, the square) around their center square in a 3x3 matrix
sys.memset(rotated, len(rotated), 0)
rotated[0] = currentBlock[2]
rotated[1] = currentBlock[6]
rotated[2] = currentBlock[10]
rotated[4] = currentBlock[1]
rotated[5] = currentBlock[5]
rotated[6] = currentBlock[9]
rotated[8] = currentBlock[0]
rotated[9] = currentBlock[4]
rotated[10] = currentBlock[8]
sys.memcopy(rotated, currentBlock, len(currentBlock))
}
}
; For movement checking it is not needed to clamp the x/y coordinates,
; because we have to check for brick collisions anyway.
; The full play area is bordered by (in)visible characters that will collide.
; Collision is determined by reading the screen data directly.
sub canRotateCW(ubyte xpos, ubyte ypos) -> ubyte {
rotateCW()
ubyte nocollision = noCollision(xpos, ypos)
rotateCCW()
return nocollision
}
sub canRotateCCW(ubyte xpos, ubyte ypos) -> ubyte {
rotateCCW()
ubyte nocollision = noCollision(xpos, ypos)
rotateCW()
return nocollision
}
sub noCollision(ubyte xpos, ubyte ypos) -> ubyte {
ubyte @zp i
for i in 15 downto 0 {
if currentBlock[i] and txt.getchr(xpos + (i&3), ypos+i/4)!=32
return false
}
return true
}
sub isGameOver(ubyte xpos, ubyte ypos) -> ubyte {
main.drawBlock(xpos, ypos, 32)
ubyte result = ypos==main.startYpos and not noCollision(xpos, ypos+1)
main.drawBlock(xpos, ypos, 160)
return result
}
sub isLineFull(ubyte ypos) -> ubyte {
ubyte x
for x in main.boardOffsetX to main.boardOffsetX+main.boardWidth-1 {
if txt.getchr(x, ypos)==32
return false
}
return true
}
sub collapse(ubyte ypos) {
while ypos>main.startYpos+1 {
ubyte x
for x in main.boardOffsetX+main.boardWidth-1 downto main.boardOffsetX {
ubyte char = txt.getchr(x, ypos-1)
ubyte color = txt.getclr(x, ypos-1)
txt.setcc(x, ypos, char, color)
}
ypos--
}
}
}
sound {
sub init() {
c64.MVOL = 15
}
sub blockrotate() {
; soft click
c64.MVOL = 5
c64.AD1 = %00100010
c64.SR1 = %00000000
c64.FREQ1 = 15600
c64.CR1 = %10000000
c64.CR1 = %10000001
}
sub blockdrop() {
; swish
c64.MVOL = 5
c64.AD1 = %01010111
c64.SR1 = %00000000
c64.FREQ1 = 4600
c64.CR1 = %10000000
c64.CR1 = %10000001
}
sub swapping() {
; beep
c64.MVOL = 8
c64.AD1 = %01010111
c64.SR1 = %00000000
c64.FREQ1 = 5500
c64.CR1 = %00010000
c64.CR1 = %00010001
}
sub lineclear() {
; explosion
c64.MVOL = 15
c64.AD1 = %01100110
c64.SR1 = %00000000
c64.FREQ1 = 1600
c64.CR1 = %10000000
c64.CR1 = %10000001
}
sub lineclear_big() {
; big explosion
c64.MVOL = 15
c64.AD1 = %01101010
c64.SR1 = %00000000
c64.FREQ1 = 2600
c64.CR1 = %10000000
c64.CR1 = %10000001
}
sub gameover() {
; buzz
c64.MVOL = 15
c64.FREQ2 = 600
c64.AD2 = %00111010
c64.SR2 = %00000000
c64.CR2 = %00110000
c64.CR2 = %00110001
}
}