level 12
很多地方做得并不好,还有待改进。比如说GAMEOVER界面会有更多的细节加入的!
2014年04月05日 12点04分
2
level 12
//核心部分源码公开
procedure TfrmMain.FormKeyPress(Sender: TObject; var Key: char);
begin
if not sOnGame then Exit;
if Key='w' then vNowKey:=tdUp;
if Key='a' then vNowKey:=tdLeft;
if Key='s' then vNowKey:=tdDown;
if Key='d' then vNowKey:=tdRight;
end;
procedure TfrmMain.tmrMainTimer(Sender: TObject);
var
tTail:Longint;
begin
if not sOnGame then Exit;
if KeyAble[vNowKey,vLastKey] then begin
vDir:=vLastKey;
vLastKey:=vNowKey;
end;
vHead.X:=vHead.X+mvdX[vDir];
vHead.Y:=vHead.Y+mvdY[vDir];
if vHead.X>mpWidth then vHead.X:=1;
if vHead.X<1 then vHead.X:=mpWidth;
if vHead.Y>mpHeight then vHead.Y:=1;
if vHead.Y<1 then vHead.Y:=mpHeight;
if (vHead.X in [1..mpWidth])and(vHead.Y in [1..mpHeight]) then begin
if vMap[vHead.Y,vHead.X]=Nothing then
begin
vLeft:=vLeft+vLength-1;
if vLeft>mpHeight*mpWidth then vLeft:=vLeft-mpHeight*mpWidth;
vMap[vSnake[vLeft].Y,vSnake[vLeft].X]:=Nothing;
vBody[vLeft].Hide;
vLeft:=vLeft+1-vLength;
if vLeft<1 then vLeft:=vLeft+mpHeight*mpWidth;
if vBody[vLeft]<>Nil then vBody[vLeft].Picture:=frmMain.imgBody.Picture;
//tTail:=vLeft+vLength-1;
//if tTail>mpHeight*mpWidth then tTail:=tTail-mpHeight*mpWidth;
//if vBody[tTail]<>Nil then vBody[tTail].Picture:=frmMain.imgTail.Picture;
vLeft:=vLeft-1;
if vLeft<1 then vLeft:=vLeft+mpHeight*mpWidth;
vSnake[vLeft]:=vHead;
vMap[vHead.Y,vHead.X]:=SnakeBody;
NewBody(vLeft);
vBody[vLeft].Picture:=frmMain.imgHead.Picture;
end
else if vMap[vHead.Y,vHead.x]=SnakeBody then
begin
tTail:=vLeft+vLength-1;
if tTail>mpHeight*mpWidth then tTail:=tTail-mpHeight*mpWidth;
if (vSnake[tTail].X=vHead.X)and(vSnake[tTail].Y=vHead.Y) then begin
vLeft:=vLeft+vLength-1;
if vLeft>mpHeight*mpWidth then vLeft:=vLeft-mpHeight*mpWidth;
vMap[vSnake[vLeft].Y,vSnake[vLeft].X]:=Nothing;
vBody[vLeft].Hide;
vLeft:=vLeft+1-vLength;
if vLeft<1 then vLeft:=vLeft+mpHeight*mpWidth;
if vBody[vLeft]<>Nil then vBody[vLeft].Picture:=frmMain.imgBody.Picture;
//if vBody[tTail]<>Nil then vBody[tTail].Picture:=frmMain.imgTail.Picture;
vLeft:=vLeft-1;
if vLeft<1 then vLeft:=vLeft+mpHeight*mpWidth;
vSnake[vLeft]:=vHead;
vMap[vHead.Y,vHead.X]:=SnakeBody;
NewBody(vLeft);
vBody[vLeft].Picture:=frmMain.imgHead.Picture;
end else begin
GameOver;
end;
end
else if vMap[vHead.Y,vHead.X]=Food then
begin
if vBody[vLeft]<>Nil then vBody[vLeft].Picture:=frmMain.imgBody.Picture;
vLeft:=vLeft-1;
if vLeft<1 then vLeft:=vLeft+mpHeight*mpWidth;
vLength:=vLength+1;
vSnake[vLeft]:=vHead;
vMap[vHead.Y,vHead.X]:=SnakeBody;
NewBody(vLeft);
vBody[vLeft].Picture:=frmMain.imgHead.Picture;
NewFood;
end
else if vMap[vHead.Y,vHead.X]=Wall then
begin
GameOver;
end;
//Paint;
end;
end;
2014年04月06日 08点04分
7
delphi?
2014年04月06日 08点04分
回复 Fallen_Breath :Lazarus
2014年04月06日 09点04分
回复 岁月晨曦的痛 :这只是一段代码
2014年06月01日 07点06分