Advertisement
Guest User

ReSnake | © 2012 by pwnfl4sh

a guest
May 5th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.75 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TWindow = class(TForm)
  11.     Logo: TLabel;
  12.     Start: TLabel;
  13.     Ende: TLabel;
  14.     S0: TShape;
  15.     S1: TShape;
  16.     S2: TShape;
  17.     Update: TTimer;
  18.     Block: TShape;
  19.  
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure StartClick(Sender: TObject);
  22.     procedure EndeClick(Sender: TObject);
  23.     procedure FormKeyDown(Sender: TObject; var Key: Word;
  24.       Shift: TShiftState);
  25.     procedure UpdateTimer(Sender: TObject);
  26.  
  27.     function GetPart(i: Integer): TShape;
  28.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  29.   private
  30.     { Private-Deklarationen }
  31.   public
  32.     { Public-Deklarationen }
  33.   end;
  34.  
  35. var
  36.   Window: TWindow;
  37.   Richtung: Char;
  38.   SList: TList;
  39.   Punkte: Integer;
  40.  
  41. implementation
  42.  
  43. {$R *.DFM}
  44.  
  45. // Prozeduren
  46.  
  47. procedure TWindow.FormCreate(Sender: TObject);
  48. begin
  49.         Window.Color := clBlue;
  50.         S0.Visible := false; // Kopf
  51.         S1.Visible := false; // Mitte
  52.         S2.Visible := false; // Schwanz
  53.         SList := TList.Create;
  54.         Update.Enabled := false;
  55.         Punkte := 0;
  56.         Block.Visible := false;
  57.         Start.Visible := true;
  58.         Ende.Visible := true;
  59.         Logo.Visible := true;
  60.         SList.Add(Pointer(S0));
  61.         SList.Add(Pointer(S1));
  62.         SList.Add(Pointer(S2));
  63.         randomize;
  64. end;
  65.  
  66. procedure TWindow.StartClick(Sender: TObject);
  67. begin
  68.      Start.Color := clGreen;
  69.      S0.Visible := true;
  70.      S1.Visible := true;
  71.      S2.Visible := true;
  72.      Richtung := 'u';
  73.      Update.Enabled := true;
  74.      S0.Left := random(ClientWidth-20);
  75.      S0.Top := random(ClientHeight-20);
  76.      S1.Left := S0.Left;
  77.      S1.Top := S0.Top-20;
  78.      S2.Left := S1.Left;
  79.      S2.Top := S1.Top-20;
  80.      Window.Caption := 'Resnake || Punkte: 0';
  81.      Block.Visible := true;
  82.      Block.Left := random(Clientwidth-20);
  83.      Block.Top := random(ClientHeight-20);
  84.      Start.Visible := false;
  85.      Ende.Visible := false;
  86.      Logo.Visible := false;
  87. end;
  88.  
  89. procedure TWindow.EndeClick(Sender: TObject);
  90. begin
  91.      Ende.Color := clGreen;
  92.      close;
  93. end;
  94.  
  95. procedure TWindow.FormKeyDown(Sender: TObject; var Key: Word;
  96.   Shift: TShiftState);
  97. begin
  98.      if Key = vk_up then
  99.      begin
  100.           Richtung := 'u';
  101.      end
  102.      else if Key = vk_down then
  103.      begin
  104.           Richtung := 'd';
  105.      end
  106.      else if Key = vk_left then
  107.      begin
  108.           Richtung := 'l';
  109.      end
  110.      else if Key = vk_right then
  111.      begin
  112.           Richtung := 'r';
  113.      end;
  114. end;
  115.  
  116. procedure TWindow.UpdateTimer(Sender: TObject);
  117. var
  118.    i: Integer;
  119.    Item: TShape;
  120.    _Item: TShape;
  121. begin
  122.      // Körper bewegen
  123.  
  124.      For i := SList.Count-1 downto 1 do
  125.      begin
  126.           Item := GetPart(i);
  127.           _Item := GetPart(i-1);
  128.           Item.Left := _Item.Left;
  129.           Item.Top := _Item.Top;
  130.      end;
  131.  
  132.      // Kopf bewegen
  133.      if Richtung = 'u' then
  134.      begin
  135.           S0.Top := S0.Top - 20;
  136.      end
  137.      else if Richtung = 'd' then
  138.      begin
  139.           S0.Top := S0.Top + 20;
  140.      end
  141.      else if Richtung = 'l' then
  142.      begin
  143.           S0.Left := S0.Left - 20;
  144.      end
  145.      else if Richtung = 'r' then
  146.      begin
  147.           S0.Left := S0.Left + 20;
  148.      end;
  149.  
  150.      if S0.Left < 0 then
  151.      begin
  152.           S0.Left := ClientWidth - S0.Width;
  153.      end
  154.      else if S0.Left > ClientWidth then
  155.      begin
  156.           S0.Left := S0.Width;
  157.      end
  158.      else if S0.Top < 0 then
  159.      begin
  160.           S0.Top := ClientHeight-S0.Height;
  161.      end
  162.      else if S0.Top > ClientHeight then
  163.      begin
  164.           S0.Top := S0.Height;
  165.      end;
  166.  
  167.      if (S0.Left < Block.Left+Block.Width) and
  168.      (S0.Left > Block.Left-Block.Width) and
  169.      (S0.Top < Block.Top+Block.Height) and
  170.      (S0.Top > Block.Top-Block.Height) then
  171.      begin
  172.           Block.Left := random(Clientwidth-20);
  173.           Block.Top := random(ClientHeight-20);
  174.           _Item := GetPart(SList.Count-1);
  175.           Item := TShape.Create(self);
  176.           Item.Parent := Window;
  177.           Item.Left := _Item.Left;
  178.           Item.Top := _Item.Top;
  179.           Item.Width := _Item.Width;
  180.           Item.Height := _Item.Height;
  181.           Item.Brush := _Item.Brush;
  182.           Item.Pen := _Item.Pen;
  183.           Item.Shape := _Item.Shape;
  184.           Item.Show;
  185.           SList.Add(Pointer(Item));
  186.           inc(Punkte);
  187.           Window.Caption := 'ReSnake || Punkte: '+inttostr(punkte);
  188.      end;
  189. end;
  190.  
  191. // Funktionen
  192.  
  193. function TWindow.GetPart(i: Integer): TShape;
  194. begin
  195.      Result := TShape(SList.Items[i]);
  196. end;
  197.  
  198. procedure TWindow.FormClose(Sender: TObject; var Action: TCloseAction);
  199. begin
  200.      SList.Clear;
  201.      SList.Free;
  202. end;
  203.  
  204. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement