unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
LCLType, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Timer1: TTimer;
Timer2: TTimer;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure Timer1Timer(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
snake: Trect;
tail: Trect;
a:integer;
b:integer;
c:integer;
d:integer;
direction:integer;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
a:=(random(24))*20;
b:=(random(24))*20 ;
c:=a+20;
d:=b+20 ;
snake.left:=a;
snake.top:=b;
snake.right:=c;
snake.bottom:=d;
tail.left:=a+40;
tail.top:=b;
tail.right:=c+40;
tail.bottom:=d;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
canvas.rectangle(snake.left, snake.top, snake.right, snake.bottom);
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
);
begin
case direction of
0:if key in [vk_left] then
begin
direction:=3;
end;
1:if key in [vk_left] then
begin
direction:=1 ;
end;
2: if key in [vk_left] then
begin
direction:=3;
end;
3: if key in [vk_left] then
begin
direction:=3;
end;
4: if key in [vk_left] then
begin
direction:=3;
end;
end;
case direction of
0:if key in [vk_right] then
begin
direction:=1;
end;
1:if key in [vk_right] then
begin
direction:=1 ;
end;
2: if key in [vk_right] then
begin
direction:=1;
end;
3: if key in [vk_right] then
begin
direction:=3;
end;
4: if key in [vk_right] then
begin
direction:=1;
end;
end;
case direction of
0:if key in [vk_up] then
begin
direction:=4;
end;
1:if key in [vk_up] then
begin
direction:=4 ;
end;
2: if key in [vk_up] then
begin
direction:=2;
end;
3: if key in [vk_up] then
begin
direction:=4;
end;
4: if key in [vk_up] then
begin
direction:=4;
end;
end;
case direction of
0:if key in [vk_down] then
begin
direction:=2;
end;
1:if key in [vk_down] then
begin
direction:=2 ;
end;
2: if key in [vk_down] then
begin
direction:=2;
end;
3: if key in [vk_down] then
begin
direction:=2;
end;
4: if key in [vk_down] then
begin
direction:=4;
end;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Canvas.Pen.Color := clBlue;
Canvas.Brush.Color:=clwhite;
canvas.rectangle(snake.left, snake.top, snake.right, snake.bottom);
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
if direction = 1 then
begin
snake.left:=snake.left+20;
snake.right:=snake.right+20;
end;
this is everything i have done but i do not know what to do next. how can i delete the rectangle that has been created before in this coding?
can you make snake game with those ideas?
Yes and No.
Yes, because the ability to draw and delete objects on the screen is the very basic skill needed