-
Notifications
You must be signed in to change notification settings - Fork 3
/
tetris.pas
573 lines (538 loc) · 11.4 KB
/
tetris.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
{$APPTYPE GUI}
program tetris;
//ax_pokl制作
uses windows, display, math;
function r2s(r: real): string;
var
i1, i2: longint;
begin
i1 := Trunc(r);
i2 := Trunc((r - i1) * 10);
r2s := i2s(i1) + '.' + i2s(i2);
end;
{$I tetris_sound.inc}
{$I tetris_pce.inc}
{$I tetris_def.inc}
{$I tetris_ai.inc}
const
ck: array[-1..8] of longword = ($000000,
$1F1F1F, $7F7F7F, $7F7FFF, $7FFF7F,
$FF7F7F, $7FFFFF, $FFFF7F, $FF7FFF,
$BFBFBF);
//背景色,7种方块颜色
var
sz: shortint;
//每格大小
var
i, j, k, l, m, n: longint;
b: boolean;
si, sj, sk: longint;
var
ksum, Ln, sum, sln: longint;
//方块数,行数,局数,总行数
lv, aln: real;
//等级,平均行数
var
stickb: boolean;
//移动成功标识
var
gamet, stept, lastt: longword;
//新局时间,单步时间,下落时间,AI时间
downt, leftt, rightt: longword;
oldt, framet:longword;
aps: real;
//手速
const
base = 2;
var
key: longword;
//按键
var
aion: shortint = 0;
//快速AI开关
var
pause: boolean = false;
ksz: boolean = false;
bmpb: boolean = true;
highb: shortint = 0;
winb: boolean = false;
var
bmp: array[1..8] of pbitmap;
procedure getk();
//新方块
begin
ksum := ksum + 1;
//方块计数加一
for i := 1 to depthm - 1 do
aiknext[i] := aiknext[i + 1];
//方块序列前移
if ksz then
aiknext[depthm].knd := Random(2) + 3
else
aiknext[depthm].knd := Random(7) + 1;
aiknext[depthm].sit := 1;
aiknext[depthm].posx := wbd div 2 + 1;
aiknext[depthm].posy := hbd - 1;
//添加方块到方块序列
lv := logn(base, Abs(Ln) + base);
//计算等级
stept := Round(1000 / lv);
//更新单步下落时间
for sj := 1 to hbd do
begin
aibd2[sj, 0] := 0;
for si := 1 to wbd do
begin
if bd[si, sj] > 0 then
aibd2[sj, 0] := aibd2[sj, 0] or 1;
aibd2[sj, 0] := aibd2[sj, 0] shl 1;
end;
aibd2[sj, 0] := (aibd2[sj, 0] shl 1) or bdcnull;
//重新设置AI盘面
end;
end;
procedure drawbd(); forward;
procedure New();
//初始化
begin
for i := -1 to wbd + 1 do
for j := -1 to hbd + 1 do
bd[i, j] := 1;
for i := 1 to wbd do
for j := 1 to hbd + 1 do
bd[i, j] := 0;
//初始化棋盘
for l := 1 to depthm do
getk();
//获取第一块方块
sum := sum + 1;
//局数加一
sln := sln + Ln;
//更新行数总和
aln := sln / sum;
//计算平均行数
Ln := 0;
ksum := 0;
//行数,块数清零
gamet := gettime;
//重计新局时间
stept := 1000;
//重设单步时间
aps := 0;
lv := 1;
playsound('start');
if aion = 0 then
replaymusic();
end;
procedure Clear();
begin
sum := 0;
sln := 0;
aln := 0;
New();
end;
function dead(): boolean;
//判定死亡
begin
with aiknext[1] do
begin
if sit = 5 then
sit := 1;
dead := false;
for si := -2 to 1 do
for sj := -2 to 1 do
if pce[knd, sit, - 1 - sj, si] then
if not (bd[posx + si, posy + sj] = 0) then
dead := true;
end;
end;
function Erase(): shortint;
//消行
begin
j := 1;
for i := 1 to hbd + 1 do
begin
b := true;
for k := 1 to wbd do
if bd[k, i] = 0 then
b := false;
if not (b) then
begin
for k := 1 to wbd do
bd[k, j] := bd[k, i];
j := j + 1;
end;
end;
Erase := i - j + 1;
if aion = 0 then
begin
if Erase > 0 then
playsound('erase');
if Erase > 2 then
playsound('erase2');
end; {
if erase<=2 then erase:=-erase
else erase:=erase-1; }
end;
procedure High();
begin
for i := 1 to wbd do
begin
for j := hbd downto 2 do
bd[i, j] := bd[i, j - 1];
bd[i, 1] := ((i + highb) mod 2) * 8;
end;
highb := 1 - highb;
end;
procedure junk();
begin
for i := 1 to wbd do
begin
for j := hbd downto 2 do
bd[i, j] := bd[i, j - 1];
bd[i, 1] := Random(2) * 8;
end;
end;
procedure Next();
//下一步
begin
with aiknext[1] do
for si := -2 to 1 do
for sj := -2 to 1 do
if pce[knd, sit, - 1 - sj, si] then
bd[posx + si, posy + sj] := knd;
//固定方块到盘面
Ln := Ln + Erase();
//消行并更新行数
getk();
//获取新方块
aps := 1000 / (((gettime - gamet + 1) / ksum));
//计算手速
end;
procedure stick(knd, sit, posx, posy: shortint);
//判断是否能塞入(移动或旋转)
begin
stickb := true;
for si := -2 to 1 do
for sj := -2 to 1 do
if pce[knd, sit, - 1 - sj, si] then
if not (bd[posx + si, posy + sj] = 0) then
stickb := false;
if stickb = true then
//如果能塞入则移动
begin
aiknext[1].knd := knd;
aiknext[1].sit := sit;
aiknext[1].posx := posx;
aiknext[1].posy := posy;
end;
end;
procedure Left();
//左移一格
begin
with aiknext[1] do
stick(knd, sit, posx - 1, posy);
leftt := gettime;
end;
procedure Right();
//右移一格
begin
with aiknext[1] do
stick(knd, sit, posx + 1, posy);
rightt := gettime;
end;
procedure up();
//旋转一下
begin
with aiknext[1] do
stick(knd, sit mod 4 + 1, posx, posy);
end;
procedure up2();
//旋转一下
begin
with aiknext[1] do
stick(knd, (sit + 2) mod 4 + 1, posx, posy);
end;
procedure down2();
//下落一格
begin
with aiknext[1] do
stick(knd, sit, posx, posy - 1);
downt := gettime;
//更新下落时间
end;
procedure down();
//下落一格并固定
begin
down2();
if not (stickb) and not (dead) then
Next();
//如果移动失败则下一步
end;
procedure space();
//下落到底
begin
repeat
down();
until not (stickb);
end;
procedure ai();
//AI移动
begin
aigo(1);
//AI计算
while airsit > 0 do
begin
up();
airsit := airsit - 1;
end;
//AI旋转
while airpos < 0 do
begin
Left();
airpos := airpos + 1;
end;
//AI左移
while airpos > 0 do
begin
Right();
airpos := airpos - 1;
end;
//AI右移
space();
end;
procedure loadbmps();
begin
bmp[1] := loadbmp('bmps\0.bmp');
bmp[2] := loadbmp('bmps\1.bmp');
bmp[3] := loadbmp('bmps\4.bmp');
bmp[4] := loadbmp('bmps\6.bmp');
bmp[5] := loadbmp('bmps\2.bmp');
bmp[6] := loadbmp('bmps\5.bmp');
bmp[7] := loadbmp('bmps\7.bmp');
bmp[8] := loadbmp('bmps\8.bmp');
end;
procedure drawk(x, y: shortint; c: shortint);
//绘制方块
begin
if (c <= 0) then
bar(sz * (x - 1), sz * (hbd - y), sz - 1, sz - 1, ck[c], ck[c])
else if bmpb then
drawbmp(bmp[c], 0, 0, 16, 16, sz * (x - 1), sz * (hbd - y), sz, sz)
else
bar(sz * (x - 1), sz * (hbd - y), sz - 1, sz - 1, white, ck[c]);
end;
procedure drawt(s: string; Pos: shortint; c: longword);
begin
bar(wbd * sz, (Pos - 1) * sz, 6 * sz, sz);
drawtextxy(s, Round((wbd + 3 - Length(s) / 4) * sz), (Pos - 1) * sz, c, black);
end;
procedure drawbd();
//绘制盘面
begin
display.Clear();
for i := 1 to wbd do
for j := 1 to hbd do
bddraw[i, j] := bd[i, j];
with aiknext[1] do
for i := -2 to 1 do
for j := -2 to 1 do
if pce[knd, sit, - 1 - j, i] then
bddraw[posx + i, posy + j] := knd;
//固定下落中的方块
for i := 1 to wbd do
for j := 1 to hbd do
drawk(i, j, bddraw[i, j]);
//绘制盘面方块
for k := 2 to depthm do
with aiknext[k] do
begin
for i := -2 to 1 do
for j := -1 to 1 do
if pce[knd, sit, - 1 - j, i] then
drawk(wbd + 4 + i, hbd - 2 + j - k * 3, knd)
else
drawk(wbd + 4 + i, hbd - 2 + j - k * 3, - 1);
//绘制next区域
drawt('next ' + i2s(k - 1), 2 + k * 3, ck[knd]);
//绘制next文字
end;
drawt('Game ' + i2s(sum), 1, ck[sum mod 7 + 1]);
drawt('Score ' + r2s(aln), 2, ck[sum mod 7 + 1]);
drawt('Level ' + r2s(lv), 3, ck[Trunc(lv) mod 7 + 1]);
drawt('APS ' + r2s(aps), 4, ck[Trunc(aps * 10) mod 7 + 1]);
drawt('Line ' + i2s(Abs(Ln)), 5, ck[Abs(Ln) mod 7 + 1]);
drawt('Depth ' + i2s(aidepthc), 6, ck[aidepthc]);
if pause then
drawt('Paused', 7, ck[5])
else
drawt('Paused', 7, black);
freshwin();
end;
procedure SetSize(size: shortint);
begin
winb := false;
sz := size;
if not(IsWin()) then
CreateWin((wbd + 6) * sz, hbd * sz, black, black);
display.SetSize((wbd + 6) * sz, hbd * sz);
//建立窗口
winb := true;
settitle('Tetris ax');
//设置窗口刷新频率
setfontsize(sz div 2, sz);
//设置文字大小
setfontname('Consolas');
//设置字体
New();
end;
procedure setbdsize(w, h: shortint);
begin
wbd := w;
hbd := h;
ainew2();
SetSize(sz);
end;
procedure init();
//程序初始化
begin
Randomize;
SetSize(25);
framet:=30;
oldt:=gettime;
//初始化随机数发生器
loadbmps();
ainew2();
//AI初始化
drawbd();
//首次绘图
end;
begin
//主程序
initsound();
//初始化音频
init();
//初始化程序
repeat
//主循环
if aion > 0 then
//判断AI是否打开
begin
ai();
if dead then
New();
if aion=1 then drawbd();
end;
//aion
// checkmusic();
if isnextmsg then
begin
if iskey() then
//判断是否按键
begin
key := getkey();
//获取按键
case key of
k_1:
if aion=1 then aion := 0 else aion:=1;
k_2:
if aion=2 then aion := 0 else aion:=2;
else
aion := 0;
end;
case key of
k_left:
Left();
k_right:
Right();
k_down:
down();
k_up:
up();
96:
up2();
k_space:
space();
k_enter:
if dead then
New()
else
ai();
k_3:
aidepthc := aidepthc mod depthm + 1;
k_4:
soundon := not (soundon);
k_5:
musicon := not (musicon);
k_9:
bmpb := not (bmpb);
107:
SetSize(min(sz + 2,64));
109:
SetSize(max(sz - 2,1));
187:
SetSize(min(sz + 2,64));
189:
SetSize(max(sz - 2,1));
k_C:
Clear();
k_H:
High();
k_J:
junk();
k_P:
pause := not (pause);
k_R:
New();
k_W:
setbdsize(wbd, max(hbd - 1,4));
k_A:
setbdsize(max(wbd - 1,4), hbd);
k_S:
setbdsize(wbd, min(hbd + 1,64));
k_D:
setbdsize(min(wbd + 1,64), hbd);
k_Z:
ksz := not (ksz);
k_esc:
Halt();
else
if dead then
New();
end;
//根据按键执行操作
case key of
k_left:;
k_right:;
k_up:
playsound('up');
k_down:;
k_space:
playsound('space');
k_5:
if musicon then
replaymusic()
else
stopaudio(musiccur);
else
playsound('click');
end;//case
//根据按键播放声音
end;//key
if dead then
playsound('lost');
end;//msg
if (gettime > downt + stept) and not (pause) then
//判断当前时间是否已超过单步下落时间
down();
//取下一条消息
if aion<2 then sleep(1);
// if aion=2 then framet:=stept else framet:=15;
if (gettime > oldt + framet) then
begin
while gettime > oldt + framet do oldt:=oldt + framet;
drawbd();
end;
//重绘盘面
until iskey(k_esc) or not(iswin());
end.