SHARE
TWEET

Untitled

a guest Sep 13th, 2017 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unit GameUtilityUnit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Graphics;
  10.  
  11. const SQUARE = 32;
  12. const STARTING_LEVEL = 1;
  13. const MAXLEVEL = 5;
  14. const IMAGE_DIR = 'images//';
  15. const SOUND_DIR = 'sounds//';
  16. const MAP_DIR = 'maps//';
  17. const HIGHSCORE_FILE = 'score.txt';
  18. const AMBUSHER_SQR = 5.0;
  19. const KILLING_TIME = 10*1000;
  20. const FPS = 50;
  21. const STARTING_LIFES = 5;
  22. const SCORE_DISPLAY_TIME = 5000;
  23. const FRAME_WIDTH = 672;
  24. const FRAME_HEIGHT = 800;
  25.  
  26. type
  27.   TDirection = (UP, LEFT ,DOWN ,RIGHT ,NONE);
  28.   TGameMode = (MMENU, MGAME, MOPTIONS, MHIGHSCORE, MSTATIC);
  29.   TLocation = record
  30.     X, Y :Real;
  31.   end;
  32.   TGameElement = (Nothing, Ball, Food, SPAWN, UL, UR, DL, DR, RL, UD, URL, URD, ULD, RLD, W, A, S, D ,X, O);
  33.   TGameNetwork = array of array of TGameElement;
  34.   TGameElementImages = array[TGameElement] of TBitMap;
  35.   TPlayerImages = array[TDirection] of TBitMap;
  36.   TASpot = record
  37.     Food :Integer;
  38.     SqrXY :TLocation;
  39.   end;
  40.  TMove = record
  41.    Direction :TDirection;
  42.    Destination :TLocation;
  43.  end;
  44.  
  45.  TASpots = array of TASpot;
  46.  TMoves = array of TMove;
  47.  
  48. function Distance(Location1, Location2 :TLocation): Real;
  49. function ConvertToLocation(X, Y :Integer): TLocation;
  50. function Sort(Locs :TMoves; Spot :TLocation): TMoves;
  51. function PseudoMove(Loc :TLocation; Dir :TDirection): TLocation;
  52. function Intersects(Loc1, Loc2 :TLocation): Boolean;
  53. function Invert(Dir :TDirection): TDirection;
  54. function GetPointForAmbusher(Map :TGameNetwork): TLocation;
  55. function GetHighScore: Integer;
  56. procedure WriteHighScore(Score :Integer);
  57.  
  58. operator = (Loc1, Loc2 : TLocation) b : Boolean;
  59.  
  60. implementation
  61.  
  62. function GetHighScore: Integer;
  63. var T :TextFile;
  64. begin
  65.   try
  66.     AssignFile(T, HIGHSCORE_FILE);
  67.     Reset(T);
  68.     Read(T, Result);
  69.     CloseFile(T);
  70.   except on E : EInOutError do Result := 0; end;
  71. end;
  72.  
  73. procedure WriteHighScore(Score: Integer);
  74. var T :TextFile;
  75. begin
  76.   AssignFile(T, HIGHSCORE_FILE);
  77.   Rewrite(T);
  78.   Write(T, Score);
  79.   CloseFile(T);
  80. end;
  81.  
  82. function Distance(Location1, Location2 :TLocation): Real;
  83. begin
  84.   Result := Sqrt(Sqr(Location1.X - Location2.X) + Sqr(Location1.Y - Location2.Y));
  85. end;
  86.  
  87. function ConvertToLocation(X,Y :Integer): TLocation;
  88. begin
  89.   Result.X := X;
  90.   Result.Y := Y;
  91. end;
  92.  
  93. function ConvertToLocation(X,Y :Real): TLocation;
  94. begin
  95. Result.X := X;
  96. Result.Y := Y;
  97. end;
  98.  
  99. function SortAndFind(Locs :TASpots): TASpot;
  100. var Swapped :Boolean;
  101.     I :Integer;
  102.     Temp :TASpot;
  103. begin
  104.   Swapped := True;
  105.   while Swapped do
  106.   begin
  107.     Swapped := False;
  108.     For I := 0 to Length(Locs) - 2 do
  109.     begin
  110.       if Locs[I].Food>Locs[I + 1].Food then
  111.       begin
  112.         Temp := Locs[I + 1];
  113.         Locs[I + 1] := Locs[I];
  114.         Locs[I] := Temp;
  115.         Swapped := True;
  116.       end;
  117.     end;
  118.   end;
  119.   Result := Locs[0];
  120. end;
  121.  
  122.  
  123. function Sort(Locs :TMoves; Spot :TLocation):TMoves;
  124. var Swapped :Boolean;
  125.     I :Integer;
  126.     Temp :TMove;
  127. begin
  128.   Swapped := True;
  129.   while Swapped do
  130.   begin
  131.     Swapped := False;
  132.     For I := 0 to Length(Locs) - 2 do
  133.     begin
  134.       if Distance(Locs[I].Destination,Spot)>Distance(Locs[I+1].Destination,Spot) then
  135.       begin
  136.         Temp := Locs[I + 1];
  137.         Locs[I + 1] := Locs[I];
  138.         Locs[I] := Temp;
  139.         Swapped := True;
  140.       end;
  141.     end;
  142.   end;
  143.   Result := Locs;
  144. end;
  145.  
  146.  
  147. operator = (Loc1, Loc2 : TLocation) b : Boolean;
  148. begin
  149.   Result := (Loc1.X = Loc2.X) and (Loc1.Y = Loc2.Y);
  150. end;
  151.  
  152. function PseudoMove(Loc :TLocation; Dir :TDirection): TLocation;
  153. begin
  154.   case Dir of
  155.     LEFT: Result.X := Loc.X - SQUARE;
  156.     RIGHT: Result.X := Loc.X + SQUARE;
  157.     UP: Result.Y := Loc.Y - SQUARE;
  158.     DOWN: Result.Y := Loc.Y + SQUARE;
  159.   end;
  160. end;
  161.  
  162. function Intersects(Loc1, Loc2:TLocation): Boolean;
  163. begin
  164.   Result := False;
  165.   if (Abs(Loc1.X - Loc2.X) <= SQUARE) and (Abs(Loc1.Y - Loc2.Y) <= SQUARE) then Result := True;
  166. end;
  167.  
  168.  
  169. function Invert(Dir :TDirection): TDirection;
  170. begin
  171.   case Dir of
  172.     LEFT: Result := RIGHT;
  173.     RIGHT: Result := LEFT;
  174.     UP: Result := DOWN;
  175.     DOWN: Result := UP;
  176.     else Result := None;
  177.   end;
  178. end;
  179.  
  180. function GetFoodInSquare(Map: TGameNetwork; SqrX0Y0:TLocation): Integer;
  181. var X0, Y0, I, J :Integer;
  182. begin
  183.   Result := 0;
  184.   X0 := Round(SqrX0Y0.X);
  185.   Y0 := Round(SqrX0Y0.Y);
  186.   for I := X0 to Round(X0 + AMBUSHER_SQR) do
  187.   begin
  188.       for J := Y0 to Round(Y0 + AMBUSHER_SQR) do
  189.       begin
  190.         if Map[J][I] = Food then Inc(Result);
  191.       end;
  192.   end;
  193. end;
  194.  
  195. function GetPointForAmbusher(Map: TGameNetwork): TLocation;
  196. var
  197.   Spots :TASpots;
  198.   SqrCr :Array of TLocation;
  199.   I, J, X ,Y :Integer;
  200.   Temp :TLocation;
  201. begin
  202.   for I := 0 to (Length(Map) div Round(AMBUSHER_SQR)) do
  203.   begin
  204.     for J := 0 to (Length(Map[I]) div Round(AMBUSHER_SQR)) do
  205.     begin
  206.       SetLength(SqrCr, Length(SqrCr) + 1);
  207.       X := J;
  208.       Y := I;
  209.       if Length(Map) <= Round((J + 1) * AMBUSHER_SQR) then X := Round(X - (AMBUSHER_SQR - Length(Map) mod Round(AMBUSHER_SQR)));
  210.       if Length(Map[I]) <= Round((I + 1) * AMBUSHER_SQR) then Y := Round(Y - (AMBUSHER_SQR - Length(Map) mod Round(AMBUSHER_SQR)));
  211.       SqrCr[Length(SqrCr)-1] := ConvertToLocation(X, Y);
  212.     end;
  213.   end;
  214.   SetLength(SPots, Length(SqrCr));
  215.   for I:=0 to Length(SqrCr)-1 do
  216.   begin
  217.     Spots[I].SqrXY := SqrCr[I];
  218.     Spots[I].Food := GetFoodInSquare(Map, SqrCr[I]);
  219.   end;
  220.   Temp := SortAndFind(Spots).SqrXY;
  221.   Result := ConvertToLocation((Temp.X+Round(AMBUSHER_SQR / 2.0)) * SQUARE,(Temp.Y + Round(AMBUSHER_SQR / 2.0)) * SQUARE);
  222. end;
  223.  
  224. end.
RAW Paste Data
Top