| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
大量收QQ微信精准粉/交友粉,非诚勿扰千里之行 始于足下
共有 1720 人关注过本帖
标题:Delphi贪吃蛇游戏的代码是什么意思?
只看楼主 加入收藏
xieyuan0553
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-11-1
  问题点数:0  回复次数:0   
Delphi贪吃蛇游戏的代码是什么意思?
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls,StdCtrls, Menus;

type
  TPosition=Record
      x,y:Byte;
  end;

const
  GlassWidth=30;
  GlassHeight=20;
  GlassTop=6;
  GlassLeft=5;
  UnitWidth=12;
  UnitHeight=12;

var
  GlassWorkSheet:Array[1..GlassWidth,1..GlassHeight] of Byte;
  OldGlassWorkSheet:Array[1..GlassWidth,1..GlassHeight] of Byte;
  HeadOfSnake,TailOfSnake:TPosition;
  Direction:Byte;

type
  TFormSnake = class(TForm)
    Timer1: TTimer;
    PopupMenu1: TPopupMenu;
    Help: TMenuItem;
    LabelHelp: TLabel;
    LabelStartGame: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure HelpClick(Sender: TObject);
    procedure FormClick(Sender: TObject);
    procedure LabelStartGameClick(Sender: TObject);
  private
    { Private declarations }
  public
    function SnakeMove:Boolean;
    function NextPosition(CurPosition:TPosition;Value:Byte):TPosition;
    procedure GameRepaint;
    procedure GenerateFood;
    procedure DrawFood(x,y:Byte);
    procedure InitailizeGame;
    procedure DrawBody(x,y:Byte);
    procedure DrawWall(x,y:Byte);
    { Public declarations }
  end;

var
  FormSnake: TFormSnake;

implementation
var
    Count:Integer;
{$R *.dfm}

procedure TFormSnake.FormCreate(Sender: TObject);
begin
    FillChar(GlassWorkSheet,SizeOf(GlassWorkSheet),0);
    FillChar(OldGlassWorkSheet,SizeOf(OldGlassWorkSheet),0);
    GlassWorkSheet[12][3]:=1;
    GlassWorkSheet[11][3]:=1;
    GlassWorkSheet[10][3]:=1;
    GlassWorkSheet[9][3]:=1;
    GlassWorkSheet[16][12]:=6;
    Move(GlassWorkSheet,OldGlassWorkSheet,SizeOf(GlassWorkSheet));
    HeadOfSnake.x:=12;
    HeadOfSnake.y:=3;
    TailOfSnake.x:=9;
    TailOfSnake.y:=3;
    Direction:=0;
    Randomize;
    LabelHelp.Caption:='↑:向上'+#13+'↓:向下'+#13+'←:向左'+#13+'→:向右'+#13+'BackSpace:回到游戏';
    LabelStartGame.Caption:='单击窗口开始'+#13+'右击查看帮助暂停';
    FormSnake.PopupMenu1.AutoPopup:=False;
end;



procedure TFormSnake.FormPaint(Sender: TObject);
var
    GlassRect:TRect;
begin
    Canvas.Brush.Color:=clWhite;
    GlassRect:=Rect(GlassLeft,GlassTop,GlassLeft+GlassWidth,GlassTop+GlassHeight*UnitHeight);
    Canvas.FillRect(GlassRect);
    InitailizeGame;
end;

function TFormSnake.SnakeMove:Boolean;
var
    HeadNext,TailNext:TPosition;
