📄 gm.pas
字号:
aBrick.tag:=0;
end;
//砖块变形
procedure TgmForm.ChangeBrick();
var
bufBrick:TMatrixData;
buf,aflag:integer;
begin
bufBrick:=curBrick; //copy curbrick to a buffer
buf:=curBrick.tag;
aflag:=curBrick.flag;
case aflag of
1,3,5:begin
bufBrick.tag:=(bufBrick.tag+1)mod 2;
SetMatrix2(bufBrick,aflag+buf,curBrick.xIndex,curBrick.yIndex);
end;
7,11,15:begin
bufBrick.tag:=(bufBrick.tag+1)mod 4;
SetMatrix2(bufBrick,aflag+buf,curBrick.xIndex,curBrick.yIndex);
end;
end;
if OutLeft(bufBrick) then
begin
while OutLeft(bufBrick)do
begin
if not OutRight(bufBrick) then
bufBrick.xIndex:=bufBrick.xIndex+1
else
break;
end;
if not(OutBottomRange(bufBrick)or OutRight(bufBrick)or OutLeft(bufBrick)) then
curBrick:=bufBrick;
end;
if OutRight(bufBrick) then
begin
while OutRight(bufBrick)do
begin
if not OutLeft(bufBrick) then
bufBrick.xIndex:=bufBrick.xIndex-1
else
break;
end;
if not(OutBottomRange(bufBrick)or OutLeft(bufBrick)or OutRight(bufBrick)) then
curBrick:=bufBrick;
end;
if not(OutLeft(bufBrick) or OutRight(bufBrick) or OutBottomRange(bufBrick)) then
begin
curBrick:=bufBrick;
end;
DrawMatrix(Canvas,curBrick);
end;
//更新数组中的数据,这个数组用来保存面板中那些位置需要画出砖块
function TgmForm.UpdateCell:integer;
var
i,j,k,x,y:integer;
str:string;
full:integer;
begin
result:=0;
for i:=0 to 3 do
begin
x:=curBrick.aMatrix[i][0]+curBrick.xIndex;
y:=curBrick.aMatrix[i][1]+curBrick.yIndex;
CellState[y,x]:=1;
end;
i:=19;
while (i>0) do
begin
full:=1;
for j:=0 to 13 do
full:=full*CellState[i,j];
if full>0 then
begin
inc(result);
for k:=i downto 1 do
for j:=0 to 13 do
CellState[k,j]:=CellState[k-1,j];
i:=i+1;
end;
dec(i);
end;
end;
//画出面板上剩下的砖块
procedure TgmForm.DrawOldCell;
var
i,j,x,y:integer;
notblank:boolean;
aRect:TRect;
str:string;
begin
notblank:=true;
for i:=19 downto 0 do
begin
if notblank then
begin
notblank:=false;
for j:=0 to 13 do
begin
if (CellState[i,j])>0 then
begin
y:=i*20;
x:=j*20;
aRect.Left:=x;
aRect.Top:=y;
aRect.Right:=x+20;
aRect.Bottom:=y+20;
Canvas.StretchDraw(aRect,picBrick);
notblank:=true;
end;
end;
end;
end;
end;
procedure TgmForm.FormDestroy(Sender: TObject);
begin
SaveSet;
FreeMem(pMove,fsMove);
FreeMem(pChange,fsChange);
end;
//显示有关游戏的数据
procedure TgmForm.DrawInfo;
var
offx,offy,x,y:integer;
begin
canvas.Font.Color:=clLime;
Canvas.Brush.Color:=clBlack;
Canvas.Font.Name:='Courier New';
Canvas.Font.Size:=10;
Canvas.TextOut(300,60,'speed:');
Canvas.TextOut(300,80,inttostr(speed));
Canvas.TextOut(300,100,'high score:');
Canvas.TextOut(300,120,inttostr(highscore));
Canvas.TextOut(300,140,'score:');
Canvas.TextOut(300,160,inttostr(score));
Canvas.TextOut(300,180,'Line:');
Canvas.TextOut(300,200,inttostr(TotalLine));
if timer1.Enabled then
Canvas.TextOut(300,220,'Running!')
else if not GameOver then
Canvas.TextOut(300,220,'Paused!')
else
Canvas.TextOut(300,220,'Stoped!');
end;
//降低方块下落速度,主要是通过修改Timer时间控件的属性
procedure TgmForm.DownSpeed;
begin
speed:=(speed-1+10)mod 10;
timer1.Interval:=500-speed*50;
end;
//提高方块下落速度
procedure TgmForm.UpSpeed;
begin
speed:=(speed+1)mod 10;
timer1.Interval:=500-speed*50;
end;
procedure TgmForm.imgPictureMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if OpenPictureDialog1.Execute then
begin
try
imgBK.Picture.LoadFromFile(OpenPictureDialog1.FileName);
gmForm.Refresh;
except
showmessage('Error when load picture!');
end;
end;
end;
//游戏的一些初始化设置
procedure TgmForm.InitGame;
var
i,j:integer;
begin
for i:=0 to 19 do
for j:=0 to 13 do
CellState[i,j]:=0;
speed:=0;
score:=0;
cntLine:=0;
TotalLine:=0;
GameOver:=false;
repaint;
NewBrick(nextBrick);
curBrick:=nextBrick;
ChangeBrick;
NewBrick(nextBrick);
DrawnextMatrix(Canvas,nextBrick);
Timer1.Enabled:=true;
end;
procedure TgmForm.imgNewMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if imgNew.Enabled then
imgNew.Cursor:=crHandPoint
else
imgNew.Cursor:=crDefault;
end;
//开始新游戏
procedure TgmForm.imgNewMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
InitGame;
Running:=true;
imgNew.Enabled:=false;
end;
//退出游戏
procedure TgmForm.imgExitMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
close;
end;
//显示Game Over 信息
procedure TgmForm.DrawGameOver;
begin
if GameOver and Running then
begin
Canvas.Font.Name:='Courier New';
Canvas.Font.Color:=clLime;
Canvas.Font.Style:=[fsBold];
Canvas.Font.Size:=16;
Canvas.TextOut(80,170,'Game Over!');
end;
end;
//打开设置窗口
procedure TgmForm.imgSetMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
// pause the game
Timer1.Enabled:=false;
setForm.ShowModal;
end;
//载入游戏设置
procedure TgmForm.LoadSet;
var
pathname:string;
StringList:TStringList;
begin
pathname:=ExtractFilePath(Application.ExeName);
StringList:=TStringList.Create;
try
StringList.LoadFromFile(pathname+'score.yy');
highScore:=strtoint(StringList.Strings[0]);
KeyLeft:=strtoint(StringList.Strings[1]);
KeyRight:=strtoint(StringList.Strings[2]);
KeyDown:=strtoint(StringList.Strings[3]);
KeyChange:=strtoint(StringList.Strings[4]);
KeyPause:=strtoint(StringList.Strings[5]);
KeySpeedUp:=strtoint(StringList.Strings[6]);
KeySpeedDown:=strtoint(StringList.Strings[7]);
except
highScore:=0;
end;
if SetForm.KeytoChar(KeyLeft)='' then
KeyLeft:=37;
if SetForm.KeytoChar(KeyRight)='' then
KeyRight:=39;
if SetForm.KeytoChar(KeyDown)='' then
KeyDown:=40;
if SetForm.KeytoChar(KeyChange)='' then
KeyChange:=38;
if SetForm.KeytoChar(KeyPause)='' then
KeyPause:=80;
if SetForm.KeytoChar(KeySpeedUp)='' then
KeySpeedUp:=85;
if SetForm.KeytoChar(KeySpeedDown)='' then
KeySpeedDown:=68;
StringList.Free;
end;
//保存游戏设置
procedure TgmForm.SaveSet;
var
StringList:TStringList;
pathName:string;
begin
if Score>highScore then
highScore:=score;
pathname:=ExtractFilePath(Application.ExeName);
StringList:=TStringList.Create;
try
StringList.Append(inttostr(highScore));
StringList.Append(inttostr(KeyLeft));
StringList.Append(inttostr(KeyRight));
StringList.Append(inttostr(KeyDown));
StringList.Append(inttostr(KeyChange));
StringList.Append(inttostr(KeyPause));
StringList.Append(inttostr(KeySpeedUp));
StringList.Append(inttostr(KeySpeedDown));
StringList.SaveToFile(pathname+'score.yy');
except
end;
StringList.Free;
end;
//播放声音的函数,影响游戏的流畅,所以没有使用.
procedure TgmForm.mySound(flag: byte=0);
begin
if (flag=0) then
sndPlaySound(pMove,SND_MEMORY or SND_SYNC)
else
sndPlaySound(pChange,SND_MEMORY or SND_SYNC);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -