叶
叶不归
Unregistered / Unconfirmed
GUEST, unregistred user!
窗体代码:
object MainForm: TMainForm
Left = 235
Top = 198
Width = 447
Height = 232
ActiveControl = BtnReload
BorderIcons = [biSystemMenu, biMinimize]
Caption = 'Snake 1.0 [ 2002.7.4 By Blank ]'
Color = clBtnFace
Constraints.MaxHeight = 232
Constraints.MaxWidth = 447
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnCreate = FormCreate
OnKeyDown = FormKeyDown
PixelsPerInch = 96
TextHeight = 12
object Shape1: TShape
Left = 7
Top = 7
Width = 343
Height = 190
Brush.Style = bsClear
end
object Label1: TLabel
Left = 361
Top = 76
Width = 30
Height = 12
Caption = '速度:'
end
object SG: TStringGrid
Left = 8
Top = 8
Width = 340
Height = 188
BorderStyle = bsNone
ColCount = 20
DefaultColWidth = 16
DefaultRowHeight = 16
DefaultDrawing = False
Enabled = False
FixedCols = 0
RowCount = 11
FixedRows = 0
Options = [goFixedVertLine, goFixedHorzLine]
TabOrder = 0
OnDrawCell = SGDrawCell
end
object BtnPause: TBitBtn
Left = 360
Top = 40
Width = 75
Height = 25
Caption = '暂停'
TabOrder = 1
OnClick = BtnPauseClick
end
object BtnReload: TBitBtn
Left = 360
Top = 8
Width = 75
Height = 25
Caption = '开始'
TabOrder = 2
OnClick = BtnReloadClick
end
object ComSpeed: TComboBox
Left = 394
Top = 72
Width = 41
Height = 20
Style = csDropDownList
DropDownCount = 9
ItemHeight = 12
TabOrder = 3
OnChange = ComSpeedChange
Items.Strings = (
'1'
'2'
'3'
'4'
'5'
'6'
'7'
'8'
'9')
end
object Memo1: TMemo
Left = 360
Top = 131
Width = 73
Height = 65
BorderStyle = bsNone
Color = clInactiveBorder
Lines.Strings = (
'箭头: 控制'
''
'回车: 开始'
' 暂停')
ReadOnly = True
TabOrder = 4
end
object Timer1: TTimer
Enabled = False
Interval = 500
OnTimer = Timer1Timer
Left = 152
Top = 72
end
end
单元代码:
unit SnakeFrm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, ExtCtrls, Buttons;
type
TMainForm = class(TForm)
SG: TStringGrid;
Timer1: TTimer;
Shape1: TShape;
Label1: TLabel;
BtnPause: TBitBtn;
BtnReload: TBitBtn;
ComSpeed: TComboBox;
Memo1: TMemo;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SGDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
State: TGridDrawState);
procedure BtnReloadClick(Sender: TObject);
procedure BtnPauseClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ComSpeedChange(Sender: TObject);
private
{ ToDo: Main: ### Private }
procedure InitSnake;
procedure StartSnake;
procedure StopSnake;
procedure DoGameOver;
procedure ClearOld;
procedure DrawNew;
procedure GetNew;
procedure SetRandomPoint;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
type
TDirectionType = (dtLeft, dtRight, dtUp, dtDown);
type
TSigle = Record { 蛇(Snake)由 n 个 Sigle 组成 }
Dt: TDirectionType; { Direction }
Ps: TPoint; { Position }
end;
const
InitPosX = 8; { 初始位置、长度和方向 }
InitPosY = 5;
InitLeng = 6;
InitDir = dtRight;
var
Snake: array of TSigle; { 蛇体 }
Tail: TSigle; { 蛇尾 }
GameOver: Boolean; { 结束标志 }
Food: TPoint; { 食物坐标 }
{ 游戏结束 }
procedure TMainForm.DoGameOver;
begin
GameOver := True;
StopSnake;
end;
{ ToDo: Main: GetNew }
{ 获取蛇的下一个位置 }
procedure TMainForm.GetNew;
var
I: Integer;
begin
{ 如果蛇已越界,结束 }
if (Snake[0].Ps.X < 0) or (Snake[0].Ps.X >= Sg.ColCount) or
(Snake[0].Ps.Y < 0) or (Snake[0].Ps.Y >= Sg.RowCount) then
begin
DoGameOver;
Exit;
end;
{ 蛇尾 }
Tail := Snake[High(Snake)];
{ 如果吃到了食物 }
if (Snake[0].Ps.X = Food.X) and (Snake[0].Ps.Y = Food.Y) then
begin
SetLength(Snake, High(Snake) + 2);
case Tail.Dt of
dtLeft: Inc(Tail.Ps.X);
dtRight: Dec(Tail.Ps.X);
dtUp: Inc(Tail.Ps.Y);
dtDown: Dec(Tail.Ps.Y);
end;
SetRandomPoint;
end;
{ 蛇体新的位置 }
for I := High(Snake) downto 1 do
Snake := Snake[I - 1];
{ 蛇头的位置 }
case Snake[0].Dt of
dtLeft: Dec(Snake[0].Ps.X);
dtRight: Inc(Snake[0].Ps.X);
dtUp: Dec(Snake[0].Ps.Y);
dtDown: Inc(Snake[0].Ps.Y);
end;
{ 如果撞到了自己的身体 }
for I := 1 to High(Snake) do
if (Snake[0].Ps.X = Snake.Ps.X) and (Snake[0].Ps.Y = Snake.Ps.Y) then
DoGameOver;
end;
{ ToDo: Main: DrawNew }
procedure TMainForm.DrawNew;
begin
{ 画出蛇体 }
with Sg.Canvas do
begin
Brush.Color := clWhite;
Brush.Style := bsSolid;
FillRect(SG.CellRect(Tail.Ps.X, Tail.Ps.Y));
Brush.Color := clNavy;
FillRect(SG.CellRect(Snake[0].Ps.X, Snake[0].Ps.Y));
end;
end;
{ ToDo: Main: Timer }
procedure TMainForm.Timer1Timer(Sender: TObject);
begin
GetNew;
DrawNew;
end;
{ ToDo: Main: ### Create }
procedure TMainForm.FormCreate(Sender: TObject);
begin
ComSpeed.ItemIndex := 8; { 速度 }
InitSnake;
GameOver := True;
Food := Point(-1, -1);
end;
procedure TMainForm.SGDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
I: Integer;
begin
{ 窗体重画 }
for I := 0 to High(Snake) do
if (ACol = Snake.Ps.X) and (ARow = Snake.Ps.Y) then
with Sg.Canvas do
begin
Brush.Color := clNavy;
Brush.Style := bsSolid;
FillRect(Rect);
Break;
end;
if (ACol = Food.X) and (ARow = Food.Y) then
Sg.Canvas.Ellipse(Rect);
end;
{ TODO : Main: StartSnake }
{ 继续 }
procedure TMainForm.StartSnake;
begin
ActiveControl := nil;
GameOver := False;
Timer1.Enabled := True;
SetRandomPoint;
end;
{ TODO : Main: SetRandomPoint }
{ 随机出现食物 }
procedure TMainForm.SetRandomPoint;
var
P: TPoint;
I: Integer;
IsExist: Boolean;
begin
Randomize;
IsExist := True;
while IsExist do
begin
IsExist := False;
P.X := Random(Sg.ColCount);
P.Y := Random(Sg.RowCount);
for I := 0 to High(Snake) do
if (P.X = Snake.Ps.X) and (P.Y = Snake.Ps.Y) then
begin
IsExist := True;
Break;
end;
end;
{ 画出食物 }
Food := P;
Sg.Canvas.Brush.Color := clNavy;
Sg.Canvas.Brush.Style := bsSolid;
Sg.Canvas.Ellipse(SG.CellRect(Food.X, Food.Y));
end;
{ TODO : Main: StopSnake }
procedure TMainForm.StopSnake;
begin
Timer1.Enabled := False;
end;
{ 重新开始 }
procedure TMainForm.BtnReloadClick(Sender: TObject);
begin
ClearOld;
InitSnake;
StartSnake;
end;
{ TODO : Main: ClearOld }
{ 清除旧图像 }
procedure TMainForm.ClearOld;
var
I: Integer;
begin
Sg.Canvas.Brush.Color := clWhite;
Sg.Canvas.Brush.Style := bsSolid;
for I := 0 to High(Snake) do
Sg.Canvas.FillRect(SG.CellRect(Snake.Ps.X, Snake.Ps.Y));
Sg.Canvas.FillRect(SG.CellRect(Food.X, Food.Y));
end;
{ TODO : Main: InitSnake }
{ 初始化蛇体 }
procedure TMainForm.InitSnake;
var
I: Integer;
begin
Sg.Canvas.Brush.Color := clNavy;
SetLength(Snake, InitLeng);
for I := 0 to InitLeng - 1 do
begin
Snake.Dt := dtRight;
Snake.Ps.X := InitPosX - I;
Snake.Ps.Y := InitPosY;
Sg.Canvas.FillRect(SG.CellRect(Snake.Ps.X, Snake.Ps.Y));
end;
Timer1.Interval := 1000 - StrToInt(ComSpeed.Text) * 100;
end;
procedure TMainForm.BtnPauseClick(Sender: TObject);
begin
StopSnake;
end;
{ 控制蛇体 }
procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
case Key of
VK_Left:
if (Snake[0].Dt <> dtLeft) and (Snake[0].Dt <> dtRight) and (Snake[1].Dt <> dtRight) then
Snake[0].Dt := dtLeft;
VK_Right:
if (Snake[0].Dt <> dtLeft) and (Snake[0].Dt <> dtRight) and (Snake[1].Dt <> dtLeft) then
Snake[0].Dt := dtRight;
VK_Up:
if (Snake[0].Dt <> dtUp) and (Snake[0].Dt <> dtDown) and (Snake[1].Dt <> dtDown) then
Snake[0].Dt := dtUp;
VK_Down:
if (Snake[0].Dt <> dtUp) and (Snake[0].Dt <> dtDown) and (Snake[1].Dt <> dtUp) then
Snake[0].Dt := dtDown;
VK_Return: if GameOver then BtnReloadClick(Self) else Timer1.Enabled := not Timer1.Enabled;
end;
end;
{ 设置速度 }
procedure TMainForm.ComSpeedChange(Sender: TObject);
begin
Timer1.Interval := 1000 - StrToInt(ComSpeed.Text) * 100;
end;
end.
object MainForm: TMainForm
Left = 235
Top = 198
Width = 447
Height = 232
ActiveControl = BtnReload
BorderIcons = [biSystemMenu, biMinimize]
Caption = 'Snake 1.0 [ 2002.7.4 By Blank ]'
Color = clBtnFace
Constraints.MaxHeight = 232
Constraints.MaxWidth = 447
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnCreate = FormCreate
OnKeyDown = FormKeyDown
PixelsPerInch = 96
TextHeight = 12
object Shape1: TShape
Left = 7
Top = 7
Width = 343
Height = 190
Brush.Style = bsClear
end
object Label1: TLabel
Left = 361
Top = 76
Width = 30
Height = 12
Caption = '速度:'
end
object SG: TStringGrid
Left = 8
Top = 8
Width = 340
Height = 188
BorderStyle = bsNone
ColCount = 20
DefaultColWidth = 16
DefaultRowHeight = 16
DefaultDrawing = False
Enabled = False
FixedCols = 0
RowCount = 11
FixedRows = 0
Options = [goFixedVertLine, goFixedHorzLine]
TabOrder = 0
OnDrawCell = SGDrawCell
end
object BtnPause: TBitBtn
Left = 360
Top = 40
Width = 75
Height = 25
Caption = '暂停'
TabOrder = 1
OnClick = BtnPauseClick
end
object BtnReload: TBitBtn
Left = 360
Top = 8
Width = 75
Height = 25
Caption = '开始'
TabOrder = 2
OnClick = BtnReloadClick
end
object ComSpeed: TComboBox
Left = 394
Top = 72
Width = 41
Height = 20
Style = csDropDownList
DropDownCount = 9
ItemHeight = 12
TabOrder = 3
OnChange = ComSpeedChange
Items.Strings = (
'1'
'2'
'3'
'4'
'5'
'6'
'7'
'8'
'9')
end
object Memo1: TMemo
Left = 360
Top = 131
Width = 73
Height = 65
BorderStyle = bsNone
Color = clInactiveBorder
Lines.Strings = (
'箭头: 控制'
''
'回车: 开始'
' 暂停')
ReadOnly = True
TabOrder = 4
end
object Timer1: TTimer
Enabled = False
Interval = 500
OnTimer = Timer1Timer
Left = 152
Top = 72
end
end
单元代码:
unit SnakeFrm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, ExtCtrls, Buttons;
type
TMainForm = class(TForm)
SG: TStringGrid;
Timer1: TTimer;
Shape1: TShape;
Label1: TLabel;
BtnPause: TBitBtn;
BtnReload: TBitBtn;
ComSpeed: TComboBox;
Memo1: TMemo;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SGDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
State: TGridDrawState);
procedure BtnReloadClick(Sender: TObject);
procedure BtnPauseClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ComSpeedChange(Sender: TObject);
private
{ ToDo: Main: ### Private }
procedure InitSnake;
procedure StartSnake;
procedure StopSnake;
procedure DoGameOver;
procedure ClearOld;
procedure DrawNew;
procedure GetNew;
procedure SetRandomPoint;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
type
TDirectionType = (dtLeft, dtRight, dtUp, dtDown);
type
TSigle = Record { 蛇(Snake)由 n 个 Sigle 组成 }
Dt: TDirectionType; { Direction }
Ps: TPoint; { Position }
end;
const
InitPosX = 8; { 初始位置、长度和方向 }
InitPosY = 5;
InitLeng = 6;
InitDir = dtRight;
var
Snake: array of TSigle; { 蛇体 }
Tail: TSigle; { 蛇尾 }
GameOver: Boolean; { 结束标志 }
Food: TPoint; { 食物坐标 }
{ 游戏结束 }
procedure TMainForm.DoGameOver;
begin
GameOver := True;
StopSnake;
end;
{ ToDo: Main: GetNew }
{ 获取蛇的下一个位置 }
procedure TMainForm.GetNew;
var
I: Integer;
begin
{ 如果蛇已越界,结束 }
if (Snake[0].Ps.X < 0) or (Snake[0].Ps.X >= Sg.ColCount) or
(Snake[0].Ps.Y < 0) or (Snake[0].Ps.Y >= Sg.RowCount) then
begin
DoGameOver;
Exit;
end;
{ 蛇尾 }
Tail := Snake[High(Snake)];
{ 如果吃到了食物 }
if (Snake[0].Ps.X = Food.X) and (Snake[0].Ps.Y = Food.Y) then
begin
SetLength(Snake, High(Snake) + 2);
case Tail.Dt of
dtLeft: Inc(Tail.Ps.X);
dtRight: Dec(Tail.Ps.X);
dtUp: Inc(Tail.Ps.Y);
dtDown: Dec(Tail.Ps.Y);
end;
SetRandomPoint;
end;
{ 蛇体新的位置 }
for I := High(Snake) downto 1 do
Snake := Snake[I - 1];
{ 蛇头的位置 }
case Snake[0].Dt of
dtLeft: Dec(Snake[0].Ps.X);
dtRight: Inc(Snake[0].Ps.X);
dtUp: Dec(Snake[0].Ps.Y);
dtDown: Inc(Snake[0].Ps.Y);
end;
{ 如果撞到了自己的身体 }
for I := 1 to High(Snake) do
if (Snake[0].Ps.X = Snake.Ps.X) and (Snake[0].Ps.Y = Snake.Ps.Y) then
DoGameOver;
end;
{ ToDo: Main: DrawNew }
procedure TMainForm.DrawNew;
begin
{ 画出蛇体 }
with Sg.Canvas do
begin
Brush.Color := clWhite;
Brush.Style := bsSolid;
FillRect(SG.CellRect(Tail.Ps.X, Tail.Ps.Y));
Brush.Color := clNavy;
FillRect(SG.CellRect(Snake[0].Ps.X, Snake[0].Ps.Y));
end;
end;
{ ToDo: Main: Timer }
procedure TMainForm.Timer1Timer(Sender: TObject);
begin
GetNew;
DrawNew;
end;
{ ToDo: Main: ### Create }
procedure TMainForm.FormCreate(Sender: TObject);
begin
ComSpeed.ItemIndex := 8; { 速度 }
InitSnake;
GameOver := True;
Food := Point(-1, -1);
end;
procedure TMainForm.SGDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
I: Integer;
begin
{ 窗体重画 }
for I := 0 to High(Snake) do
if (ACol = Snake.Ps.X) and (ARow = Snake.Ps.Y) then
with Sg.Canvas do
begin
Brush.Color := clNavy;
Brush.Style := bsSolid;
FillRect(Rect);
Break;
end;
if (ACol = Food.X) and (ARow = Food.Y) then
Sg.Canvas.Ellipse(Rect);
end;
{ TODO : Main: StartSnake }
{ 继续 }
procedure TMainForm.StartSnake;
begin
ActiveControl := nil;
GameOver := False;
Timer1.Enabled := True;
SetRandomPoint;
end;
{ TODO : Main: SetRandomPoint }
{ 随机出现食物 }
procedure TMainForm.SetRandomPoint;
var
P: TPoint;
I: Integer;
IsExist: Boolean;
begin
Randomize;
IsExist := True;
while IsExist do
begin
IsExist := False;
P.X := Random(Sg.ColCount);
P.Y := Random(Sg.RowCount);
for I := 0 to High(Snake) do
if (P.X = Snake.Ps.X) and (P.Y = Snake.Ps.Y) then
begin
IsExist := True;
Break;
end;
end;
{ 画出食物 }
Food := P;
Sg.Canvas.Brush.Color := clNavy;
Sg.Canvas.Brush.Style := bsSolid;
Sg.Canvas.Ellipse(SG.CellRect(Food.X, Food.Y));
end;
{ TODO : Main: StopSnake }
procedure TMainForm.StopSnake;
begin
Timer1.Enabled := False;
end;
{ 重新开始 }
procedure TMainForm.BtnReloadClick(Sender: TObject);
begin
ClearOld;
InitSnake;
StartSnake;
end;
{ TODO : Main: ClearOld }
{ 清除旧图像 }
procedure TMainForm.ClearOld;
var
I: Integer;
begin
Sg.Canvas.Brush.Color := clWhite;
Sg.Canvas.Brush.Style := bsSolid;
for I := 0 to High(Snake) do
Sg.Canvas.FillRect(SG.CellRect(Snake.Ps.X, Snake.Ps.Y));
Sg.Canvas.FillRect(SG.CellRect(Food.X, Food.Y));
end;
{ TODO : Main: InitSnake }
{ 初始化蛇体 }
procedure TMainForm.InitSnake;
var
I: Integer;
begin
Sg.Canvas.Brush.Color := clNavy;
SetLength(Snake, InitLeng);
for I := 0 to InitLeng - 1 do
begin
Snake.Dt := dtRight;
Snake.Ps.X := InitPosX - I;
Snake.Ps.Y := InitPosY;
Sg.Canvas.FillRect(SG.CellRect(Snake.Ps.X, Snake.Ps.Y));
end;
Timer1.Interval := 1000 - StrToInt(ComSpeed.Text) * 100;
end;
procedure TMainForm.BtnPauseClick(Sender: TObject);
begin
StopSnake;
end;
{ 控制蛇体 }
procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
case Key of
VK_Left:
if (Snake[0].Dt <> dtLeft) and (Snake[0].Dt <> dtRight) and (Snake[1].Dt <> dtRight) then
Snake[0].Dt := dtLeft;
VK_Right:
if (Snake[0].Dt <> dtLeft) and (Snake[0].Dt <> dtRight) and (Snake[1].Dt <> dtLeft) then
Snake[0].Dt := dtRight;
VK_Up:
if (Snake[0].Dt <> dtUp) and (Snake[0].Dt <> dtDown) and (Snake[1].Dt <> dtDown) then
Snake[0].Dt := dtUp;
VK_Down:
if (Snake[0].Dt <> dtUp) and (Snake[0].Dt <> dtDown) and (Snake[1].Dt <> dtUp) then
Snake[0].Dt := dtDown;
VK_Return: if GameOver then BtnReloadClick(Self) else Timer1.Enabled := not Timer1.Enabled;
end;
end;
{ 设置速度 }
procedure TMainForm.ComSpeedChange(Sender: TObject);
begin
Timer1.Interval := 1000 - StrToInt(ComSpeed.Text) * 100;
end;
end.