begin
    SnakeMove:=True;
    HeadNext:=NextPosition(HeadOfSnake,OldGlassWorkSheet[HeadOfSnake.x][HeadOfSnake.y]);
    TailNext:=NextPosition(TailOfSnake,OldGlassWorkSheet[TailOfSnake.x][TailOfSnake.y]);
    Case OldGlassWorkSheet[HeadNext.x][HeadNext.y] of
        0:
        begin
            GlassWorkSheet[TailOfSnake.x][TailOfSnake.y]:=0;
            TailOfSnake:=TailNext;
            GlassWorkSheet[HeadNext.x][HeadNext.y]:=GlassWorkSheet[HeadOfSnake.x][HeadOfSnake.y];
            HeadOfSnake:=HeadNext;
        end;
        1,2,3,4,5:
        begin
            SnakeMove:=False;
            Exit;
        end;
        6:
        begin
            GenerateFood;
            GlassWorkSheet[HeadNext.x][HeadNext.y]:=GlassWorkSheet[HeadOfSnake.x][HeadOfSnake.y];
            HeadOfSnake:=HeadNext;
        end;
    end;
end;

function TFormSnake.NextPosition(CurPosition:TPosition;Value:Byte):TPosition;
begin
    Case Value Of
        1:CurPosition.x:=CurPosition.x+1;
        2:CurPosition.y:=CurPosition.y+1;
        3:CurPosition.x:=CurPosition.x-1;
        4:CurPosition.y:=CurPosition.y-1;
    end;
    if CurPosition.x=GlassWidth+1 then CurPosition.x:=1;
    if CurPosition.x=0 then CurPosition.x:=GlassWidth;
    if CurPosition.y=GlassHeight+1 then CurPosition.y:=1;
    if CurPosition.y=0 then CurPosition.y:=GlassHeight;
    NextPosition.x:=CurPosition.x;
    NextPosition.y:=CurPosition.y;
end;

procedure TFormSnake.GameRepaint;
var
    i,j:Byte;
    GlassRect:TRect;
begin
    for i:=1 to GlassWidth do
        for j:=1 to GlassHeight do
        begin
            if OldGlassWorkSheet[i][j]<>GlassWorkSheet[i][j] then
            begin
                Case GlassWorkSheet[i][j] of
                    0:
                    begin
                        GlassRect:=Rect(GlassLeft+(i-1)*UnitWidth,GlassTop+(j-1)*UnitHeight,GlassLeft+i*UnitWidth,GlassTop+j*UnitHeight);
                        Canvas.Brush.Color:=clWhite;
                        Canvas.FillRect(GlassRect);
                        OldGlassWorkSheet[i][j]:=0;
                    end;
                    1,2,3,4:
                    begin
                        GlassRect:=Rect(GlassLeft+(i-1)*UnitWidth,GlassTop+(j-1)*UnitHeight,GlassLeft+i*UnitWidth,GlassTop+j*UnitHeight);
                        Canvas.Brush.Color:=clBlack;
                        Canvas.FillRect(GlassRect);
                        OldGlassWorkSheet[i][j]:=GlassWorkSheet[i][j];
                    end;
                    6:
                    DrawFood(i,j);
                end;
            end;
        end;
end;



