Blocks - Tetris Clone - Purebasic

Posted on by Kenny Cason
tags = [ blocks, game, programming, purebasic, tetris ]

Blocks is a Tetris like game written in Purebasic.

Blocks features 3 modes:

View on GitHub


Blocks was compiled using Purebasic v3.94, However, It should be fairly easy to make changes to get it to recompile in the most recent version, or just to convert it into another language.

Blocks 2D Tetris clone Blocks 2D Tetris clone Blocks 2D Tetris clone
Blocks 2D Tetris clone Blocks 2D Tetris clone

Credits

Programmer
- Kenny Cason
Graphics Design
- Kenny Cason
Music
- Nintendo
Beta Testers
- Kenny Cason
- David Johnson

My old procedural code :D

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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
If InitSprite()
Else
  MessageRequester("Blocks","DirectX v.7 or later was unable to be initialized",#MB_ICONERROR)
  End
EndIf
If InitKeyboard()
Else
  MessageRequester("Blocks","A keyboard was unable to be initialized!",#MB_ICONERROR)
  End
EndIf
If InitSound()
  UseOGGSoundDecoder()
Else
  MessageRequester("Blocks","Sound was unable to be initialized!",#MB_ICONERROR)
  End
EndIf

If InitJoystick()
  joystick = 1
Else
  joystick = 0
EndIf


OnErrorResume()

 Enumeration ; sprites
   #Block = 1;
   #BorderL = 30
   #BorderH
   #BorderTC
   #BorderV
   #BorderBC
   #Next
   #Numbers = 40
   #Letters = 50
 EndEnumeration

 Enumeration ; sound
   #Line = 1
   #Tetris
   #Blk
   #Rotate
   #lost
   #pause
   #musicA
   #musicB
   #musicC
 EndEnumeration

Procedure BitmapText(Text$,x,y)

  Text$ = UCase(Text$)
  Length = Len(Text$)
  For k=1 To Length
    A$ = Mid(Text$, k,1)
    If A$=" "
      x+20
    Else
      v = Asc(A$)
      If v>=65 And v<=90
        v = #Letters + v - 65 ; 65 ASCII 'A'
      ElseIf v>=48 And v<=57
        v = #Numbers + v - 48 ; 48 ASCII '0'
      ElseIf v=33
        v =  #Letters+26 ; 33 ASCII '!'
      EndIf
      If IsSprite(v)
        DisplayTransparentSprite(v,x,y)
        x+SpriteWidth(v)
      EndIf
    EndIf
  Next
EndProcedure

Procedure.s getName()
  name$ = InputRequester("Blocks - You have a new Hi Score!","Enter your name","")
  name$ = Trim(name$)
  If Len(name$)>10
    name$ = Mid(name$,1,10)
  EndIf
  ProcedureReturn name$
EndProcedure

Define.l score, highscore

Structure piece
  image.w
  b1x.w
  b1y.w
  b2x.w
  b2y.w
  b3x.w
  b3y.w
  b4x.w
  b4y.w
  b5x.w
  b5y.w
  b6x.w
  b6y.w
EndStructure

np.piece
cp.piece

Width = 15
Height = 25
Dim blocks.b(Width, Height)


 DataSection     ; ********* a includebinary file bigger .exe but noone can steal stuff :)

   blk1: IncludeBinary "images\1.bmp"
   blk2: IncludeBinary "images\2.bmp"
   blk3: IncludeBinary "images\3.bmp"
   blk4: IncludeBinary "images\4.bmp"
   blk5: IncludeBinary "images\5.bmp"
   blk6: IncludeBinary "images\6.bmp"
   blk7: IncludeBinary "images\7.bmp"
   blk8: IncludeBinary "images\8.bmp"
   blk9: IncludeBinary "images\9.bmp"
   blk10: IncludeBinary "images\10.bmp"
   blk11: IncludeBinary "images\11.bmp"
   blk12: IncludeBinary "images\12.bmp"
   blk13: IncludeBinary "images\13.bmp"
   blk14: IncludeBinary "images\14.bmp"
   blk15: IncludeBinary "images\15.bmp"
   blk16: IncludeBinary "images\16.bmp"
   blk17: IncludeBinary "images\17.bmp"
   blk18: IncludeBinary "images\18.bmp"
   blk19: IncludeBinary "images\19.bmp"
   blk20: IncludeBinary "images\20.bmp"
   blk21: IncludeBinary "images\21.bmp"
   blk22: IncludeBinary "images\22.bmp"
   blk23: IncludeBinary "images\23.bmp"
   blk24: IncludeBinary "images\24.bmp"
   blk25: IncludeBinary "images\25.bmp"
   blk26: IncludeBinary "images\26.bmp"
   blk27: IncludeBinary "images\27.bmp"
   blk28: IncludeBinary "images\28.bmp"
   blk29: IncludeBinary "images\29.bmp"

   bdrL: IncludeBinary "images\bdrL.bmp"
   bdrH: IncludeBinary "images\bdrH.bmp"
   bdrTC: IncludeBinary "images\bdrTC.bmp"
   bdrV: IncludeBinary "images\bdrV.bmp"
   bdrBC: IncludeBinary "images\bdrBC.bmp"

   Nxt: IncludeBinary "images\next.bmp"

   a: IncludeBinary "images\Font\A.bmp"
   b: IncludeBinary "images\Font\B.bmp"
   c: IncludeBinary "images\Font\C.bmp"
   d: IncludeBinary "images\Font\D.bmp"
   e: IncludeBinary "images\Font\E.bmp"
   f: IncludeBinary "images\Font\F.bmp"
   g: IncludeBinary "images\Font\G.bmp"
   h: IncludeBinary "images\Font\H.bmp"
   i: IncludeBinary "images\Font\I.bmp"
   j: IncludeBinary "images\Font\J.bmp"
   k: IncludeBinary "images\Font\K.bmp"
   l: IncludeBinary "images\Font\L.bmp"
   m: IncludeBinary "images\Font\M.bmp"
   n: IncludeBinary "images\Font\N.bmp"
   o: IncludeBinary "images\Font\O.bmp"
   p: IncludeBinary "images\Font\P.bmp"
   q: IncludeBinary "images\Font\Q.bmp"
   r: IncludeBinary "images\Font\R.bmp"
   s: IncludeBinary "images\Font\S.bmp"
   t: IncludeBinary "images\Font\T.bmp"
   u: IncludeBinary "images\Font\U.bmp"
   v: IncludeBinary "images\Font\V.bmp"
   w: IncludeBinary "images\Font\W.bmp"
   x: IncludeBinary "images\Font\X.bmp"
   y: IncludeBinary "images\Font\Y.bmp"
   z: IncludeBinary "images\Font\Z.bmp"
   exclam: IncludeBinary "images\Font\!.bmp"

   0: IncludeBinary "images\Font\0.bmp"
   1: IncludeBinary "images\Font\1.bmp"
   2: IncludeBinary "images\Font\2.bmp"
   3: IncludeBinary "images\Font\3.bmp"
   4: IncludeBinary "images\Font\4.bmp"
   5: IncludeBinary "images\Font\5.bmp"
   6: IncludeBinary "images\Font\6.bmp"
   7: IncludeBinary "images\Font\7.bmp"
   8: IncludeBinary "images\Font\8.bmp"
   9: IncludeBinary "images\Font\9.bmp"


   tetris: IncludeBinary "sound\tetris.wav"
   line: IncludeBinary "sound\one_line.wav"
   blk: IncludeBinary "sound\block.wav"
   rotateS: IncludeBinary "sound\rotate.wav"
   lost: IncludeBinary "sound\lost.wav"
   pause: IncludeBinary "sound\pause.wav"
   AA: IncludeBinary "sound\A.ogg" :AAA:
   BB: IncludeBinary "sound\B.ogg" :BBB:
   CC: IncludeBinary "sound\C.ogg" :CCC:
 EndDataSection


 CatchSound(#Tetris, ?tetris)
 CatchSound(#Line, ?line)
 CatchSound(#Blk, ?Blk)
 CatchSound(#Rotate, ?rotateS)
 CatchSound(#lost, ?lost)
 CatchSound(#pause, ?pause)

 CatchSound(#musicA, ?AA, ?AAA - ?AA)
 CatchSound(#musicB, ?BB, ?BBB - ?BB)
 CatchSound(#musicC, ?CC, ?CCC - ?CC)

 BackGround = #musicA

screenX = Width*24+195
screenY = Height*24+48-3
If OpenWindow(0,200,50,screenX,screenY,"Blocks",#PB_Window_SystemMenu | #PB_Window_MinimizeGadget| #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)

   Enumeration
     #M_Baby
     #M_Normal
     #M_Blocks
     #M_SizeS
     #M_SizeM
     #M_SizeB
     #M_Reset
     #M_Level
     #M_StartHeight
     #M_ResetHiScore    
     #M_Controls
     #M_ViewHiScores
     #M_About
     #M_Exit
     #M_MusicA
     #M_MusicB
     #M_MusicC
     #M_MusicD
   EndEnumeration

    If CreateMenu(0, WindowID(0))
      MenuTitle("Mode")
        MenuItem(#M_Baby, "Baby")
        MenuItem(#M_Normal, "Normal")
        MenuItem(#M_Blocks, "Blocks")
      MenuTitle("Size")
        MenuItem(#M_SizeS, "Small  10x10")
        MenuItem(#M_SizeM, "Medium 10x20")
        MenuItem(#M_SizeB, "Big 15x25")
      MenuTitle("Options")
        MenuItem(#M_Reset, "Reset")
        MenuItem(#M_Level, "Choose Level")
        MenuItem(#M_StartHeight, "Starting Height")
        MenuItem(#M_ResetHiScore, "Reset HiScore")
        MenuItem(#M_Controls, "View Controls")
        MenuItem(#M_ViewHiScores, "View HiScores")
        MenuItem(#M_About, "About")
        MenuBar()
        MenuItem( #M_Exit, "Exit")
      MenuTitle("Sound")  
        MenuItem(#M_MusicA, "Music A")
        MenuItem(#M_MusicB, "Music B")
        MenuItem(#M_MusicC, "Music C")
        MenuItem(#M_MusicD, "None")
    EndIf

    ; open screen bigger than window that way the play area can be adjusted
  If OpenWindowedScreen(WindowID(0),0,0,screenX+240,screenY+240,0,0,0)  
    CatchSprite(#Block,?blk1,0)
    CatchSprite(#Block+1,?blk2,0)
    CatchSprite(#Block+2,?blk3,0)
    CatchSprite(#Block+3,?blk4,0)
    CatchSprite(#Block+4,?blk5,0)
    CatchSprite(#Block+5,?blk6,0)
    CatchSprite(#Block+6,?blk7,0)
    CatchSprite(#Block+7,?blk8,0)
    CatchSprite(#Block+8,?blk9,0)
    CatchSprite(#Block+9,?blk10,0)
    CatchSprite(#Block+10,?blk11,0)
    CatchSprite(#Block+11,?blk12,0)
    CatchSprite(#Block+12,?blk13,0)
    CatchSprite(#Block+13,?blk14,0)
    CatchSprite(#Block+14,?blk15,0)
    CatchSprite(#Block+15,?blk16,0)
    CatchSprite(#Block+16,?blk17,0)
    CatchSprite(#Block+17,?blk18,0)
    CatchSprite(#Block+18,?blk19,0)
    CatchSprite(#Block+19,?blk20,0)
    CatchSprite(#Block+20,?blk21,0)
    CatchSprite(#Block+21,?blk22,0)
    CatchSprite(#Block+22,?blk23,0)
    CatchSprite(#Block+23,?blk24,0)
    CatchSprite(#Block+24,?blk25,0)
    CatchSprite(#Block+25,?blk26,0)
    CatchSprite(#Block+26,?blk27,0)
    CatchSprite(#Block+27,?blk28,0)
    CatchSprite(#Block+28,?blk29,0)

    CatchSprite(#BorderL,?bdrL,0)
    CatchSprite(#BorderH,?bdrH,0)
    CatchSprite(#BorderTC,?bdrTC,0)
    CatchSprite(#BorderV,?bdrV,0)
    CatchSprite(#BorderBC,?bdrBC,0)

    CatchSprite(#Next,?Nxt,0)

    CatchSprite(#Letters+0,?a,0)
    CatchSprite(#Letters+1,?b,0)
    CatchSprite(#Letters+2,?c,0)
    CatchSprite(#Letters+3,?d,0)
    CatchSprite(#Letters+4,?e,0)
    CatchSprite(#Letters+5,?f,0)
    CatchSprite(#Letters+6,?g,0)
    CatchSprite(#Letters+7,?h,0)
    CatchSprite(#Letters+8,?i,0)
    CatchSprite(#Letters+9,?j,0)
    CatchSprite(#Letters+10,?k,0)
    CatchSprite(#Letters+11,?l,0)
    CatchSprite(#Letters+12,?m,0)
    CatchSprite(#Letters+13,?n,0)
    CatchSprite(#Letters+14,?o,0)
    CatchSprite(#Letters+15,?p,0)
    CatchSprite(#Letters+16,?q,0)
    CatchSprite(#Letters+17,?r,0)
    CatchSprite(#Letters+18,?s,0)
    CatchSprite(#Letters+19,?t,0)
    CatchSprite(#Letters+20,?u,0)
    CatchSprite(#Letters+21,?v,0)
    CatchSprite(#Letters+22,?w,0)
    CatchSprite(#Letters+23,?x,0)
    CatchSprite(#Letters+24,?y,0)
    CatchSprite(#Letters+25,?z,0)
    CatchSprite(#Letters+26,?exclam,0)

    CatchSprite(#Numbers+0,?0,0)
    CatchSprite(#Numbers+1,?1,0)
    CatchSprite(#Numbers+2,?2,0)
    CatchSprite(#Numbers+3,?3,0)
    CatchSprite(#Numbers+4,?4,0)
    CatchSprite(#Numbers+5,?5,0)
    CatchSprite(#Numbers+6,?6,0)
    CatchSprite(#Numbers+7,?7,0)
    CatchSprite(#Numbers+8,?8,0)
    CatchSprite(#Numbers+9,?9,0)

    ;Mode = 1 ; NORMAL
    Mode = 2; BLOCK
    Gosub LoadHiScore
    musicPlaying = 1
    startHeight = 0
    Gosub newGame

    Repeat

      Gosub ExamineKeyboard          
      If ElapsedMilliseconds() - time >=timeD
        timeD = LevelSpeed
        time = ElapsedMilliseconds()
        Gosub dropPiece
        Gosub Draw
      EndIf
      Gosub GetEvents
    Until playing <> 1 Or  event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)

    Goto GameEnd  

  Else
    MessageRequester("Tetris","A Screen unable to be initialized!",#MB_ICONERROR)  
  EndIf
Else
  MessageRequester("Tetris","A window was unable to be opened!",#MB_ICONERROR)
EndIf

; shouldn't get here.... but if it does
Goto GameEnd


;**********************************************************
;
;- Draw
;
;**********************************************************
Draw:
   ClearScreen(RGB(0,0,0))  
   Gosub DrawPieces
   Gosub DrawMenu
   If TetrisCtr > 0
     bitmapText(TetrisMsg$,(Width*24)/2-50,(Height-1)*24/2)      
     TetrisCtr-1
   EndIf
   FlipBuffers()
Return


;**********************************************************
;
;- ExamineKeyboard
;
;**********************************************************
ExamineKeyboard:
  ExamineKeyboard()
  If joystick = 1
    ExamineJoystick()
  EndIf

If ElapsedMilliseconds() - Movetime >= moveD
  Movetime = ElapsedMilliseconds()

  If KeyboardPushed(#PB_Key_Left) Or (joystick = 1 And JoystickAxisX() = -1 )
    If PieceY <= Height
      If PieceX+cp\b1x-1 >= 0 And PieceX+cp\b2x-1 >= 0 And PieceX+cp\b3x-1 >= 0 And PieceX+cp\b4x-1 >= 0 And PieceX+cp\b5x-1 >= 0 And PieceX+cp\b6x-1 >= 0 And PieceY+cp\b1y > 0 And PieceY+cp\b2y > 0 And PieceY+cp\b3y > 0 And PieceY+cp\b4y > 0 And PieceY+cp\b5y > 0 And PieceY+cp\b6y > 0
        If blocks(PieceX + cp\b1x-1, PieceY + cp\b1y) = 0 And blocks(PieceX + cp\b2x-1, PieceY + cp\b2y) = 0 And blocks(PieceX + cp\b3x-1, PieceY + cp\b3y) = 0 And blocks(PieceX + cp\b4x-1, PieceY + cp\b4y) = 0 And blocks(PieceX + cp\b5x-1, PieceY + cp\b5y) = 0 And blocks(PieceX + cp\b6x-1, PieceY + cp\b6y) = 0
          PieceX-1
        EndIf
      EndIf
    EndIf
    Gosub Draw
  EndIf


  If  KeyboardPushed(#PB_Key_Right) Or (joystick = 1 And JoystickAxisX() = 1 )
    If PieceY <= Height
      If PieceX+cp\b1x+1 < Width And PieceX+cp\b2x+1 < Width And PieceX+cp\b3x+1 < Width And PieceX+cp\b4x+1 < Width And PieceX+cp\b5x+1 < Width And PieceX+cp\b6x+1 < Width  And PieceY+cp\b1y > 0 And PieceY+cp\b2y > 0 And PieceY+cp\b3y > 0 And PieceY+cp\b4y > 0 And PieceY+cp\b5y > 0 And PieceY+cp\b6y > 0
        If blocks(PieceX + cp\b1x+1, PieceY + cp\b1y) = 0 And blocks(PieceX + cp\b2x+1, PieceY + cp\b2y) = 0 And blocks(PieceX + cp\b3x+1, PieceY + cp\b3y) = 0 And blocks(PieceX + cp\b4x+1, PieceY + cp\b4y) = 0 And blocks(PieceX + cp\b5x+1, PieceY + cp\b5y) = 0 And blocks(PieceX + cp\b6x+1, PieceY + cp\b6y) = 0         
          PieceX+1
        EndIf
      EndIf
    EndIf
    Gosub Draw
  EndIf
  If  KeyboardPushed(#PB_Key_Down) Or (joystick = 1 And JoystickAxisY() = 1 )
    Gosub Draw
    timeD = 0
  EndIf
EndIf

If ElapsedMilliseconds() - Fliptime >= flipD
  Fliptime = ElapsedMilliseconds()

  If KeyboardPushed(#PB_Key_X) Or (joystick = 1 And (JoystickButton(4) Or  JoystickButton(1) Or JoystickButton(8) ) )
    Gosub rotateL
    Gosub Draw
  EndIf
  If KeyboardPushed(#PB_Key_C) Or (joystick = 1 And (JoystickButton(3) Or  JoystickButton(2) Or JoystickButton(7) ) )
    Gosub rotateR
    Gosub Draw
  EndIf

EndIf

If KeyboardPushed(#PB_Key_R) Or (joystick = 1 And JoystickButton(9) )
  Gosub newgame
  Gosub Draw
EndIf


 Repeat
    If WindowID(0)
      Event = WindowEvent()
      If Event    
        Delay(10)
      EndIf   
    EndIf

    Gosub getEvents

    ExamineKeyboard()
    If KeyboardReleased(#PB_Key_RightAlt) Or KeyboardReleased(#PB_Key_Up);pause
      If Paused = 0
        ClearScreen(RGB(0,0,0))
        Gosub DrawMenu
        BitmapText("PAUSE",(Width*24)/2-50,(Height-1)*24/2)
        FlipBuffers()
        Paused = 1
        PlaySound(#pause)      
      Else
        Paused = 0
      EndIf
    EndIf      
 Until paused = 0

   Gosub getEvents
   If joystick = 1
    If JoystickButton(10)
        ClearScreen(RGB(0,0,0))
        Gosub DrawMenu
        BitmapText("PAUSE",(Width*24)/2-50,(Height-1)*24/2)
        FlipBuffers()
        Paused = 1
        PlaySound(#pause)
      FlipBuffers()
    EndIf
    Repeat
      If joystick = 1
        ExamineJoystick()
      EndIf
      If pausecounter < 0
        If joystick = 1
          If JoystickButton(1) Or JoystickButton(2) Or JoystickButton(3) Or JoystickButton(4) Or JoystickButton(5) Or JoystickButton(6) Or JoystickButton(7) Or JoystickButton(8)
            paused = 0
            pausecounter = 20
          EndIf
       EndIf
      Else
        pausecounter - 1
      EndIf
    Until paused = 0
  EndIf   

Return



;**********************************************************
;
;- GetEvents
;
;**********************************************************
GetEvents:
  EventID.l = WindowEvent()

    Select EventID
      Case #PB_Event_CloseWindow
        playing = 0
        Paused = 0
      Case #PB_Event_Menu      ; A Menu item has been selected         
        Select EventMenu()

          Case   #M_Baby
            If mode <> 0
              Mode = 0    
              Gosub newGame
              Paused = 0
            Else
              MessageRequester("Block","You are playing Baby Mode.",0)
            EndIf

          Case   #M_Normal
            If mode <> 1
              Mode = 1    
              Gosub newGame
              Paused = 0
            Else
              MessageRequester("Block","You are playing Normal Mode.",0)
            EndIf

          Case   #M_Blocks
            If mode <> 2  
              Mode = 2    
              Gosub newGame
              Paused = 0
            Else
              MessageRequester("Block","You are playing Block Mode.",0)
            EndIf

          Case   #M_SizeS
              Width = 10
              Height = 10    
              Gosub newGame
              Paused = 0
              ResizeWindow(0,WindowX(0),WindowY(0),Width*24+195,Height*2*24+48-3)   

          Case   #M_SizeM
              Width = 10
              Height = 20   
              Gosub newGame
              Paused = 0
              ResizeWindow(0,WindowX(0),WindowY(0),Width*24+195,Height*24+48-3)   

          Case   #M_SizeB
              Width = 15
              Height = 25   
              Gosub newGame
              Paused = 0
              ResizeWindow(0,WindowX(0),WindowY(0),Width*24+195,Height*24+48-3)     


          Case   #M_Reset
            Result = MessageRequester("Block","Are you sure that you want to Restart? "+var$,#PB_MessageRequester_YesNo)
            If Result = #PB_MessageRequester_Yes  
              t = Mode     
              Gosub newGame
              Mode = t
              Paused = 0
            EndIf

          Case   #M_Level
            var$ = InputRequester("Blocks","Choose your level 0-9","0")
            If Val(var$) >= 0 And Val(var$) <= 9            
              Result = MessageRequester("Block","Are you Sure? level "+var$,#PB_MessageRequester_YesNo)
              If Result = #PB_MessageRequester_Yes       
                Gosub newGame
                level = Val(var$)
                levelSpeed = levelSpeed - 25*level
                If levelSpeed < 25
                  levelSpeed = 25
                EndIf
                timeD = levelSpeed
                Paused = 0
              EndIf      
            Else
              MessageRequester("Blocks","Not a valid Selection",0)
            EndIf

          Case   #M_StartHeight
              s$ = InputRequester("Blocks","Input starting height 0 -"+Str(Height-5),"0")
              If Val(s$) >= 0 And Val(s$) <= Height-5
                Gosub newGame
                Paused = 0     
                For y = Height-1 To Height - Val(s$) Step -1
                  k = 0
                  For x = 0 To Width-1
                    If Random(5) >=4 And k < Width/2
                      If mode <> 2
                        blocks(x,y) = Random(7)+1
                      Else
                        blocks(x,y) = Random(26)+1
                      EndIf
                      k+1
                    EndIf
                  Next  
                Next   
              Else
                MessageRequester("Blocks","Invalid choice",0)
              EndIf

          Case #M_Controls
            MessageRequester("Blocks","Controls"+Chr(13)+"--------"+Chr(13)+"X - Rotate Counterclockwise"+Chr(13)+"C - Rotate Counterclockwise"+Chr(13)+"Right Alt/ Up Arrow - Pause"+Chr(13)+"Left & Right Arrows - Move Pieces"+Chr(13)+"Down Arrow - Speed Fall"+Chr(13)+"R - Reset"+Chr(13)+"Escape - Exit",0)

          Case #M_ViewHiScores
            MessageRequester("Blocks","Baby Mode"+Chr(13)+"--------"+Chr(13)+"Small     "+hiscoreN$+": "+Str(hiscore)+Chr(13)+"Medium  "+hiscoreMN$+": "+Str(hiscoreM)+Chr(13)+"Big        "+hiscoreBN$+": "+Str(hiscoreB)+Chr(13)+Chr(13)+"Normal Mode"+Chr(13)+"--------"+Chr(13)+"Small     "+hiscore2N$+": "+Str(hiscore2)+Chr(13)+"Medium  "+hiscore2MN$+": "+Str(hiscore2M)+Chr(13)+"Big        "+hiscore2BN$+": "+Str(hiscore2B)+Chr(13)+Chr(13)+"Blocks Mode"+Chr(13)+"--------"+Chr(13)+"Small     "+hiscore3N$+": "+Str(hiscore3)+Chr(13)+"Medium  "+hiscore3MN$+": "+Str(hiscore3M)+Chr(13)+"Big        "+hiscore3BN$+": "+Str(hiscore3B))

          Case #M_About
            MessageRequester("Blocks","Programmed by: Kenneth Cason"+Chr(13)+"Email: reddragon72455@yahoo.com",0)

          Case #M_ResetHiScore
            temp = score
            score = 0
            If Height = 10; Baby
              If mode = 0
                hiscore = 0
                hiscoreN$ = ""
              ElseIf mode = 1
                hiscore2 = 0
                hiscore2N$ = ""
              Else
                hiscore3 = 0
                hiscore3N$ = ""
              EndIf
            ElseIf Height = 20; Normal
              If mode = 0
                hiscoreM = 0
                hiscoreMN$ = ""
              ElseIf mode = 1
                hiscore2M = 0
                hiscore2MN$ = ""
              Else
                hiscore3M = 0
                hiscore3MN$ = ""
              EndIf
            ElseIf Height = 25; Blocks
              If mode = 0
                hiscoreB = 0
                hiscoreBN$ = ""
              ElseIf mode = 1
                hiscore2B = 0
                hiscore2BN$ = ""
              Else
                hiscore3B = 0
                hiscore3BN$ = ""
              EndIf
            EndIf                   
            Gosub saveHiScore
            score = temp

          Case #M_Exit
            playing = 0
            Paused = 0

          Case #M_MusicA
            If IsSound(BackGround)
              If musicPlaying = 1
                StopSound(BackGround)
              EndIf   
              StopSound(BackGround)
              BackGround = #musicA
              PlaySound(BackGround,1)
              musicPlaying = 1
            EndIf           

          Case #M_MusicB
            If IsSound(BackGround)
              If musicPlaying = 1
                StopSound(BackGround)
              EndIf
              StopSound(BackGround)
              BackGround = #musicB
              PlaySound(BackGround,1)
              musicPlaying = 1
            EndIf

          Case #M_MusicC
            If IsSound(BackGround)
              If musicPlaying = 1
                StopSound(BackGround)
              EndIf
              BackGround = #musicC
              PlaySound(BackGround,1)
              musicPlaying = 1
            EndIf

          Case #M_MusicD
            If IsSound(BackGround)
              musicPlaying = 0
              StopSound(BackGround)
            EndIf      

        EndSelect
    EndSelect
Return


;**********************************************************
;
;- DrawMenu
;
;**********************************************************
DrawMenu:
  ;********* Draw Border
  DisplaySprite(#BorderL, 0, 0)
  For k = 1 To Width
    DisplaySprite(#BorderH, k*24, 0)
  Next
  DisplaySprite(#BorderTC, Width*24, 0)
  For k = 1 To Height-1
    DisplaySprite(#BorderV, Width*24, k*24)
  Next
  DisplaySprite(#BorderBC, Width*24, Height*24)
  DisplaySprite(#BorderL, 0, Height*24)
  For k = 1 To Width-1
    DisplaySprite(#BorderH, k*24, Height*24)
  Next
  ;*********
  OffsetX = Width*24+24
  bitmapText("BLOCKS",OffsetX,10)
  bitmapText("NEXT",OffsetX,35)
    DisplaySprite(#Next,OffsetX,62)
    DisplaySprite(np\image,OffsetX+60+np\b1x*24+(4*np\b1x),94+np\b1y*24+(4*np\b1y))
    DisplaySprite(np\image,OffsetX+60+np\b2x*24+(4*np\b2x),94+np\b2y*24+(4*np\b2y))
    DisplaySprite(np\image,OffsetX+60+np\b3x*24+(4*np\b3x),94+np\b3y*24+(4*np\b3y))
    DisplaySprite(np\image,OffsetX+60+np\b4x*24+(4*np\b4x),94+np\b4y*24+(4*np\b4y))
    DisplaySprite(np\image,OffsetX+60+np\b5x*24+(4*np\b5x),94+np\b5y*24+(4*np\b5y))
    DisplaySprite(np\image,OffsetX+60+np\b6x*24+(4*np\b6x),94+np\b6y*24+(4*np\b6y))
  bitmapText("MODE",OffsetX,155)
  If mode = 0
    bitmapText(" BABY",OffsetX,180)
  ElseIf mode = 1
    bitmapText(" NORMAL",OffsetX,180)
  ElseIf mode = 2
    bitmapText(" BLOCKS",OffsetX,180)
  EndIf
  bitmapText("LEVEL",OffsetX,205)    
  bitmapText(" "+Str(level),OffsetX,230)
  bitmapText("LINES",OffsetX,255)
  bitmapText(" "+Str(lines),OffsetX,280)
  bitmapText("SCORE",OffsetX,305)
  bitmapText(" "+Str(score),OffsetX,330)
  bitmapText("HISCORE",OffsetX,355)
  bitmapText(" "+Str(hiscoreThisMode),OffsetX,380)
  bitmapText(" "+hiscoreThisModeN$,OffsetX,405)

Return


;**********************************************************
;
;- rotateL
;
;**********************************************************
rotateL:

  If CurrentPiece > 0 ; not a square
    If PieceX + cp\b1y >= 0 And PieceX + cp\b2y >= 0 And PieceX + cp\b3y >= 0 And PieceX + cp\b4y >= 0  And PieceX + cp\b5y >= 0  And PieceX + cp\b6y >= 0 And PieceX + cp\b1y < Width And PieceX + cp\b2y < Width And PieceX + cp\b3y < Width And PieceX + cp\b4y < Width  And PieceX + cp\b5y < Width And PieceX + cp\b6y < Width  And PieceY - cp\b1x < Height And PieceY - cp\b2x < Height And PieceY - cp\b3x < Height And PieceY - cp\b4x < Height  And PieceY - cp\b5x < Height  And PieceY - cp\b6x < Height And PieceY - cp\b1x >= 0 And PieceY - cp\b2x >= 0  And PieceY - cp\b3x >= 0  And PieceY - cp\b4x >= 0 And PieceY - cp\b5x >= 0 And PieceY - cp\b6x >= 0 ; flip is in bounds
      If blocks(PieceX + cp\b1y, PieceY - cp\b1x) = 0 And blocks(PieceX + cp\b2y, PieceY - cp\b2x) = 0 And blocks(PieceX + cp\b3y, PieceY - cp\b3x) = 0 And blocks(PieceX + cp\b4y, PieceY - cp\b4x) = 0 And blocks(PieceX + cp\b5y, PieceY - cp\b5x) = 0 And blocks(PieceX + cp\b6y, PieceY - cp\b6x) = 0
        t = cp\b1x
        cp\b1x = cp\b1y
        cp\b1y=-t
        t = cp\b2x
        cp\b2x = cp\b2y
        cp\b2y=-t
        t = cp\b3x
        cp\b3x = cp\b3y
        cp\b3y=-t
        t = cp\b4x
        cp\b4x = cp\b4y
        cp\b4y=-t   
        t = cp\b5x
        cp\b5x = cp\b5y
        cp\b5y=-t
        t = cp\b6x
        cp\b6x = cp\b6y
        cp\b6y=-t
      EndIf
    EndIf
  EndIf
Return

;**********************************************************
;
;- rotateR
;
;**********************************************************
rotateR:

  If CurrentPiece > 0 ; not a square
    If PieceX - cp\b1y >= 0 And PieceX - cp\b2y >= 0 And PieceX - cp\b3y >= 0 And PieceX - cp\b4y >= 0  And PieceX - cp\b5y >= 0  And PieceX - cp\b6y >= 0 And PieceX - cp\b1y < Width And PieceX - cp\b2y < Width And PieceX - cp\b3y < Width And PieceX - cp\b4y < Width  And PieceX - cp\b5y < Width And PieceX - cp\b6y < Width  And PieceY + cp\b1x < Height And PieceY + cp\b2x < Height And PieceY + cp\b3x < Height And PieceY + cp\b4x < Height  And PieceY + cp\b5x < Height  And PieceY + cp\b6x < Height And PieceY + cp\b1x >= 0 And PieceY + cp\b2x >= 0  And PieceY + cp\b3x >= 0  And PieceY + cp\b4x >= 0 And PieceY + cp\b5x >= 0 And PieceY + cp\b6x >= 0 ; flip is in bounds
      If blocks(PieceX - cp\b1y, PieceY + cp\b1x) = 0 And blocks(PieceX - cp\b2y, PieceY + cp\b2x) = 0 And blocks(PieceX - cp\b3y, PieceY + cp\b3x) = 0 And blocks(PieceX - cp\b4y, PieceY + cp\b4x) = 0 And blocks(PieceX - cp\b5y, PieceY + cp\b5x) = 0 And blocks(PieceX - cp\b6y, PieceY + cp\b6x) = 0
        t = cp\b1x
        cp\b1x = -cp\b1y
        cp\b1y=t
        t = cp\b2x
        cp\b2x = -cp\b2y
        cp\b2y=t
        t = cp\b3x
        cp\b3x = -cp\b3y
        cp\b3y=t
        t = cp\b4x
        cp\b4x = -cp\b4y
        cp\b4y=t   
        t = cp\b5x
        cp\b5x = -cp\b5y
        cp\b5y=t
        t = cp\b6x
        cp\b6x = -cp\b6y
        cp\b6y=t
      EndIf
    EndIf
  EndIf
Return

;**********************************************************
;
;- dropPiece
;
;**********************************************************
dropPiece:
  doneFalling = 0
  If (PieceY + cp\b1y+1) < Height And (PieceY + cp\b2y+1) < Height And (PieceY + cp\b3y+1) < Height And (PieceY + cp\b4y+1) < Height And (PieceY + cp\b5y+1) < Height  And (PieceY + cp\b6y+1) < Height
    If blocks(PieceX + cp\b1x, PieceY + cp\b1y+1) = 0 And blocks(PieceX + cp\b2x, PieceY + cp\b2y+1) = 0  And blocks(PieceX + cp\b3x, PieceY + cp\b3y+1) = 0 And blocks(PieceX + cp\b4x, PieceY + cp\b4y+1) = 0  And blocks(PieceX + cp\b5x, PieceY + cp\b5y+1) = 0 And blocks(PieceX + cp\b6x, PieceY + cp\b6y+1) = 0
      PieceY+1
    Else
      doneFalling = 1
    EndIf  
  Else
    doneFalling = 1
  EndIf

  If doneFalling = 1
    PlaySound(#Blk)
    If PieceY + cp\b1y >= 0
      blocks(PieceX + cp\b1x, PieceY + cp\b1y) = cp\image
    EndIf
    If PieceY + cp\b2y >= 0
      blocks(PieceX + cp\b2x, PieceY + cp\b2y) = cp\image
    EndIf
    If PieceY + cp\b3y >= 0  
      blocks(PieceX + cp\b3x, PieceY + cp\b3y) = cp\image
    EndIf
    If PieceY + cp\b4y >= 0  
      blocks(PieceX + cp\b4x, PieceY + cp\b4y) = cp\image
    EndIf  
    If PieceY + cp\b5y >= 0  
      blocks(PieceX + cp\b5x, PieceY + cp\b5y) = cp\image
    EndIf
    If PieceY + cp\b6y >= 0  
      blocks(PieceX + cp\b6x, PieceY + cp\b6y) = cp\image
    EndIf  
    score+30+10*level
    Gosub setCurrentPiece
    Gosub getNextPiece
    PieceX = Width/2
    PieceY = 0
  EndIf
  Gosub isLine
  Gosub isDead
Return

;**********************************************************
;
;- DrawPieces
;
;**********************************************************
DrawPieces:
  For x = 0 To Width-1
    For y = 0 To Height-1
      If Blocks(x,y) > 0
        DisplaySprite(blocks(x,y), x*24, y*24)
      EndIf
    Next
  Next
  If PieceY + cp\b1y >= 0
    DisplaySprite(cp\image, (PieceX + cp\b1x)*24, (PieceY + cp\b1y)*24)
  EndIf
  If PieceY + cp\b1y >= 0
    DisplaySprite(cp\image, (PieceX + cp\b2x)*24, (PieceY + cp\b2y)*24)
  EndIf
  If PieceY + cp\b1y >= 0
    DisplaySprite(cp\image, (PieceX + cp\b3x)*24, (PieceY + cp\b3y)*24)
  EndIf
  If PieceY + cp\b1y >= 0  
    DisplaySprite(cp\image, (PieceX + cp\b4x)*24, (PieceY + cp\b4y)*24)
  EndIf
  If PieceY + cp\b1y >= 0
    DisplaySprite(cp\image, (PieceX + cp\b5x)*24, (PieceY + cp\b5y)*24)
  EndIf
  If PieceY + cp\b1y >= 0
    DisplaySprite(cp\image, (PieceX + cp\b6x)*24, (PieceY + cp\b6y)*24)
  EndIf
Return

;**********************************************************
;
;- isLine
;
;**********************************************************
isLine:
  lineCtr = 0
  For y = 0 To Height-1
    blockCtr = 0
    For x = 0 To Width-1
      If Blocks(x,y) > 0
        blockCtr + 1
        If blockCtr = Width
          lines + 1
          lineCtr + 1
          For k = 0 To Width-1 ; delete Row
            blocks(k,y) = 0
          Next
          For x = 0 To Width-1 ; drop Lines
            For j = y To 1 Step -1
              blocks(x,j)= blocks(x,j-1)
            Next
          Next
        EndIf
      EndIf
    Next
  Next
  TetrisLine = 0
  If lineCtr > 0 And lineCtr < 4
    If lineCtr = 3 And mode = 0
      score+2500
      tetrisCtr = 30
      TetrisLine = 3
      TetrisMsg$ = "WHAMMO!"
      PlaySound(#Tetris)
    Else
      score+150+200*lineCtr
      PlaySound(#Line)
    EndIf
  ElseIf lineCtr = 4 ; BLOCK!!!
    tetrisCtr = 30
    score+5000
    TetrisLine = 4
    If mode = 0
      TetrisMsg$ = "GOOD!"
    Else
      TetrisMsg$ = "AWESOME!"
    EndIf
    PlaySound(#Tetris)
  ElseIf lineCtr = 5 ; BLOCK!!!
    tetrisCtr = 30
    score+10000
    TetrisLine = 5
    TetrisMsg$ = "ALMOST!"
    PlaySound(#Tetris)
  ElseIf lineCtr >= 6 ; BLOCK!!!
    tetrisCtr = 30
    score+15000
    TetrisLine = 6
    TetrisMsg$ = "BLOCK!!"
    PlaySound(#Tetris)  
  EndIf

  If lines >= level*10+10
    level + 1
    If levelSpeed >=75
      levelSpeed-25
    ElseIf levelSpeed > 30
      levelSpeed-2
    EndIf
    If levelSpeed < 30
      levelSpeed = 30
    EndIf
  EndIf  
Return


;**********************************************************
;
;- isDead
;
;**********************************************************
isDead:
For x = 0 To Width-1
  If blocks(x,0) > 0
    Gosub saveHiScore
    If IsSound(BackGround)
      StopSound(BackGround)
    EndIf
    PlaySound(#lost)
    Gosub newGame
    Delay(2000)
  EndIf
Next

Return

;**********************************************************
;
;- newGame
;
;**********************************************************
newGame:
  ; get hiscore
  If Height = 10 ; Baby Size
    If mode = 0
      hiscoreThisMode = hiscore
      hiscoreThisModeN$ = hiscoreN$
    ElseIf mode = 1
      hiscoreThisMode = hiscore2
      hiscoreThisModeN$ = hiscore2N$
    ElseIf mode = 2
      hiscoreThisMode = hiscore3
      hiscoreThisModeN$ = hiscore3N$
    EndIf
  ElseIf Height = 20 ; Normal Size
    If mode = 0
      hiscoreThisMode = hiscoreM

      hiscoreThisModeN$ = hiscoreMN$
    ElseIf mode = 1
      hiscoreThisMode = hiscore2M
      hiscoreThisModeN$ = hiscore2MN$
    ElseIf mode = 2
      hiscoreThisMode = hiscore3M
      hiscoreThisModeN$ = hiscore3MN$
    EndIf
  ElseIf Height = 25 ; big Size
    If mode = 0
      hiscoreThisMode = hiscoreB
      hiscoreThisModeN$ = hiscoreBN$
    ElseIf mode = 1
      hiscoreThisMode = hiscore2B
      hiscoreThisModeN$ = hiscore2BN$
    ElseIf mode = 2
      hiscoreThisMode = hiscore3B
      hiscoreThisModeN$ = hiscore3BN$
    EndIf   
  EndIf

  Dim blocks.b(Width,Height)
  For x = 0 To Width-1
    For y = 0 To Height-1
      Blocks(x,y) = 0
    Next
  Next
  Gosub getNextPiece
  Gosub setCurrentPiece
  Gosub getNextPiece
  level = 0
  lines = 0
  Gosub loadHiScore
  score = 0
  PieceX = Width/2
  PieceY = 0
  time = ElapsedMilliseconds()
  Movetime = ElapsedMilliseconds()
  Fliptime = ElapsedMilliseconds()
  LevelSpeed = 350
  TimeD = LevelSpeed
  MoveD = 70
  flipD = 150
  playing = 1
  If musicPlaying = 1
    If IsSound(BackGround)
      StopSound(BackGround)
      PlaySound(BackGround,1)
    EndIf
  EndIf

Return




Return

;**********************************************************
;
;- loadHiScore
;
;**********************************************************
LoadHiScore:
  If ReadFile(0, "blocks.hs")
    hiscore   = ReadLong(0)+1234567890
    hiscore2  = ReadLong(0)+1234567890
    hiscore3  = ReadLong(0)+1234567890
    hiscoreM  = ReadLong(0)+1234567890
    hiscore2M = ReadLong(0)+1234567890
    hiscore3M = ReadLong(0)+1234567890
    hiscoreB  = ReadLong(0)+1234567890
    hiscore2B = ReadLong(0)+1234567890
    hiscore3B = ReadLong(0)+1234567890

    hiscoreN$   = ReadString(0)
    hiscore2N$  = ReadString(0)
    hiscore3N$  = ReadString(0)
    hiscoreMN$   = ReadString(0)
    hiscore2MN$ = ReadString(0)
    hiscore3MN$ = ReadString(0)
    hiscoreBN$   = ReadString(0)
    hiscore2BN$ = ReadString(0)
    hiscore3BN$ = ReadString(0)
    CloseFile(0)
  EndIf
Return

;**********************************************************
;
;- saveHiScore
;
;**********************************************************
SaveHiScore:
If Height = 10 ; baby
  If score > hiscore And mode = 0
    hiscore = score
    hiscoreN$ = getName()
  ElseIf score > hiscore2 And mode = 1
    hiscore2 = score
    hiscore2N$ = getName()
  ElseIf score > hiscore3 And mode = 2
    hiscore3 = score
    hiscore3N$ = getName()
  EndIf
ElseIf Height = 20
  If score > hiscoreM And mode = 0
    hiscoreM = score
    hiscoreMN$ = getName()
  ElseIf score > hiscore2M And mode = 1
    hiscore2M = score
    hiscore2MN$ = getName()
  ElseIf score > hiscore3M And mode = 2
    hiscore3M = score
    hiscore3MN$ = getName()
  EndIf
ElseIf Height = 25
  If score > hiscoreB And mode = 0
    hiscoreB = score
    hiscoreBN$ = getName()
  ElseIf score > hiscore2B And mode = 1
    hiscore2B = score
    hiscore2BN$ = getName()
  ElseIf score > hiscore3B And mode = 2
    hiscore3B = score
    hiscore3BN$ = getName()
  EndIf
EndIf

If OpenFile(0, "blocks.hs")
  WriteLong(0,hiscore-1234567890)  ; baby size
  WriteLong(0,hiscore2-1234567890)
  WriteLong(0,hiscore3-1234567890)

  WriteLong(0,hiscoreM-1234567890)   ; Normal size
  WriteLong(0,hiscore2M-1234567890)
  WriteLong(0,hiscore3M-1234567890)

  WriteLong(0,hiscoreB-1234567890)   ; Big Size
  WriteLong(0,hiscore2B-1234567890)
  WriteLong(0,hiscore3B-1234567890)

  WriteStringN(0,hiscoreN$)  ; names
  WriteStringN(0,hiscore2N$)
  WriteStringN(0,hiscore3N$)

  WriteStringN(0,hiscoreMN$)
  WriteStringN(0,hiscore2MN$)
  WriteStringN(0,hiscore3MN$)

  WriteStringN(0,hiscoreBN$)
  WriteStringN(0,hiscore2BN$)
  WriteStringN(0,hiscore3BN$)
  CloseFile(0)
EndIf
Return


;**********************************************************
;
;- GetNextPiece
;
;**********************************************************
GetNextPiece:

If mode > 0 ; not baby
   If mode = 1 ; NORMAL MODE
     g = Random(6)
   ElseIf mode = 2 ; BLOCKS MODE
     g = Random(28)   
   EndIf

   If g = 0
     np.piece\image=#Block
     np.piece\b1x=0   ; a square
     np.piece\b1y=0   
     np.piece\b2x=1   
     np.piece\b2y=0
     np.piece\b3x=0
     np.piece\b3y=1
     np.piece\b4x=1
     np.piece\b4y=1  ; since these extra blocks arent used in this mode
     np.piece\b5x=0  ; just make the piece over another piece to make only 4 appear.
     np.piece\b5y=0
     np.piece\b6x=1
     np.piece\b6y=0    
   ElseIf g = 1
     np.piece\image=#Block+1
     np.piece\b1x=-1   ; L
     np.piece\b1y=1   
     np.piece\b2x=-1   
     np.piece\b2y=0
     np.piece\b3x=0
     np.piece\b3y=0
     np.piece\b4x=1
     np.piece\b4y=0
     np.piece\b5x=-1
     np.piece\b5y=1
     np.piece\b6x=-1
     np.piece\b6y=0
   ElseIf g = 2
     np.piece\image=#Block+2
     np.piece\b1x=-1   ; L-backwards
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=0   
     np.piece\b3x=1
     np.piece\b3y=0
     np.piece\b4x=1
     np.piece\b4y=1
     np.piece\b5x=0
     np.piece\b5y=0
     np.piece\b6x=0
     np.piece\b6y=0
   ElseIf g = 3
     np.piece\image=#Block+3
     np.piece\b1x=-1   ; Line
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=0  
     np.piece\b3x=1
     np.piece\b3y=0
     np.piece\b4x=2
     np.piece\b4y=0
     np.piece\b5x=-1
     np.piece\b5y=0
     np.piece\b6x=0
     np.piece\b6y=0
   ElseIf g = 4
     np.piece\image=#Block+4
     np.piece\b1x=1   ; N
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=0   
     np.piece\b3x=0
     np.piece\b3y=1
     np.piece\b4x=-1
     np.piece\b4y=1
     np.piece\b5x=1
     np.piece\b5y=0
     np.piece\b6x=0
     np.piece\b6y=0
   ElseIf g = 5
     np.piece\image=#Block+5
     np.piece\b1x=1   ; N-backwards
     np.piece\b1y=1   
     np.piece\b2x=0   
     np.piece\b2y=1   
     np.piece\b3x=0
     np.piece\b3y=0
     np.piece\b4x=-1
     np.piece\b4y=0  
     np.piece\b5x=1
     np.piece\b5y=1
     np.piece\b6x=0
     np.piece\b6y=1   
   ElseIf g = 6
     np.piece\image=#Block+6
     np.piece\b1x=0   ; T
     np.piece\b1y=1   
     np.piece\b2x=-1   
     np.piece\b2y=0   
     np.piece\b3x=0   
     np.piece\b3y=0
     np.piece\b4x=1
     np.piece\b4y=0
     np.piece\b5x=0
     np.piece\b5y=1
     np.piece\b6x=-1
     np.piece\b6y=0
   ElseIf g = 7            ;********************* BLOCK ********************
     np.piece\image=#Block+7
     np.piece\b1x=0   ; dot
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=0
     np.piece\b3x=0
     np.piece\b3y=0
     np.piece\b4x=0
     np.piece\b4y=0
     np.piece\b5x=0  
     np.piece\b5y=0
     np.piece\b6x=0
     np.piece\b6y=0    
   ElseIf g = 8
     np.piece\image=#Block+8
     np.piece\b1x=-1   ; Screw
     np.piece\b1y=-1   
     np.piece\b2x=-1   
     np.piece\b2y=0
     np.piece\b3x=0
     np.piece\b3y=0
     np.piece\b4x=1
     np.piece\b4y=0
     np.piece\b5x=1
     np.piece\b5y=1
     np.piece\b6x=-1
     np.piece\b6y=-1
   ElseIf g = 9
     np.piece\image=#Block+9
     np.piece\b1x=1    ; Screw backwards
     np.piece\b1y=-1   
     np.piece\b2x=1   
     np.piece\b2y=0
     np.piece\b3x=0
     np.piece\b3y=0
     np.piece\b4x=-1
     np.piece\b4y=0
     np.piece\b5x=-1
     np.piece\b5y=1
     np.piece\b6x=1
     np.piece\b6y=-1
   ElseIf g = 10
     np.piece\image=#Block+10
     np.piece\b1x=-1   ; long cross
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=0  
     np.piece\b3x=1
     np.piece\b3y=0
     np.piece\b4x=0
     np.piece\b4y=-1
     np.piece\b5x=0
     np.piece\b5y=1
     np.piece\b6x=0
     np.piece\b6y=0
   ElseIf g = 11
     np.piece\image=#Block+11
     np.piece\b1x=-1   ; cross
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=0  
     np.piece\b3x=1
     np.piece\b3y=0
     np.piece\b4x=0
     np.piece\b4y=-1
     np.piece\b5x=0
     np.piece\b5y=1
     np.piece\b6x=2
     np.piece\b6y=0
   ElseIf g = 12
     np.piece\image=#Block+12
     np.piece\b1x=-1   ; layers
     np.piece\b1y=-1   
     np.piece\b2x=0   
     np.piece\b2y=-1   
     np.piece\b3x=1
     np.piece\b3y=-1
     np.piece\b4x=-1
     np.piece\b4y=1  
     np.piece\b5x=0
     np.piece\b5y=1
     np.piece\b6x=1
     np.piece\b6y=1   
   ElseIf g = 13
     np.piece\image=#Block+13
     np.piece\b1x=-1   ; Y
     np.piece\b1y=-1   
     np.piece\b2x=-1   
     np.piece\b2y=0  
     np.piece\b3x=0   
     np.piece\b3y=0
     np.piece\b4x=0
     np.piece\b4y=1
     np.piece\b5x=1
     np.piece\b5y=-1
     np.piece\b6x=1
     np.piece\b6y=0
   ElseIf g = 14
     np.piece\image=#Block+14
     np.piece\b1x=-1   ; U
     np.piece\b1y=0   
     np.piece\b2x=-1   
     np.piece\b2y=1   
     np.piece\b3x=0   
     np.piece\b3y=1
     np.piece\b4x=0
     np.piece\b4y=1
     np.piece\b5x=1
     np.piece\b5y=0
     np.piece\b6x=1
     np.piece\b6y=1
   ElseIf g = 15
     np.piece\image=#Block+15
     np.piece\b1x=-2   ; 5 line
     np.piece\b1y=0   
     np.piece\b2x=-1   
     np.piece\b2y=0   
     np.piece\b3x=0   
     np.piece\b3y=0
     np.piece\b4x=1
     np.piece\b4y=0
     np.piece\b5x=2
     np.piece\b5y=0
     np.piece\b6x=2
     np.piece\b6y=0
   ElseIf g = 16
     np.piece\image=#Block+16
     np.piece\b1x=-2   ; 6 line
     np.piece\b1y=0   
     np.piece\b2x=-1   
     np.piece\b2y=0   
     np.piece\b3x=0   
     np.piece\b3y=0
     np.piece\b4x=1
     np.piece\b4y=0
     np.piece\b5x=2
     np.piece\b5y=0
     np.piece\b6x=3
     np.piece\b6y=0
   ElseIf g = 17
     np.piece\image=#Block+17
     np.piece\b1x=-1   ; 3x2 block
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=0   
     np.piece\b3x=1   
     np.piece\b3y=0
     np.piece\b4x=-1
     np.piece\b4y=1
     np.piece\b5x=0
     np.piece\b5y=1
     np.piece\b6x=1
     np.piece\b6y=1
   ElseIf g = 18
     np.piece\image=#Block+18
     np.piece\b1x=-1   ; zig-zag
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=1   
     np.piece\b3x=1   
     np.piece\b3y=0
     np.piece\b4x=-1
     np.piece\b4y=0
     np.piece\b5x=-1
     np.piece\b5y=0
     np.piece\b6x=-1
     np.piece\b6y=0
   ElseIf g = 19
     np.piece\image=#Block+19
     np.piece\b1x=-1   ; 2x2 block + notch top
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=0   
     np.piece\b3x=0   
     np.piece\b3y=0
     np.piece\b4x=-1
     np.piece\b4y=1
     np.piece\b5x=0
     np.piece\b5y=1
     np.piece\b6x=1
     np.piece\b6y=0
   ElseIf g = 20
     np.piece\image=#Block+20
     np.piece\b1x=-1   ; 2x2 block + notch bottom
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=0   
     np.piece\b3x=0   
     np.piece\b3y=0
     np.piece\b4x=-1
     np.piece\b4y=1
     np.piece\b5x=0
     np.piece\b5y=1
     np.piece\b6x=1
     np.piece\b6y=1  
   ElseIf g = 21   ; ************* BLOCKS
     np.piece\image=#Block+21
     np.piece\b1x=0   ; Small L
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=1   
     np.piece\b3x=0   
     np.piece\b3y=0
     np.piece\b4x=0
     np.piece\b4y=0
     np.piece\b5x=0
     np.piece\b5y=0
     np.piece\b6x=0
     np.piece\b6y=0  
   ElseIf g = 22
     np.piece\image=#Block+22
     np.piece\b1x=-1   ; Big T
     np.piece\b1y=-1   
     np.piece\b2x=0   
     np.piece\b2y=-1   
     np.piece\b3x=1   
     np.piece\b3y=-1
     np.piece\b4x=0
     np.piece\b4y=0
     np.piece\b5x=0
     np.piece\b5y=1
     np.piece\b6x=0
     np.piece\b6y=0   
   ElseIf g = 23
     np.piece\image=#Block+23
     np.piece\b1x=-1   ; Short parallel, by connie :)
     np.piece\b1y=0   
     np.piece\b2x=-1  
     np.piece\b2y=1  
     np.piece\b3x=1  
     np.piece\b3y=0
     np.piece\b4x=1
     np.piece\b4y=1
     np.piece\b5x=1
     np.piece\b5y=1
     np.piece\b6x=1
     np.piece\b6y=1  
   ElseIf g = 24
     np.piece\image=#Block+24
     np.piece\b1x=-1   ; Big L backwards
     np.piece\b1y=-1   
     np.piece\b2x=0   
     np.piece\b2y=-1   
     np.piece\b3x=1   
     np.piece\b3y=-1
     np.piece\b4x=1
     np.piece\b4y=0
     np.piece\b5x=1
     np.piece\b5y=1
     np.piece\b6x=-1
     np.piece\b6y=-1
   ElseIf g = 25
     np.piece\image=#Block+25
     np.piece\b1x=-2   ; TO
     np.piece\b1y=0   
     np.piece\b2x=-1   
     np.piece\b2y=0   
     np.piece\b3x=0   
     np.piece\b3y=0
     np.piece\b4x=1
     np.piece\b4y=0
     np.piece\b5x=2
     np.piece\b5y=0
     np.piece\b6x=0
     np.piece\b6y=1
   ElseIf g = 26
     np.piece\image=#Block+26
     np.piece\b1x=-1   ; Small L
     np.piece\b1y=0  
     np.piece\b2x=0   
     np.piece\b2y=0   
     np.piece\b3x=0   
     np.piece\b3y=1
     np.piece\b4x=0
     np.piece\b4y=0
     np.piece\b5x=0
     np.piece\b5y=0
     np.piece\b6x=0
     np.piece\b6y=0  
   ElseIf g = 27
     np.piece\image=#Block+27
     np.piece\b1x=-1   ; 3 by 1 line
     np.piece\b1y=0  
     np.piece\b2x=0   
     np.piece\b2y=0   
     np.piece\b3x=1   
     np.piece\b3y=0
     np.piece\b4x=0
     np.piece\b4y=0
     np.piece\b5x=0
     np.piece\b5y=0
     np.piece\b6x=0
     np.piece\b6y=0     
   ElseIf g = 28
     np.piece\image=#Block+28
     np.piece\b1x=-1+Random(2)   ; crazy
     np.piece\b1y=-1+Random(2)   
     np.piece\b2x=-1+Random(2)   
     np.piece\b2y=-1+Random(2)   
     np.piece\b3x=-1+Random(2)   
     np.piece\b3y=-1+Random(2)
     np.piece\b4x=-1+Random(2)
     np.piece\b4y=-1+Random(2)
     np.piece\b5x=-1+Random(2)
     np.piece\b5y=-1+Random(2)
     np.piece\b6x=-1+Random(2)
     np.piece\b6y=-1+Random(2)    
   EndIf
 Else  ; BABY BLOCKS
   g = Random(8)   

   If g = 0
     np.piece\image=#Block
     np.piece\b1x=0   ; a dot
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=0
     np.piece\b3x=0
     np.piece\b3y=0
   ElseIf g = 1
     np.piece\image=#Block+1
     np.piece\b1x=0   ; line
     np.piece\b1y=-1   
     np.piece\b2x=0   
     np.piece\b2y=0
     np.piece\b3x=0
     np.piece\b3y=1
   ElseIf g = 2
     np.piece\image=#Block+2
     np.piece\b1x=-1   ; spaced
     np.piece\b1y=0   
     np.piece\b2x=1   
     np.piece\b2y=0   
     np.piece\b3x=1
     np.piece\b3y=0
   ElseIf g = 3
     np.piece\image=#Block+3
     np.piece\b1x=-1   ; L
     np.piece\b1y=0   
     np.piece\b2x=0   
     np.piece\b2y=0  
     np.piece\b3x=0
     np.piece\b3y=1
   ElseIf g = 4
     np.piece\image=#Block+4
     np.piece\b1x=0   ; two line
     np.piece\b1y=0   
     np.piece\b2x=1   
     np.piece\b2y=0   
     np.piece\b3x=0
     np.piece\b3y=0
   ElseIf g = 5
     np.piece\image=#Block+5
     np.piece\b1x=-1   ; Zig
     np.piece\b1y=-1   
     np.piece\b2x=0   
     np.piece\b2y=0   
     np.piece\b3x=1
     np.piece\b3y=1
   ElseIf g = 6
     np.piece\image=#Block+6
     np.piece\b1x=-1   ; bent
     np.piece\b1y=-1   
     np.piece\b2x=0   
     np.piece\b2y=0   
     np.piece\b3x=-1   
     np.piece\b3y=1
   ElseIf g = 7           
     np.piece\image=#Block+7
     np.piece\b1x= -1+Random(1)
     np.piece\b1y= -1+Random(1)
     np.piece\b2x= -1+Random(1)
     np.piece\b2y= -1+Random(1)
     np.piece\b3x= -1+Random(1)
     np.piece\b3y= -1+Random(1)
   EndIf
     np.piece\b4x = np.piece\b1x
     np.piece\b4y = np.piece\b1y; since these extra blocks arent used in this mode
     np.piece\b5x = np.piece\b1x; just make the piece over another piece to make only 3
     np.piece\b5y = np.piece\b1y
     np.piece\b6x = np.piece\b1x
     np.piece\b6y = np.piece\b1y   

 EndIf

 Return



 Return
;**********************************************************
;
;- SetCurrentPiece
;
;**********************************************************
 SetCurrentPiece:
   CurrentPiece = g
   cp.piece\image = np.piece\image
   cp.piece\b1x = np.piece\b1x
   cp.piece\b1y = np.piece\b1y
   cp.piece\b2x = np.piece\b2x
   cp.piece\b2y = np.piece\b2y  
   cp.piece\b3x = np.piece\b3x
   cp.piece\b3y = np.piece\b3y
   cp.piece\b4x = np.piece\b4x
   cp.piece\b4y = np.piece\b4y
   cp.piece\b5x = np.piece\b5x
   cp.piece\b5y = np.piece\b5y
   cp.piece\b6x = np.piece\b6x
   cp.piece\b6y = np.piece\b6y

 Return

;**********************************************************
;
;- GameEnd
;
;**********************************************************
GameEnd:
  Gosub SaveHiScore
  End   ; :D enjoy


; IDE Options = PureBasic 4.10 (Windows - x86)
; CursorPosition = 106
; FirstLine = 85
; Folding = -
; UseIcon = images\icon.ico
; Executable = Blocks.exe
comments powered by Disqus