-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit1.pas
443 lines (403 loc) · 10.1 KB
/
Unit1.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
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Vcl.StdCtrls, GameOver, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
ScorePanel: TPanel;
Beenden: TButton;
Button1: TButton;
PiecePreview: TPaintBox;
procedure FormActivate(Sender: TObject);
procedure IdleHandler(Sender: TObject; var Done: boolean);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure BeendenClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
type
TGrid = array [0 .. 103] of integer;
TPieceCoord = array [1 .. 4, 1 .. 2] of integer;
TPieces = array [1 .. 7] of TPieceCoord;
CONST
FPS_CAP = trunc(1000 / 30);
COLORS: array [0 .. 5] of integer = (clMenu, $AE4103, $3BCB72, $00D5FF,
$1C97FF, $1332FF);
// Order: L, J, O, I, T, S, Z
Pieces: TPieces = (((0, -1), (1, -1), (1, 0), (1, 1)),
((0, -1), (-1, -1), (-1, 0), (-1, 1)), ((0, 0), (1, 0), (0, 1), (1, 1)),
((0, -1), (0, 0), (0, 1), (0, 2)), ((0, 0), (-1, -1), (0, -1), (1, -1)),
((0, 0), (1, 0), (-1, 1), (0, 1)), ((0, 0), (-1, 0), (0, 1), (1, 1)));
var
GAME_STATE: (gsStart, gsPaused, gsGameOver);
GAME_TICK: LongInt;
DELTA_TIME: integer;
GAME_GRID: TGrid;
SCORE: Integer;
PIECE_GRID: TGrid;
CURRENT_PIECE: integer;
CURRENT_COLOR: integer;
CURRENT_PIECE_POS: array [1 .. 2] of integer;
CURRENT_PIECE_ROTATION: integer;
NEXT_PIECE: integer;
Form1: TForm1;
implementation
Uses Start;
{$R *.dfm}
function RotatePoints(const Points: TPieceCoord; rotation: integer)
: TPieceCoord;
var
I: integer;
begin
for I := 1 to 4 do
begin
case rotation of
1:
begin
Result[I][1] := Points[I][2];
Result[I][2] := -Points[I][1];
end;
2:
begin
Result[I][1] := -Points[I][1];
Result[I][2] := -Points[I][2];
end;
3:
begin
Result[I][1] := -Points[I][2];
Result[I][2] := Points[I][1];
end;
else
Result := Points;
end;
end;
end;
procedure Draw(form: TForm);
var
I, size, size_x, size_y: integer;
Buffer: TBitmap;
c: TCanvas;
begin
size := 35;
size_x := 8;
size_y := 13;
Buffer := TBitmap.Create;
Buffer.Width := Form1.Width;
Buffer.Height := Form1.Height;
c := Buffer.Canvas;
c.brush.style := bsSolid;
c.pen.style := psClear;
c.brush.style := bsClear;
c.pen.style := psSolid;
c.pen.color := clGray;
// DRAW GRID
for I := 0 to length(GAME_GRID) - 1 do
begin
c.brush.color := COLORS[GAME_GRID[I]];
c.rectangle(15 + (I mod 8) * size, 15 + trunc(I / 8) * size,
15 + size + (I mod 8) * size, 15 + size + trunc(I / 8) * size);
end;
// DRAW PIECE
for I := 0 to length(PIECE_GRID) - 1 do
begin
c.brush.color := COLORS[PIECE_GRID[I]];
if PIECE_GRID[I] = 0 then
c.brush.style := bsClear;
c.rectangle(15 + (I mod 8) * size, 15 + trunc(I / 8) * size,
15 + size + (I mod 8) * size, 15 + size + trunc(I / 8) * size);
end;
form.Canvas.Draw(0, 0, Buffer);
with form1.PiecePreview do
begin
canvas.brush.color := clWhite;
canvas.pen.Style := psSolid;
canvas.pen.color := clBlack;
canvas.rectangle(1,1,Width-1,height-1);
canvas.pen.Style := psClear;
canvas.brush.color := clGray;
for I := 1 to 4 do
begin
Canvas.Rectangle(2+(PIECES[NEXT_PIECE][I][1]+3)*25,2+(PIECES[NEXT_PIECE][I][2]+1)*25,27+(PIECES[NEXT_PIECE][I][1]+3)*25,27+(PIECES[NEXT_PIECE][I][2]+1)*25);
end;
end;
// PIECE PREVIEW
Application.ProcessMessages;
end;
procedure UpdateScore();
begin
Form1.ScorePanel.Caption := 'Score: '+InttoStr(SCORE);
end;
procedure clearRow(line_y: integer; var grid: TGrid);
var
I: integer;
begin
for I := 7 + (line_y * 8) downto 0 do
begin
if trunc(I / 8) = 0 then
grid[I] := 0
else
begin
grid[I] := grid[I - 8];
end;
end;
end;
procedure MovePiece(direction: boolean);
// direction: true-left | false-right
var
I: integer;
begin
if direction then
begin
// PRECHECK
for I := 0 to 103 do
begin
if (I mod 8 = 0) and (PIECE_GRID[I] <> 0) then
Abort;
if (I mod 8 > 0) and (GAME_GRID[I - 1] <> 0) and (PIECE_GRID[I] <> 0) then
Abort;
end;
// MODIFY
for I := 0 to 103 do
begin
if (I mod 8) = 7 then
PIECE_GRID[I] := 0
else if (I mod 8 = 0) and (PIECE_GRID[I] <> 0) then
Abort
else
begin
PIECE_GRID[I] := PIECE_GRID[I + 1];
end;
end;
dec(CURRENT_PIECE_POS[1]);
end
else
begin
// PRECHECK
for I := 103 downto 0 do
begin
if (I mod 8 = 7) and (PIECE_GRID[I] <> 0) then
Abort;
if (I mod 8 < 7) and (GAME_GRID[I + 1] <> 0) and (PIECE_GRID[I] <> 0) then
Abort;
end;
// MODIFY
for I := 103 downto 0 do
begin
if (I mod 8) = 0 then
PIECE_GRID[I] := 0
else
begin
PIECE_GRID[I] := PIECE_GRID[I - 1];
end;
end;
inc(CURRENT_PIECE_POS[1])
end;
end;
procedure ClearGrid(var grid: TGrid);
var
I: integer;
begin
for I := 0 to length(grid) - 1 do
grid[I] := 0
end;
procedure RotatePiece();
var
new_points: TPieceCoord;
I: integer;
begin
new_points := RotatePoints(Pieces[CURRENT_PIECE], CURRENT_PIECE_ROTATION);
ClearGrid(PIECE_GRID);
for I := 1 to 4 do
PIECE_GRID[CURRENT_PIECE_POS[1] + new_points[I][1] +
(new_points[I][2] + CURRENT_PIECE_POS[2]) * 8] := CURRENT_COLOR;
end;
procedure SpawnNextPiece();
var
I: integer;
begin
CURRENT_COLOR := random(5) + 1;
for I := 1 to 4 do
begin
PIECE_GRID[3 + Pieces[NEXT_PIECE][I][1] + (Pieces[NEXT_PIECE][I][2] + 1) *
8] := CURRENT_COLOR
end;
CURRENT_PIECE_POS[1] := 3;
CURRENT_PIECE_POS[2] := 1;
CURRENT_PIECE_ROTATION := 0;
CURRENT_PIECE := NEXT_PIECE;
NEXT_PIECE := random(length(Pieces)) + 1;
end;
procedure CheckFullRows();
var
I, J: integer;
full_row: boolean;
full_row_counter: Integer;
begin
full_row_counter := 0;
for I := 0 to 12 do
begin
full_row := true;
for J := 0 to 7 do
if GAME_GRID[(I * 8) + J] = 0 then
full_row := false;
if full_row = true then begin
clearRow(I, GAME_GRID);
inc(full_row_counter);
end;
end;
// SCORE SYSTEM: https://tetris.wiki/Scoring
case full_row_counter of
1: inc(SCORE, 40);
2: inc(SCORE, 100);
3: inc(SCORE, 300);
4..13: inc(SCORE,(1200*(full_row_counter-3))); // ab 4: 1200 & für jede weitere 1200 p.
end;
UpdateScore();
end;
procedure Pause(ms: integer);
var
current_time: LongInt;
pause_time: LongInt;
begin
current_time := GetTickCount;
pause_time := current_time + ms;
while current_time < (pause_time) do
begin
Application.ProcessMessages;
current_time := GetTickCount;
end;
end;
procedure AttachPiece();
var
I: integer;
begin
for I := 0 to 103 do
if PIECE_GRID[I] <> 0 then
GAME_GRID[I] := PIECE_GRID[I];
ClearGrid(PIECE_GRID);
CheckFullRows();
GAME_STATE := gsPaused;
Pause(500);
GAME_STATE := gsStart;
SpawnNextPiece();
end;
function CheckCollision(): boolean;
var
I: integer;
collide: boolean;
begin
collide := false;
for I := 0 to 103 do
begin
if (trunc(I / 8) = 12) and (PIECE_GRID[I] <> 0) then
collide := true
else if (PIECE_GRID[I] <> 0) and (GAME_GRID[I + 8] <> 0) then
collide := true;
end;
CheckCollision := collide;
end;
procedure PieceTick();
begin
if CheckCollision then
AttachPiece()
else
begin
clearRow(12, PIECE_GRID);
inc(CURRENT_PIECE_POS[2])
end;
if GAME_GRID[3] <> 0 then begin
GAME_STATE := gsGameOver;
Application.CreateForm(TForm2, Form2);
Form2.ShowModal;
end;
end;
procedure GameLoop;
var
time_index: LongInt;
begin
inc(GAME_TICK);
time_index := LongInt(GetTickCount);
case GAME_STATE of
gsStart:
begin
if ((GAME_TICK mod 10) = 0) then
PieceTick();
Draw(Form1);
end;
end;
DELTA_TIME := LongInt(GetTickCount) - time_index;
if DELTA_TIME < FPS_CAP then
Pause(FPS_CAP - DELTA_TIME);
Application.ProcessMessages;
end;
// -------------------------------------------------------------------------------------
procedure TForm1.FormKeyDown(Sender: TObject;
var Key: Word; Shift: TShiftState);
begin
if GAME_STATE = gsPaused then Abort;
case Key of
// https://keycode.info
32, 38, 87: // SPACE, W, UP ARROW
begin
inc(CURRENT_PIECE_ROTATION);
if CURRENT_PIECE_ROTATION = 4 then
CURRENT_PIECE_ROTATION := 0;
RotatePiece();
end;
37, 65: // LEFT ARROW, A
begin
MovePiece(true);
end;
39, 68: // RIGHT ARROW, D
begin
MovePiece(false);
end;
40, 83: // DOWN ARROW, S
begin
PieceTick();
end;
end;
end;
procedure TForm1.IdleHandler(Sender: TObject;
var Done: boolean);
begin
GameLoop;
Done := false;
end;
procedure TForm1.BeendenClick(Sender: TObject);
begin
GAME_STATE := gsGameOver;
Application.CreateForm(TForm2, Form2);
Form2.ShowModal;
// Form1.hide();
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GAME_STATE := gsGameOver;
close();
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
Randomize;
GAME_STATE := gsStart;
SCORE := 0;
UpdateScore();
GAME_TICK := 0;
DELTA_TIME := 0;
Application.OnIdle := IdleHandler;
ClearGrid(GAME_GRID);
ClearGrid(PIECE_GRID);
CURRENT_PIECE_ROTATION := 0;
NEXT_PIECE := random(length(Pieces)) + 1;
SpawnNextPiece();
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Form3.show();
end;
end.