procedure TFormSnake.Timer1Timer(Sender: TObject);
begin
    if Not SnakeMove then
    begin
        Timer1.Enabled:=False;
        Application.MessageBox(PChar('游戏结束!!!'+#13+'你的得分:'+IntToStr(Count)+'分'),'提示',MB_ICONINFORMATION+MB_OK);
        Close;
    end
    else
    begin
        GameRepaint;
        Move(GlassWorkSheet,OldGlassWorkSheet,SizeOf(GlassWorkSheet));
    end;
end;

procedure TFormSnake.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    Case Key of
        VK_UP:Direction:=4;
        VK_DOWN:Direction:=2;
        VK_LEFT:Direction:=3;
        VK_RIGHT:Direction:=1;
        VK_BACK:
        begin
            Timer1.Enabled:=True;
            LabelHelp.Visible:=False;
            FormSnake.PopupMenu1.AutoPopup:=True;
        end;
    end;
    if (OldGlassWorkSheet[HeadOfSnake.x][HeadOfSnake.y]=2) and (Direction=4) then Exit;
    if (OldGlassWorkSheet[HeadOfSnake.x][HeadOfSnake.y]=4) and (Direction=2) then Exit;
    if (OldGlassWorkSheet[HeadOfSnake.x][HeadOfSnake.y]=3) and (Direction=1) then Exit;
    if (OldGlassWorkSheet[HeadOfSnake.x][HeadOfSnake.y]=1) and (Direction=3) then Exit;
    OldGlassWorkSheet[HeadOfSnake.x][HeadOfSnake.y]:=Direction;
    Move(OldGlassWorkSheet,GlassWorkSheet,SizeOf(GlassWorkSheet));
end;

procedure TFormSnake.GenerateFood;
var
    i,j:Byte;
begin
    Repeat
        i:=Random(GlassWidth)+1;
        j:=Random(GlassHeight)+1;
    Until (OldGlassWorkSheet[i][j]=0) and (GlassWorkSheet[i][j]=0);
    GlassWorkSheet[i][j]:=6;
    Count:=Count+1;
end;

procedure TFormSnake.DrawFood(x,y:Byte);
begin
    Canvas.Pen.Color:=clBlack;
    Canvas.Pen.Width:=2;
    Canvas.MoveTo(GlassLeft+(x-1)*UnitWidth+1,GlassTop+(y-1)*UnitHeight+1);
    Canvas.LineTo(GlassLeft+(x-1)*UnitWidth+1,GlassTop+y*UnitHeight-1);
    Canvas.LineTo(GlassLeft+x*UnitWidth-1,GlassTop+y*UnitHeight-1);
    Canvas.LineTo(GlassLeft+x*UnitWidth-1,GlassTop+(y-1)*UnitHeight+1);
    Canvas.LineTo(GlassLeft+(x-1)*UnitWidth,GlassTop+(y-1)*UnitHeight+1);
end;

procedure TFormSnake.InitailizeGame;
var
    i,j:Integer;
begin
    for i:=1 to GlassWidth do
        for j:=1 to GlassHeight do
        begin
            if GlassWorkSheet[i][j]>0 then
                Case GlassWorkSheet[i][j] of
                    1,2,3,4:DrawBody(i,j);
                    5:DrawWall(i,j);
                    6:DrawFood(i,j);
                end;

        end;
end;

procedure TFormSnake.DrawBody(x,y:Byte);
var
    GlassRect:TRect;
begin
    Canvas.Brush.Color:=clBlack;
    GlassRect:=Rect(GlassLeft+(x-1)*UnitWidth,GlassTop+(y-1)*UnitHeight,GlassLeft+x*UnitWidth,GlassTop+y*UnitHeight);
    Canvas.FillRect(GlassRect);
end;

procedure TFormSnake.DrawWall(x,y:Byte);
var
    GlassRect:TRect;
begin
    Canvas.Brush.Color:=clBlack;
    GlassRect:=Rect(GlassLeft+(x-1)*UnitWidth,GlassTop+(y-1)*UnitHeight,GlassLeft+x*UnitWidth,GlassTop+y*UnitHeight);
    Canvas.FillRect(GlassRect);
end;

procedure TFormSnake.HelpClick(Sender: TObject);
begin
    Timer1.Enabled:=False;
    LabelHelp.Visible:=True;
    FormSnake.PopupMenu1.AutoPopup:=False;
end;

procedure TFormSnake.FormClick(Sender: TObject);
begin
    if LabelHelp.Visible=False then
    begin
        Timer1.Enabled:=True;
        LabelStartGame.Visible:=False;
        FormSnake.PopupMenu1.AutoPopup:=True;
    end;
end;

procedure TFormSnake.LabelStartGameClick(Sender: TObject);
begin
    Timer1.Enabled:=True;
    LabelStartGame.Visible:=False;
    FormSnake.PopupMenu1.AutoPopup:=True;
end;

end.

这是我编的“贪吃蛇”游戏,请问每句代码是什么意思?
搜索更多相关主题的帖子: Windows  贪吃蛇  游戏  
2017-04-05 10:35







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024647 second(s), 8 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved