Advertisement
MadCortez

Untitled

May 21st, 2021
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.91 KB | None | 0 0
  1. unit UnitPlayer;
  2.  
  3. interface
  4.  
  5. procedure GameOverPlayer;
  6. procedure ClearPlayer;
  7. procedure EatApplePlayer;
  8. procedure CheckForLosePlayer;
  9. procedure GamePlayPlayer;
  10. procedure MoveSnakePlayer;
  11. procedure SpawnHeadPlayer;
  12.  
  13. implementation
  14.  
  15. Uses
  16.    SysUtils, UnitGame, UnitMain, Classes, Vcl.Dialogs, UnitBot;
  17.  
  18. procedure GameOverPlayer;
  19. var
  20.    i: Integer;
  21.    GameOverTxt: String;
  22. begin
  23.    Form1.TimerPlayer.Enabled := False;
  24.    Form1.TimerBot.Enabled := False;
  25.    StopBot;
  26.    GameOverTxt := 'Game Over' + #10#13 + 'Your score = ' + IntToStr(Score);
  27.    MessageDlg(GameOverTxt, mtCustom, [mbOK], 0);
  28.    ClearPlayer;
  29.    Form1.NewGame;
  30. end;
  31.  
  32. procedure ClearPlayer;
  33. var
  34.    i: Integer;
  35. begin
  36.    AHead.Free;
  37.    AApple.Free;
  38.    for i := 1 to SnakeLen do
  39.       ATails[i].Free;
  40.    for i := 1 to BoxNum do
  41.       ABoxs[i].Free;
  42. end;
  43.  
  44. procedure EatApplePlayer;
  45. begin
  46.    Inc(Score);
  47.    Form1.LabelPoint.Caption := IntToStr(Score);
  48.    SpawnApple;
  49.    Inc(SnakeLen);
  50.    ATails[SnakeLen] := TTail.Create(ATails[SnakeLen - 1].GetX, ATails[SnakeLen - 1].GetY, TailPic);
  51.    if Form1.TimerPlayer.Interval > 55 then
  52.       Form1.TimerPlayer.Interval := Form1.TimerPlayer.Interval - 5;
  53. end;
  54.  
  55. procedure CheckForLosePlayer;
  56. var
  57.    i: Integer;
  58. begin
  59.    for i := 1 to SnakeLen do
  60.       if (AHead.GetX = ATails[i].GetX) and (AHead.GetY = ATails[i].GetY) then
  61.          GameOverPlayer;
  62.    for i := 1 to BoxNum do
  63.       if (AHead.GetX = ABoxs[i].GetX) and (AHead.GetY = ABoxs[i].GetY) then
  64.          GameOverPlayer;
  65.    for i := 1 to SnakeLenBot do
  66.       if (AHead.GetX = ATailsBot[i].GetX) and (AHead.GetY = ATailsBot[i].GetY) then
  67.          GameOverPlayer;
  68. end;
  69.  
  70. procedure GamePlayPlayer;
  71. var
  72.    i: Integer;
  73.    Valid: Boolean;
  74. begin
  75.    if (AHead.GetX = AApple.GetX) and (AHead.GetY = AApple.GetY) then
  76.       EatApplePlayer;
  77.  
  78.    if AHead.GetX < 0 then
  79.       AHead.SetX(19 * 24);
  80.    if AHead.GetY < 0 then
  81.       AHead.SetY(19 * 24);
  82.    if AHead.GetX >= 20 * 24 then
  83.       AHead.SetX(0);
  84.    if AHead.GetY >= 20 * 24 then
  85.       AHead.SetY(0);
  86.  
  87.    CheckForLosePlayer;
  88. end;
  89.  
  90. procedure MoveSnakePlayer;
  91. var
  92.    i: Integer;
  93. begin
  94.    for i := SnakeLen downto 2 do
  95.    begin
  96.       ATails[i].SetX(ATails[i - 1].GetX);
  97.       ATails[i].SetY(ATails[i - 1].GetY);
  98.    end;
  99.    ATails[i].SetX(AHead.GetX);
  100.    ATails[i].SetY(AHead.GetY);
  101.  
  102.    if Dir = 'Down' then
  103.       AHead.SetY(AHead.GetY + 24);
  104.    if Dir = 'Up' then
  105.       AHead.SetY(AHead.GetY - 24);
  106.    if Dir = 'Left' then
  107.       AHead.SetX(AHead.GetX - 24);
  108.    if Dir = 'Right' then
  109.       AHead.SetX(AHead.GetX + 24);
  110. end;
  111.  
  112. procedure SpawnHeadPlayer;
  113. var
  114.    Valid: Boolean;
  115.    i: Integer;
  116. begin
  117.    repeat
  118.       Valid := True;
  119.       AHead.SetX(random(20) * 24);
  120.       AHead.SetY(random(20) * 24);
  121.       for i := 1 to BoxNum do
  122.          if (AHead.GetX = ABoxs[i].GetX) and (AHead.GetY = ABoxs[i].GetY) then
  123.             Valid := False;
  124.    until Valid;
  125. end;
  126.  
  127. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement