uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
snaketimer: TTimer;
procedure snaketimerTimer(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure FormPaint(Sender: TObject);
PRIVATE
PUBLIC
end;
var
Form1: TForm1;
score : Integer;
procedure ResetGame;
function HitWall : Boolean;
function HitFood : Boolean;
procedure DrawSnake;
procedure PlaceFood;
procedure DrawPlayfield;
procedure EndGame;
implementation
const
snakewidth = 10;
snakegrow : Integer = 2;
snakecolor : tcolor = clBlack;
foodcolor : tcolor = clRed;
pfcolor : tcolor = clWhite;
pfdimention : tpoint = (x:200; y:200);
pfposition : tpoint = (x:5; y:5);
dup = 0;
ddown = 1;
dleft = 2;
dright = 3;
dnone = 4;
speed : Integer = 100;
var
snake : array of tpoint;
snakelength : Integer;
food : tpoint;
direction : dup..dnone;
olddirection : dup..dnone;
function HitWall : Boolean;
var
i : Integer;
begin
for i := 0 to snakelength - 1 do
begin
if (snake[i].x < pfposition.X) Or
(snake[i].x + snakewidth > pfposition.X + pfdimention.X) Or
(snake[i].y < pfposition.y) Or
(snake[i].y + snakewidth > pfposition.y + pfdimention.y) then
begin
hitwall := True;
exit;
end;
end;
hitwall := False;
end;
function HitFood : Boolean;
var
i : Integer;
begin
for i := 0 to snakelength - 1 do
begin
if (snake[i].x = food.x) And (snake[i].y = food.y) then
begin
hitfood := True;
exit;
end;
end;
hitfood := False;
end;
procedure PlaceFood;
begin
food.x := random(pfdimention.x - snakewidth - pfposition.x) + pfposition.x;
food.y := random(pfdimention.y - snakewidth - pfposition.y) + pfposition.y;
while (pfdimention.x - (food.x - pfposition.x)) Mod snakewidth <> 0 do
begin
inc(food.x)
end;
while (pfdimention.y - (food.y - pfposition.y)) Mod snakewidth <> 0 do
begin
inc(food.y)
end;
if HitFood then
begin
PlaceFood
end;
Form1.Canvas.Brush.Color := foodcolor;
Form1.Canvas.Pen.Color := snakecolor;
Form1.Canvas.Ellipse(food.x + 1, food.y + 1,
food.x + snakewidth - 2, food.y + snakewidth - 2);
end;