Создаю змею из компонентов TShape. Тело змеи хранится в списке, изначально в списке 3 Shape'a. Как сделать, что бы при съедании еды, змея увеличивалась(то есть добавлялся к телу змеи еще один Shape) и уже двигалось не 3 (как изначально), а уже 4 Shape'a , ну и тд.
Вот то что я уже написал:
Код | unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, Menus;
type TForm1 = class(TForm) Timer1: TTimer; Panel1: TPanel; Image1: TImage; MainMenu1: TMainMenu; Main: TMenuItem; NewGames: TMenuItem; N1: TMenuItem; Exit: TMenuItem; Cpravka: TMenuItem; About_program: TMenuItem; procedure Timer1Timer(Sender: TObject); procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public { Public declarations } List:TList;
procedure CreateListOfShape; procedure Vpravo; procedure VLevo; procedure VVerx; procedure Vniz; end; const shag = 10;
var Form1: TForm1; var napravlenie: string[1]; a,b,n,max:integer; implementation
{$R *.dfm}
//Процедура для начального создания тела змеии, создаю вчерез список (изначально 3 Shape'a)
procedure TForm1.CreateListOfShape(); var i: Integer; TopItem: Integer; Item:TShape; begin TopItem:=10; List:= TList.Create; for i:=0 to 3 do begin Item:=TShape.Create(self); Item.Width:=10; Item.Height:=10; Item.Top:=TopItem; TopItem:=TopItem+Item.Height; Item.Parent:=Form1.Panel1; List.Add(Item); end;
//Переменные для правильного поворота змеии при движении a := 0; max := 3; b := max - 1; n := max - 1; end;
//Процедура для поворота вправо procedure TForm1.VPravo(); var Item:TShape; Item1:TShape; begin Item:= Form1.List[a]; Item1:= Form1.List[b]; Item.Top:=Item1.top; Item.Left:=Item1.Left+shag; b := a; a := a + 1; if a > n then a := 0; end;
//Процедура для поворота влево procedure TForm1.VLevo(); var Item:TShape; Item1:TShape; begin Item:= Form1.List[a]; Item1:= Form1.List[b]; Item.Top:=Item1.top; Item.Left:=Item1.Left-shag; b := a; a := a + 1; if a > n then a := 0; end;
//Процедура для поворота вверх procedure TForm1.VVerx(); var Item:TShape; Item1:TShape; begin Item:= Form1.List[a]; Item1:= Form1.List[b]; Item.Top:=Item1.top-shag; Item.Left:=Item1.Left; b := a; a := a + 1; if a > n then a := 0; end;
//Процедура для поворота вниз procedure TForm1.Vniz(); var Item:TShape; Item1:TShape; begin Item:= Form1.List[a]; Item1:= Form1.List[b]; Item.Top:=Item1.top+shag; Item.Left:=Item1.Left; b := a; a := a + 1; if a > n then a := 0; end;
procedure TForm1.Timer1Timer(Sender: TObject); var Item:TShape; begin if napravlenie = 'R' then Vpravo; if napravlenie = 'L' then VLevo; if napravlenie = 'D' then Vniz; if napravlenie = 'U' then VVerx; end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin case key of vk_RIGHT: napravlenie:='R'; VK_LEFT: napravlenie:='L'; vk_DOWN: napravlenie:='D'; VK_UP: napravlenie:='U'; end; end;
procedure TForm1.FormCreate(Sender: TObject);
begin CreateListOfShape; end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin List.Free; end;
end.
|
Помогите пожалуйста. |