-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAIN.PAS
1106 lines (976 loc) · 25.4 KB
/
MAIN.PAS
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
// FIXED bugs: X plane movement at the edges!!!!
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
FastIMG, FastBMP, FastDraw, FastRGB, ExtCtrls,FastFX, StdCtrls, Menus,abox,
scores,chskin,inifiles;
type TPlayMatrix = array of array of byte;
TBlock = array[0..3] of array[0..3] of byte;
TGameState = (gsPaused, gsPlaying, gsCreatingBlock);
TBlockType = integer;
TTetrisGame = record
Xpos : integer;
Ypos : integer;
GameState : TGameState;
Block : TBlock;
BlockType : TBlockType;
PlayMatrix : TPlayMatrix;
BlockBitmap : TFastBMP;
rotateindex : Integer;
NumRotations : integer;
CurrLevel : integer;
LinesRemoved : integer;
end;
type
TfrmTetris = class(TForm)
GameClock: TTimer;
panelScore: TPanel;
Label1: TLabel;
Label2: TLabel;
panelPlayArea: TPanel;
PlayArea: TFastIMG;
MainMenu: TMainMenu;
mnuGame: TMenuItem;
mnuExit: TMenuItem;
mnuNewGame: TMenuItem;
mnuOptions: TMenuItem;
mnuSFX: TMenuItem;
N1: TMenuItem;
mnuPause: TMenuItem;
mnuHelp: TMenuItem;
mnuAbout: TMenuItem;
mnuScores: TMenuItem;
pnNB: TPanel;
NB: TFastIMG;
mnuNB: TMenuItem;
Label3: TLabel;
mnuCS: TMenuItem;
BackIMG: TImage;
Label4: TLabel;
Skill1: TMenuItem;
mnuStartingLevels: TMenuItem;
mnuStartingRows: TMenuItem;
N01: TMenuItem;
N11: TMenuItem;
N21: TMenuItem;
N31: TMenuItem;
N41: TMenuItem;
N51: TMenuItem;
N61: TMenuItem;
N71: TMenuItem;
N81: TMenuItem;
N91: TMenuItem;
panelscr1: TPanel;
lblSCR: TLabel;
panelscr2: TPanel;
lbllevel: TLabel;
panelscr3: TPanel;
lbllines: TLabel;
procedure GameClockTimer(Sender: TObject);
procedure btnNewClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure mnuNewGameClick(Sender: TObject);
procedure mnuExitClick(Sender: TObject);
procedure mnuSFXClick(Sender: TObject);
procedure mnuPauseClick(Sender: TObject);
procedure mnuAboutClick(Sender: TObject);
procedure mnuScoresClick(Sender: TObject);
procedure mnuNBClick(Sender: TObject);
procedure mnuCSClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure N01Click(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure CenterPlayArea();
private
{ Private declarations }
procedure RandomOutro;
public
{ Public declarations }
procedure NewGame;
procedure HMatrixShift(iy: integer);
procedure AdvanceGame;
procedure RemoveBlocks;
procedure RemoveLine(iy: integer);
procedure PutBlock(ix,iy: integer);
procedure RemoveBlock(ix,iy: integer);
procedure DrawMatrix;
procedure ChooseBlockBitmap(BlockType : TBlockType; var Bitmap : TFastBmp);
function CheckPutY(BLK : TBlock; ix, iy : integer) : boolean;
function GetNumRotations(BlockType : TBlockType): integer;
function initBlock(BlockType: TBlockType): TBlock;
function GetNextRotation(BlockType :TBlockType; Rotateindex: integer): TBlock;
procedure NeutralizeBlock;
procedure RotateBlock;
procedure DropDown;
procedure DrawBlock(dst: TFastRGB; ix, iy : integer; Bitmap : TFastBMP);
procedure DrawNextBlock;
procedure InitBlockBitmaps;
procedure EndGame;
procedure OnAppDeactivate(Sender : TObject);
end;
var
frmTetris: TfrmTetris;
const APPNAME = 'Tetris by Vedran Rodic';
INIFILENAME = 'tetris.ini';
const
// BLOCK DEFINITIONS
// field fill types
ftBLANK = 0;
ftMOVING = 1;
ftIBLOCK = 2;
ftBOXBLOCK = 3;
ftL1BLOCK = 4;
ftL2BLOCK = 5;
ftCANONBLOCK = 6;
ftSTH1BLOCK = 7;
ftSTH2BLOCK = 8;
// explains itself
NUMBLOCKS = 7;
// block types and their rotated counterparts
var
IBLOCKR0 : TBlock =
((1,1,1,1),
(0,0,0,0),
(0,0,0,0),
(0,0,0,0));
IBLOCKR1 : TBlock =
((0,0,1,0),
(0,0,1,0),
(0,0,1,0),
(0,0,1,0));
// BOX block
BOXBLOCKR0 : TBlock =
((1,1,0,0),
(1,1,0,0),
(0,0,0,0),
(0,0,0,0));
// L1 block
L1BLOCKR0 : TBlock =
((1,1,1,0),
(0,0,1,0),
(0,0,0,0),
(0,0,0,0));
L1BLOCKR1 : TBlock =
((1,1,0,0),
(1,0,0,0),
(1,0,0,0),
(0,0,0,0));
L1BLOCKR2 : TBlock =
((1,0,0,0),
(1,1,1,0),
(0,0,0,0),
(0,0,0,0));
L1BLOCKR3 : TBlock =
((0,0,1,0),
(0,0,1,0),
(0,1,1,0),
(0,0,0,0));
// L2 block
L2BLOCKR0 : TBlock =
((1,1,1,0),
(1,0,0,0),
(0,0,0,0),
(0,0,0,0));
L2BLOCKR1 : TBlock =
((1,0,0,0),
(1,0,0,0),
(1,1,0,0),
(0,0,0,0));
L2BLOCKR2 : TBlock =
((0,0,1,0),
(1,1,1,0),
(0,0,0,0),
(0,0,0,0));
L2BLOCKR3 : TBlock =
((0,1,1,0),
(0,0,1,0),
(0,0,1,0),
(0,0,0,0));
// CANON block
CANONBLOCKR0 : TBlock =
((1,1,1,0),
(0,1,0,0),
(0,0,0,0),
(0,0,0,0));
CANONBLOCKR1 : TBlock =
((0,1,0,0),
(0,1,1,0),
(0,1,0,0),
(0,0,0,0));
CANONBLOCKR2 : TBlock =
((0,1,0,0),
(1,1,1,0),
(0,0,0,0),
(0,0,0,0));
CANONBLOCKR3 : TBlock =
((0,1,0,0),
(1,1,0,0),
(0,1,0,0),
(0,0,0,0));
// something1 block
STH1BLOCKR0 : TBlock =
((1,1,0,0),
(0,1,1,0),
(0,0,0,0),
(0,0,0,0));
STH1BLOCKR1 : TBlock =
((0,1,0,0),
(1,1,0,0),
(1,0,0,0),
(0,0,0,0));
// something2 block
STH2BLOCKR0 : TBlock =
((0,1,1,0),
(1,1,0,0),
(0,0,0,0),
(0,0,0,0));
STH2BLOCKR1 : TBlock =
((1,0,0,0),
(1,1,0,0),
(0,1,0,0),
(0,0,0,0));
var TetrisGame : TTetrisGame;
CANTOUCH : Boolean;
Score : LONGWORD; // a quickie!
SQUAREWIDTH :integer =27;
SQUAREHEIGHT : integer = 22;
FIELDWIDTH : integer= 10;
FIELDHEIGHT : integer= 20;
NextBlock : TBlockType;
Delayer : byte; // used to delay after block has soft landed
// enables player to move the rock some time after
procedure DrawToFBMP(Bmp, dst:TFastRGB;ix, iy : integer);
// bitmap data constants and vars
const
SKINDIR = 'DATA';
var
ibmp : TFastBMP;
boxbmp: TFastBMP;
l1bmp: TFastBMP;
l2bmp:TFastBMP;
canonbmp : TFastBmp;
sth1bmp : TFastBMP;
sth2bmp : TFastBMP;
currskin : string;
//background bitmap
backbmp : TFastBMP;
DELAYNUM : word = 1;
STARTYNUM: integer =0;
STARTINGROWS: integer;
procedure Delay(time : longword);
function GetNextBlock(): TBlockType;
implementation
{$R *.DFM}
function GetNextBlock(): TBlockType;
begin
Result := Random(NUMBLOCKS)+2;
end;
procedure TfrmTetris.NewGame;
var x, y : integer;
rnd : integer;
RNDOK : boolean;
begin
Randomize;
with TetrisGame do
begin
Caption := APPNAME;
SetLength(PlayMatrix, FIELDHEIGHT,FIELDWIDTH);
for y := 0 to FIELDHEIGHT -1 do
begin
for x := 0 to FIELDWIDTH -1 do
begin
PlayMatrix[y,x] := ftBLANK;
end;
end;
LinesRemoved := 0;
CurrLevel := 1;
GameState := gsCreatingBlock;
PlayArea.CreateNew(SQUAREWIDTH * FIELDWIDTH, SQUAREHEIGHT * FIELDHEIGHT, PFASTBMP);
NB.CreateNew(SQUAREWIDTH * 4, SQUAREHEIGHT * 5, PFASTBMP);
PlayArea.Draw;
Delayer := 0;
CanTouch := False;
GameClock.Enabled := True; // start!;
Score := 0;
lblLevel.Caption := IntToStr(GameClock.Interval);
NextBlock := GetNextBlock;
pnNB.Visible := mnuNB.Checked;
end;
centerPlayArea;
if STARTINGROWS <> 0 then
begin
RNDOK := False;
for y := FIELDHEIGHT -1 downto (FIELDHEIGHT )-STARTINGROWS do
begin
for x := 0 to FIELDWIDTH -1 do
begin
rnd :=Random(NUMBLOCKS+2);
if rnd > NUMBLOCKS then rnd := RANDOM(NUMBLOCKS div 2);
if rnd = ftMoving then rnd := ftBlank;
if rnd = ftBlank then RNDOK := True;
TetrisGame.PlayMatrix[y,x] := rnd;
end;
// this I do to make sure that no line is totaly full
if not RNDOK then
begin
TetrisGame.PlayMatrix[y,FIELDWIDTH-1] := ftBlank;
end;
RNDOK := False;
end;
DrawMatrix;
end;
end;
procedure TfrmTetris.GameClockTimer(Sender: TObject);
begin
AdvanceGame;
end;
procedure TfrmTetris.btnNewClick(Sender: TObject);
begin
NewGame;
end;
procedure TfrmTetris.AdvanceGame;
begin
with TetrisGame do
begin
lblSCR.Caption := IntToStr(Score);
lblLines.Caption := IntToStr(LinesRemoved);
case GameState of
gsPlaying:
begin
if CheckPUTY(Block, XPOS,YPOS+1) then
begin
RemoveBlock(XPOS,YPOS);
INC(YPOS);
PutBlock(XPOS,YPOS);
end
else
begin
if Delayer = DELAYNUM then
begin
Inc(SCORE,YPOS);
NeutralizeBlock;
GameState := gsCreatingBlock;
CANTOUCH := False;
RemoveBlocks;
DrawMatrix;
Delayer := 0;
end
else inc(Delayer);
end;
IF YPOS = STARTYNUM then
begin
EndGame;
//exit;
end;
end;
gsCreatingBlock:
begin
CanTouch := True;
XPOS := (FIELDWIDTH div 2) - 2;
YPOS := STARTYNUM;
RotateIndex := 0;
BlockType := NextBlock;
NextBlock := GetNextBlock;
NumRotations := GetNumRotations (blocktype);
Block := initBlock(BlockType);
ChooseBlockBitmap(BlockType, BlockBitmap);
GameState := gsPlaying;
if not CheckPutY(Block,XPOS,YPOS) then
begin
EndGame;
Exit;
end;
PutBlock(XPOS,YPOS);
if mnuNB.Checked then DrawNextBlock;
end;
end;
PlayArea.Draw;
//Caption := IntToStr(XPOS); // DEBUG
end;
end;
procedure TfrmTetris.HMatrixShift(iy: integer);
var TMP : TPlayMatrix;
x,y : integer;
begin
SetLength(TMP, FIELDHEIGHT,FIELDWIDTH);
for x:= 0 to FIELDWIDTH -1 do
TMP[0,x] := ftBLANK;
for y := iy to FIELDHEIGHT -1 do
begin
for x := 0 to FIELDWIDTH -1 do
begin
TMP[y,x] := TetrisGame.PlayMatrix[y,x]
end;
end;
for y := 0 to iy-1 do
begin
for x := 0 to FIELDWIDTH -1 do
begin
TMP[y+1,x] := TetrisGame.PlayMatrix[y,x]
end;
end;
TetrisGame.PlayMatrix := TMP;
end;
procedure TfrmTetris.PutBlock(ix,iy: integer);
var x,y : integer;
begin
with TetrisGame do
begin
for y := 0 to 3 do
for x := 0 to 3 do
begin
if Block[y,x] = ftMoving then
begin
PlayMatrix [iy+y,ix+x] := ftMoving;
DrawBlock(PlayArea.Bmp,ix+x,iy+y,BlockBitmap);
end;
end;
end;
end;
procedure TfrmTetris.RemoveBlock(ix,iy: integer);
var x,y : integer;
begin
with TetrisGame do
begin
for y := 0 to 3 do
for x := 0 to 3 do
begin
if Block[y,x] = ftMOVING then
begin
PlayMatrix [iy+y,ix+x] := ftBLANK;
FillRect(PlayArea.Bmp,(ix+x)*SQUAREWIDTH,(iy+y)*SQUAREHEIGHT,SQUAREWIDTH,SQUAREHEIGHT, FRGB(0,0,0));
end;
end;
end;
end;
procedure TfrmTetris.ChooseBlockBitmap(BlockType : TBlockType;Var Bitmap :TFastBMP);
begin
case BlockType of
ftIBLOCK: Bitmap := ibmp;
ftBOXBLOCK: Bitmap := boxbmp;
ftCANONBLOCK: Bitmap := canonbmp;
ftL2BLOCK: Bitmap := l2bmp;
ftL1BLOCK: Bitmap := l1bmp;
ftSTH1BLOCK : Bitmap := sth1bmp;
ftSTH2BLOCK : Bitmap := sth2bmp;
end;
end;
procedure TfrmTetris.RemoveBlocks;
var LINEFULL : boolean;
x,y : integer;
begin
for y := FIELDHEIGHT -1 downto 0 do
begin
LINEFULL := True;
for x := 0 to FIELDWIDTH -1 do
begin
if TetrisGame.PlayMatrix[y,x] = ftBLANK then
begin
LINEFULL := false;
break;
end;
end;
if LINEFULL then
begin
Inc(Score,100);
RemoveLine(y);
RemoveBlocks;
end;
end;
end;
procedure TfrmTetris.RemoveLine(iy: integer);
var i : integer;
begin
inc(TetrisGame.LinesRemoved);
for i := 0 to FIELDWIDTH -1 do
begin
TetrisGame.PlayMatrix[iy,i] := ftBLANK;
end;
HMatrixShift(iy);
if mnuSFX.checked then
begin
DrawMatrix;
PlayArea.Draw;
Delay(100);
end;
end;
procedure TfrmTetris.DrawMatrix;
var x, y : integer;
var tmpbmp : TFastBMP;
begin
for y := 0 to FIELDHEIGHT -1 do
begin
for x := 0 to FIELDWIDTH -1 do
begin
if TetrisGame.PlayMatrix[y,x] = ftBLANK then
FillRect(PlayArea.Bmp, x*SQUAREWIDTH, y* SQUAREHEIGHT,SQUAREWIDTH, SQUAREHEIGHT, FRGB(0,0,0))
else if TetrisGame.PlayMatrix[y,x] = ftMOVING then
begin
DrawBlock(PlayArea.Bmp,x,y,TetrisGame.BlockBitmap);
end
else
begin
ChooseBlockBitmap(TetrisGame.PlayMatrix[y,x],tmpbmp);
DrawBlock(PlayArea.BMP,x,y,tmpbmp);
end;
end;
end;
end;
function TfrmTetris.CheckPutY(BLK : TBlock; ix, iy : integer) : boolean;
var x,y : integer;
begin
Result := True;
with TetrisGame do
begin
for y := 0 to 3 do
begin
for x := 0 to 3 do
begin
if BLK[y,x] = ftMoving then
begin
if (iy+y) > FIELDHEIGHT -1 then
begin
Result := False;
exit;
end;
{if (iy+y) < 0 then
begin
Result := False;
exit;
end;}
if (ix+x) > FIELDWIDTH -1 then
begin
Result := False;
exit;
end;
if (ix+x) < 0 then
begin
Result := False;
exit;
end;
if (PlayMatrix [iy+y,ix+x] > ftMoving) then
begin
Result := False;
exit;
end
else
begin
Result := True;
end;
end;
end;
end;
end;
end;
function TfrmTetris.GetNumRotations(BlockType : TBlockType): integer;
begin
case BlockType of
ftIBLOCK: Result := 1;
ftBOXBLOCK: Result := 0;
ftCANONBLOCK: Result := 3;
ftL2BLOCK: Result := 3;
ftL1BLOCK: Result := 3;
ftSTH1BLOCK : Result := 1;
ftSTH2BLOCK : Result := 1;
end;
end;
function TfrmTetris.initBlock(BlockType: TBlockType): TBlock;
begin
case BlockType of
ftIBlock: Result := IBLOCKR0;
ftBoxBlock: Result := BOXBLOCKR0;
ftL1Block: Result := L1BLOCKR0;
ftL2Block: Result := L2BLOCKR0;
ftCanonBlock: Result := CANONBLOCKR0;
ftSTH1Block: Result := STH1BLOCKR0;
ftSTH2Block: Result := STH2BLOCKR0;
end;
end;
procedure TfrmTetris.FormCreate(Sender: TObject);
var IniFile : TIniFile;
i : Integer;
begin
Application.OnDeactivate :=OnAppDeactivate;
BackIMG.Width := ClientRect.Right - ClientRect.Left;
BackIMG.Height := ClientRect.Bottom - ClientRect.Top;
iniFile := TIniFile.Create(extractFilePath(Application.EXEName) + INIFILENAME);
currskin := inifile.ReadString('Data','DataDirName','classic');
mnuNB.Checked := inifile.ReadBool('Options','PreviewBlock',false);
mnuSFX.Checked := inifile.ReadBool('Options','SFX',true);
STARTINGROWS := inifile.ReadInteger('Options','StartingRows',0);
GameClock.Interval := inifile.ReadInteger('Options','TimeDelay',140);
inifile.Free;
for i := 0 to mnuStartingRows.Count - 1 do
begin
if mnuStartingRows[i].Tag = STARTINGROWS then
begin
mnuStartingRows[i].Checked := true;
break;
end;
end;
TetrisGame.CurrLevel := 1;
InitBlockBitmaps;
NewGame;
Caption := APPNAME;
end;
procedure TfrmTetris.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
//Caption := IntTostr(key); // DEBUG
if mnuPause.Checked then Exit;
if not CANTOUCH then Exit; // VR2023 not sure if needed
with TetrisGame do
begin
case key of
39: // RIGHT
begin
IF CheckPutY(Block, XPOS+1,YPOS) then
begin
RemoveBlock(XPOS,YPOS);
INC(XPOS);
PutBlock(XPOS,YPOS);
PlayArea.Draw;
end;
end;
37: // LEFT
begin
IF CheckPutY(Block, XPOS-1,YPOS) then
begin
RemoveBlock(XPOS,YPOS);
DEC(XPOS);
PutBlock(XPOS,YPOS);
PlayArea.Draw;
end;
end;
//113: NewGame;
38,12: // UP
begin
RemoveBlock(XPOS,YPOS);
RotateBlock;
PutBlock(XPOS,YPOS);
PlayArea.Draw;
end;
116:// F5
begin
GameClock.Interval := GameClock.interval -10;
lblLevel.Caption := IntToStr(GameClock.Interval);
end;
117:// F6
begin
GameClock.Interval := GameClock.interval +10;
lblLevel.Caption := IntToStr(GameClock.Interval);
end;
32: // SPACE
begin
IF CheckPutY(Block, XPOS,YPOS+1) then
begin
RemoveBlock(XPOS,YPOS);
INC(YPOS);
PutBlock(XPOS,YPOS);
PlayArea.Draw;
end;
end;
40: // DOWN
begin
DropDown;
end;
end;
end;
end;
procedure TfrmTetris.NeutralizeBlock;
var x,y : integer;
begin
with TetrisGame do
begin
for y := 0 to 3 do
for x := 0 to 3 do
begin
if Block[y,x] = ftMoving then
begin
PlayMatrix [ypos+y,xpos+x] := Blocktype;
end;
end;
end;
end;
function TfrmTetris.GetNextRotation(BlockType :TBlockType; Rotateindex: integer): TBlock;
begin
case BlockType of
ftBOXBLOCK: Result := BOXBLOCKR0;
ftIBLOCK:
begin
case Rotateindex of
0: Result := IBLOCKR0;
1: Result := IBLOCKR1;
end;
end;
ftCANONBLOCK:
begin
case Rotateindex of
0: Result := CANONBLOCKR0;
1: Result := CANONBLOCKR1;
2: Result := CANONBLOCKR2;
3: Result := CANONBLOCKR3;
end;
end;
ftL2BLOCK:
begin
case Rotateindex of
0: Result := L2BLOCKR0;
1: Result := L2BLOCKR1;
2: Result := L2BLOCKR2;
3: Result := L2BLOCKR3;
end;
end;
ftL1BLOCK:
begin
case Rotateindex of
0: Result := L1BLOCKR0;
1: Result := L1BLOCKR1;
2: Result := L1BLOCKR2;
3: Result := L1BLOCKR3;
end;
end;
ftSTH1BLOCK :
begin
case Rotateindex of
0: Result := STH1BLOCKR0;
1: Result := STH1BLOCKR1;
end;
end;
ftSTH2BLOCK :
begin
case Rotateindex of
0: Result := STH2BLOCKR0;
1: Result := STH2BLOCKR1;
end;
end;
end;
end;
procedure TfrmTetris.RotateBlock;
var TMP : TBlock;
ri : integer;
begin
with TetrisGame do
begin
ri := rotateindex;
if NumRotations > RotateIndex then
inc(rotateindex)
else RotateIndex := 0;
TMP := GetNextRotation(BlockType, RotateIndex);
if not CheckPutY(TMP,XPOS,YPOS) then
begin
if (BlockType = ftL2Block) and (XPOS = FIELDWIDTH -2) and (RotateIndex = 2) then
begin
RemoveBlock(XPOS,YPOS);
if CheckPutY(TMP,XPOS-1,YPOS) then
begin
dec(xpos);
//PutBlock(XPOS,YPOS);
end
else
begin
PutBlock(XPOS,YPOS);
rotateindex := ri;
exit;
end;
end
else
begin
rotateindex := ri;
exit;
end;
end;
Block := GetNextRotation(BlockType, RotateIndex);
end;
end;
procedure TfrmTetris.DropDown;
var CANDOIT : boolean;
SCRTMP : integer;
begin
with TetrisGame do
begin
SCRTMP := YPOS*5;
CANDOIT := true;
while CANDOIT do
begin
CANDOIT := CheckPUTY(Block, XPOS,YPOS+1);
if CANDOIT then
begin
RemoveBlock(XPOS,YPOS);
INC(YPOS);
PutBlock(XPOS,YPOS);
end
else
begin
NeutralizeBlock;
GameState := gsCreatingBlock;
CANTOUCH := False;
RemoveBlocks;
DrawMatrix;
end;
IF YPOS = STARTYNUM then
begin
EndGame;
end;
end;
Inc(score,SCRTMP+YPOS);
end;
PlayArea.Draw;
lblSCR.Caption := IntToStr(Score);
end;
procedure TfrmTetris.mnuNewGameClick(Sender: TObject);
begin
mnuPause.Checked := False;
NewGame;
end;
procedure TfrmTetris.mnuExitClick(Sender: TObject);
begin
Close;
end;
procedure TfrmTetris.mnuSFXClick(Sender: TObject);
begin
mnuSFX.Checked := not mnuSFX.Checked;
end;
procedure TfrmTetris.mnuPauseClick(Sender: TObject);
begin
GameClock.Enabled := not GameClock.Enabled;
mnuPause.Checked := not GameClock.Enabled;
if mnuPause.Checked then Caption := APPNAME + ' (Paused)' else
Caption := APPNAME;
end;
procedure TfrmTetris.DrawBlock(dst: TFastRGB; ix, iy : integer; Bitmap : TFastBMP);
begin
DrawTOFbmp(Bitmap,dst,ix*SQUAREWIDTH,iy*SQUAREHEIGHT);
end;
procedure TfrmTetris.mnuAboutClick(Sender: TObject);
begin
if GameClock.Enabled then mnuPauseClick(self);
ABox.ShowAboutBox;
end;
procedure DrawToFBMP(Bmp, dst:TFastRGB;ix, iy : integer);
var x,y: Integer;
begin
for y:=0 to SQUAREHEIGHT-1 do
begin
for x:=0 to SQUAREWIDTH-1 do
begin
Dst.Pixels[iy+y,ix+x]:=Bmp.Pixels[y,x];
end;
end;
end;
procedure TfrmTetris.mnuScoresClick(Sender: TObject);
begin
if GameClock.Enabled then mnuPauseClick(self);
ShowScores(0);
end;
procedure TfrmTetris.DrawNextBlock;
var x,y : integer;
Blocky : TBlock;
tmpbmp : TFastBMP;
begin
FillRect(NB.Bmp,0,0,SQUAREWIDTH *4, SQUAREHEIGHT *4, FRGB(0,0,0));
Blocky := initBlock(NextBlock);
ChooseBLockBitmap(nextblock,tmpbmp);
for y := 0 to 3 do
for x := 0 to 3 do
if Blocky[y,x] = ftMoving then
DrawBlock(NB.Bmp,x,y,tmpbmp);
NB.Draw;
end;
procedure TfrmTetris.mnuNBClick(Sender: TObject);
begin