unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TWindow = class(TForm) Logo: TLabel; Start: TLabel; Ende: TLabel; S0: TShape; S1: TShape; S2: TShape; Update: TTimer; Block: TShape; procedure FormCreate(Sender: TObject); procedure StartClick(Sender: TObject); procedure EndeClick(Sender: TObject); procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure UpdateTimer(Sender: TObject); function GetPart(i: Integer): TShape; procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private-Deklarationen } public { Public-Deklarationen } end; var Window: TWindow; Richtung: Char; SList: TList; Punkte: Integer; implementation {$R *.DFM} // Prozeduren procedure TWindow.FormCreate(Sender: TObject); begin Window.Color := clBlue; S0.Visible := false; // Kopf S1.Visible := false; // Mitte S2.Visible := false; // Schwanz SList := TList.Create; Update.Enabled := false; Punkte := 0; Block.Visible := false; Start.Visible := true; Ende.Visible := true; Logo.Visible := true; SList.Add(Pointer(S0)); SList.Add(Pointer(S1)); SList.Add(Pointer(S2)); randomize; end; procedure TWindow.StartClick(Sender: TObject); begin Start.Color := clGreen; S0.Visible := true; S1.Visible := true; S2.Visible := true; Richtung := 'u'; Update.Enabled := true; S0.Left := random(ClientWidth-20); S0.Top := random(ClientHeight-20); S1.Left := S0.Left; S1.Top := S0.Top-20; S2.Left := S1.Left; S2.Top := S1.Top-20; Window.Caption := 'Resnake || Punkte: 0'; Block.Visible := true; Block.Left := random(Clientwidth-20); Block.Top := random(ClientHeight-20); Start.Visible := false; Ende.Visible := false; Logo.Visible := false; end; procedure TWindow.EndeClick(Sender: TObject); begin Ende.Color := clGreen; close; end; procedure TWindow.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = vk_up then begin Richtung := 'u'; end else if Key = vk_down then begin Richtung := 'd'; end else if Key = vk_left then begin Richtung := 'l'; end else if Key = vk_right then begin Richtung := 'r'; end; end; procedure TWindow.UpdateTimer(Sender: TObject); var i: Integer; Item: TShape; _Item: TShape; begin // Körper bewegen For i := SList.Count-1 downto 1 do begin Item := GetPart(i); _Item := GetPart(i-1); Item.Left := _Item.Left; Item.Top := _Item.Top; end; // Kopf bewegen if Richtung = 'u' then begin S0.Top := S0.Top - 20; end else if Richtung = 'd' then begin S0.Top := S0.Top + 20; end else if Richtung = 'l' then begin S0.Left := S0.Left - 20; end else if Richtung = 'r' then begin S0.Left := S0.Left + 20; end; if S0.Left < 0 then begin S0.Left := ClientWidth - S0.Width; end else if S0.Left > ClientWidth then begin S0.Left := S0.Width; end else if S0.Top < 0 then begin S0.Top := ClientHeight-S0.Height; end else if S0.Top > ClientHeight then begin S0.Top := S0.Height; end; if (S0.Left < Block.Left+Block.Width) and (S0.Left > Block.Left-Block.Width) and (S0.Top < Block.Top+Block.Height) and (S0.Top > Block.Top-Block.Height) then begin Block.Left := random(Clientwidth-20); Block.Top := random(ClientHeight-20); _Item := GetPart(SList.Count-1); Item := TShape.Create(self); Item.Parent := Window; Item.Left := _Item.Left; Item.Top := _Item.Top; Item.Width := _Item.Width; Item.Height := _Item.Height; Item.Brush := _Item.Brush; Item.Pen := _Item.Pen; Item.Shape := _Item.Shape; Item.Show; SList.Add(Pointer(Item)); inc(Punkte); Window.Caption := 'ReSnake || Punkte: '+inttostr(punkte); end; end; // Funktionen function TWindow.GetPart(i: Integer): TShape; begin Result := TShape(SList.Items[i]); end; procedure TWindow.FormClose(Sender: TObject; var Action: TCloseAction); begin SList.Clear; SList.Free; end; end.