SHARE
TWEET

Untitled

a guest Aug 2nd, 2017 150 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type
  2.   TPoint = record
  3.     x : integer;
  4.     y : integer;
  5.   end;
  6.  
  7.   TRoom = class
  8.   public
  9.     widthR, heightR : integer;
  10.     x, y : integer;
  11.     procedure Draw(handle :TForm);
  12.     procedure SetSize(xr, yr, wr, hr :integer);
  13.     procedure GetSize(var p1, p2 :TPoint);
  14.   end;
  15.  
  16.   TEat = class
  17.   public
  18.     pos : TPoint;
  19.     procedure Draw(handle :TForm; room :TRoom);
  20.     procedure SetPos(ps :TPoint);
  21.   end;
  22.  
  23.   TSnake = class
  24.   public
  25.     n : integer;
  26.     pos : array [0..100] of TPoint;
  27.     procedure Move(infE :TPoint);
  28.     procedure Draw(handle :TForm; room :TRoom);
  29.     function IsCollision(room :TRoom) : boolean;
  30.     procedure IsEat(eat :TEat; room :TRoom);
  31.     procedure NewPos(eat :TEat; room :TRoom);
  32.     procedure Init;
  33.   end;
  34.  
  35. const
  36.   G_NULL = 0;
  37.   G_UP = 1;
  38.   G_DOWN = 2;
  39.   G_LEFT = 3;
  40.   G_RIGHT = 4;
  41.  
  42. var
  43.   snake :TSnake;
  44.   room :TRoom;
  45.   eat :TEat;
  46.  
  47.   S_UP, S_DOWN, S_LEFT, S_RIGHT :TPoint;
  48.   path :byte;
  49.   count :byte = 0;
  50.  
  51. function SetIt(x, y :integer) :TPoint;
  52. begin
  53.   Result.x := x;
  54.   Result.y := y;
  55. end;
  56.  
  57. function IsEQ(e1, e2 :TPoint) : boolean;
  58. begin
  59.   if ((e1.x = e2.x) and (e1.y = e2.y)) then Result := true
  60.   else Result := false;
  61. end;
  62.  
  63. procedure TEat.Draw(handle: TForm; room :TRoom);
  64. begin
  65.   {Drawing eat cube}
  66.   with handle.Canvas do
  67.   begin
  68.     Pen.Width := 1;
  69.     Pen.Color := clGreen;
  70.     Rectangle(room.x+pos.x-5, room.y+pos.y-5,
  71.       room.x+pos.x+5, room.y+pos.y+5);
  72.     Rectangle(room.x+pos.x+2, room.y+pos.y+2,
  73.       room.x+pos.x-2, room.y+pos.y-2);
  74.   end;
  75. end;
  76.  
  77. procedure TEat.SetPos(ps :TPoint);
  78. begin
  79.   pos := ps;
  80. end;
  81.  
  82. procedure TRoom.Draw(handle: TForm);
  83. begin
  84.   {Draw game room}
  85.   with handle.Canvas do
  86.   begin
  87.     Pen.Width := 2;
  88.     Pen.Color := clBlack;
  89.     MoveTo(x, y);
  90.     LineTo(widthR, y);
  91.     LineTo(widthR, heightR);
  92.     LineTo(x, heightR);
  93.     LineTo(x, y);
  94.   end;
  95. end;
  96.  
  97. procedure TRoom.SetSize(xr, yr, wr, hr :integer);
  98. begin
  99.   x := xr; y := yr;
  100.   widthR := wr; heightR := hr;
  101. end;
  102.  
  103. procedure TRoom.GetSize(var p1, p2 :TPoint);
  104. begin
  105.   p1 := SetIt(x, y);
  106.   p2 := SetIt(widthR, heightR);
  107. end;
  108.  
  109. procedure TSnake.Init;
  110. begin
  111.   {Initialize snake for new game}
  112.   path := G_NULL;
  113.   snake.n := 1;
  114.  
  115.   snake.pos[0] := SetIt(5, 5);
  116.   snake.Move(snake.pos[0]);
  117.  
  118.   count := 0;
  119. end;
  120.  
  121. procedure TSnake.Move(infE : TPoint);
  122. var j : integer;
  123.     nE : TPoint;
  124. begin
  125.   {Magical function for moving snake}
  126.   nE.x := pos[0].x+infE.x;
  127.   nE.y := pos[0].y+infE.y;
  128.   for j := n downto 1 do
  129.     pos[j] := pos[j-1];
  130.   pos[0] := nE;
  131. end;
  132.  
  133. procedure TSnake.Draw(handle :TForm; room :TRoom);
  134. var i : integer;
  135. begin
  136.   {Draw snake}
  137.   with handle.Canvas do
  138.   begin
  139.     Pen.Width := 1;
  140.     Pen.Color := clBlack;
  141.     for i := 0 to n-1 do
  142.     begin
  143.       Rectangle(room.x+pos[i].x-5, room.y+pos[i].y-5,
  144.         room.x+pos[i].x+5, room.y+pos[i].y+5);
  145.       Rectangle(room.x+pos[i].x+2, room.y+pos[i].y+2,
  146.         room.x+pos[i].x-2, room.y+pos[i].y-2);
  147.     end;
  148.   end;
  149. end;
  150.  
  151. function TSnake.IsCollision(room : TRoom) : boolean;
  152. var p1, p2 :TPoint;
  153. begin
  154.   {collision detection}
  155.   room.GetSize(p1, p2);
  156.   if p1.x > pos[0].x then Result := true
  157.   else if p1.y > pos[0].y then Result := true
  158.   else if p2.x < pos[0].x+10 then Result := true
  159.   else if p2.y < pos[0].y+10 then Result := true
  160.   else Result := false;
  161. end;
  162.  
  163. procedure TSnake.IsEat(eat: TEat; room :TRoom);
  164. begin
  165.   {Mmmm eat}
  166.   if IsEQ(pos[0], eat.pos) then
  167.   begin
  168.     Inc(n);
  169.     NewPos(eat, room);
  170.   end;
  171. end;
  172.  
  173. procedure TSnake.NewPos(eat: TEat; room: TRoom);
  174. var xp, yp :integer;
  175. begin
  176.   {Set new pos for a snake}
  177.   xp := Random((room.widthR-room.x) div 10 - 1) * 10 + room.x;
  178.   yp := Random((room.heightR-room.y) div 10 - 1) * 10 + room.y;
  179.   eat.SetPos(SetIt(xp, yp));
  180.   count := 0;
  181. end;
  182.  
  183. procedure TGame.timerTimer(Sender: TObject);
  184. var i :integer;
  185. begin
  186.   {Move snake}
  187.   case path of
  188.     G_UP: snake.Move(S_UP);
  189.     G_DOWN: snake.Move(S_DOWN);
  190.     G_LEFT: snake.Move(S_LEFT);
  191.     G_RIGHT: snake.Move(S_RIGHT);
  192.   end;
  193.  
  194.   if snake.IsCollision(room) then snake.Init;
  195.  
  196.   {If snake eat themself}
  197.   for i := 1 to snake.n do
  198.     if IsEQ(snake.pos[i], snake.pos[0]) then
  199.     begin
  200.       snake.Init;
  201.       break;
  202.     end;
  203.  
  204.   {End of gmae}
  205.   if snake.n = 100 then snake.Init;
  206.  
  207.   Repaint;
  208.   Inc(count);
  209.   if count = 20 then snake.NewPos(eat, room);
  210. end;
  211.  
  212. procedure TGame.FormPaint(Sender: TObject);
  213. begin
  214.   {Drawing: snake, room, eat}
  215.   snake.Draw(Game, room);
  216.   room.Draw(Game);
  217.   eat.Draw(Game, room);
  218.  
  219.   snake.IsEat(eat, room);
  220.  
  221.   Label1.Caption := 'Длина змейки: '+IntToStr(snake.n)+
  222.     Char($0A) + 'Время: '+IntToStr((20-count) div 2)+' c';
  223. end;
  224.  
  225. procedure TGame.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  226. begin
  227.   {Key to move}
  228.   case Key of
  229.     VK_UP:
  230.     if path <> G_UP then
  231.     begin
  232.       snake.Move(S_UP);
  233.       path := G_UP;
  234.     end;
  235.  
  236.     VK_DOWN:
  237.     if path <> G_DOWN then
  238.     begin
  239.       snake.Move(S_DOWN);
  240.       path := G_DOWN;
  241.     end;
  242.  
  243.     VK_LEFT:
  244.     if path <> G_LEFT then
  245.     begin
  246.       snake.Move(S_LEFT);
  247.       path := G_LEFT;
  248.     end;
  249.  
  250.     VK_RIGHT:
  251.     if path <> G_RIGHT then
  252.     begin
  253.       snake.Move(S_RIGHT);
  254.       path := G_RIGHT;
  255.     end;
  256.   end;
  257.   Repaint;
  258. end;
  259.  
  260. procedure TGame.FormCreate(Sender: TObject);
  261. begin
  262.   {First init}
  263.   Randomize;
  264.  
  265.   snake := TSnake.Create;
  266.   snake.Init;
  267.  
  268.   room := TRoom.Create;
  269.   room.SetSize(10, 10, 310, 310);
  270.  
  271.   eat := TEat.Create;
  272.   eat.SetPos(SetIt(40, 20));
  273.  
  274.   S_UP := SetIt(0, -10);
  275.   S_DOWN := SetIt(0, 10);
  276.   S_LEFT := SetIt(-10, 0);
  277.   S_RIGHT := SetIt(10, 0);
  278.  
  279.   Label1.Left := room.widthR+room.x*2;
  280.   Label1.Top := room.y;
  281. end;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top