Shinz3K

Untitled

Sep 18th, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. Program Game_Snake;
  2. Uses crt;
  3. const
  4. Max_X = 60;
  5. Max_Y = 25;
  6. Min_X = 1;
  7. Min_Y = 1;
  8. Than =#219;
  9. Dau = #219;
  10. Var
  11. dai,foodx,foody,foodcl,rancl,speed, score, MaxScore: integer;
  12. ranx,rany: array[1..100] of integer;
  13. dir_food,time,t: integer;
  14. f: text;
  15. ReGame: char;
  16.  
  17. Procedure Draw_Range;
  18. var i: integer;
  19. Begin
  20. TextColor(Green);
  21. for i:= Min_X to Max_X do
  22. begin
  23. Gotoxy(i,Min_Y);
  24. Write(#178);
  25. Gotoxy(i,Max_Y);
  26. Write(#178);
  27. end;
  28.  
  29. for i:= Min_Y to Max_Y do
  30. begin
  31. Gotoxy(Min_X,i);
  32. Write(#178);
  33. Gotoxy(Max_X,i);
  34. Write(#178);
  35. end;
  36. End;
  37.  
  38. Procedure Write_Snake;
  39. var i: integer;
  40. Begin
  41. Gotoxy(ranx[dai],rany[dai]);Write(#32);
  42. for i := dai downto 2 do
  43. begin
  44. ranx[i] := ranx[i-1];
  45. rany[i] := rany[i-1];
  46. TextColor(rancl);
  47. gotoxy(ranx[i],rany[i]);
  48. write(than);
  49. end;
  50. TextColor(White);
  51. gotoxy(ranx[1],rany[1]);
  52. write(dau);
  53.  
  54. {Draw Food}
  55. Textcolor(foodcl);
  56. Gotoxy(foodx,foody);Write(#220);
  57. End;
  58.  
  59. Procedure Create_Food;
  60. Begin
  61. Randomize;
  62. repeat
  63. foodx := Random(Max_X - 1);
  64. foody := Random(Max_Y - 1);
  65. foodcl := random(10);
  66. Until (foodx > 1) and (foody > 1) and (foodcl > 1);
  67. Textcolor(foodcl);
  68. Gotoxy(foodx,foody);Write(#220);
  69. End;
  70.  
  71. Function Die: Boolean;
  72. var x,y,i: integer;
  73. Begin
  74. Die := False;
  75. x := ranx[1];
  76. y := rany[1];
  77. if (x > Min_X) and (x < Max_X) and (y > Min_Y) and (y < Max_Y) then
  78. begin
  79. for i := dai downto 4 do
  80. if (x = ranx[i]) and (y = rany[i]) then
  81. begin
  82. Die := True;
  83. break;
  84. end;
  85. end
  86. else Die := True;
  87. End;
  88.  
  89. Procedure Set_Dir_Food;
  90. Begin
  91. repeat
  92. time := 5 + random(5);
  93. dir_food := random(5);
  94. until (time > 0) and (dir_food > 0);
  95. t:= 0;
  96. End;
  97.  
  98. Procedure Move_Food;
  99. Begin
  100. t := t+1;
  101. if t mod 3 = 0 then
  102. case dir_food of
  103. 1: if foody > Min_Y + 1 then begin
  104. gotoxy(foodx,foody);Write(#32);
  105. foody := foody - 1;
  106. end;
  107. 2: if foodx > Min_X + 1 then begin
  108. gotoxy(foodx,foody);Write(#32);
  109. foodx := foodx - 1;
  110. end;
  111. 3: if foody < Max_Y - 1 then begin
  112. gotoxy(foodx,foody);Write(#32);
  113. foody := foody + 1;
  114. end;
  115. 4: if foodx < Max_X - 1 then begin
  116. gotoxy(foodx,foody);Write(#32);
  117. foodx := foodx + 1;
  118. end;
  119. end;
  120. if t = time then Set_Dir_Food;
  121. End;
  122.  
  123. Procedure Move;
  124. var k,lk: char;
  125. Begin
  126. Set_Dir_Food;
  127. k := lk;
  128. repeat until Keypressed;
  129. repeat
  130. Move_Food;
  131. if Keypressed then begin lk:= k;k := ReadKey;end;
  132. case k of
  133. #72: rany[1] := rany[1] - 1;
  134. #75: ranx[1] := ranx[1] - 1;
  135. #77: ranx[1] := ranx[1] + 1;
  136. #80: rany[1] := rany[1] + 1;
  137. 'p':
  138. begin
  139. TextColor(Red);
  140. repeat
  141. gotoxy(63,10);
  142. Write('Bam p de tiep tuc!');
  143. until readkey = 'p';
  144. Gotoxy(63,10);
  145. Write(' ');
  146. end;
  147. end;
  148. if k = 'p' then k := lk;
  149. if (ranx[1] = foodx) and (rany[1] = foody) then
  150. Begin
  151. dai := dai + 1;
  152. score := score + 1;
  153. rancl := Foodcl;
  154. Create_Food;
  155. TextColor(Cyan);
  156. Gotoxy(63,3);
  157. Write('Diem: ',score);
  158. End;
  159. Write_Snake;
  160. Delay(Speed);
  161. Until (k = #27) or Die;
  162. if k = #27 then ReGame := #27;
  163. End;
  164.  
  165. Procedure Choose_Level;
  166. var level: integer;
  167. Begin
  168. TextColor(White);
  169. Writeln('Chon che do: ');
  170. Writeln('1. Kho');
  171. Writeln('2. Vua');
  172. Writeln('3. De');
  173. repeat readln(level); until (level >=1) and (level <= 3);
  174. speed := level*50;
  175. clrscr;
  176. TextColor(Cyan);
  177. Gotoxy(63,7);
  178. Write('Muc do: ');
  179. case level of
  180. 1: Write('Kho');
  181. 2: Write('Vua');
  182. 3: Write('De');
  183. end;
  184. End;
  185.  
  186. Procedure OpenFile;
  187. Begin
  188. reset(f);
  189. read(f,MaxScore);
  190. close(f);
  191. End;
  192. Procedure ReWriteFile;
  193. Begin
  194. if Score > MaxScore then
  195. begin
  196. ReWrite(f);
  197. Write(f,Score);
  198. Close(f);
  199. end;
  200. End;
  201.  
  202. Procedure Main;
  203. var i: integer;
  204. Begin
  205. Repeat
  206. clrscr;
  207. Assign(f,'MaxScore.txt');
  208. OpenFile;
  209. Choose_Level;
  210. dai := 5;
  211. rancl := Blue;
  212. score := 0;
  213. TextColor(Cyan);
  214. Gotoxy(63,3);
  215. Write('Diem: ',score);
  216. Gotoxy(63,5);
  217. Write('Diem cao nhat:',MaxScore);
  218. for i := 1 to dai do
  219. begin
  220. ranx[i] := i+3;
  221. rany[i] := 5;
  222. TextColor(rancl);
  223. gotoxy(ranx[i],rany[i]);
  224. write(than);
  225. end;
  226. TextColor(White);
  227. gotoXy(ranx[1],rany[1]);Write(dau);
  228. rancl := Blue;
  229. Draw_Range;
  230. Create_Food;
  231. Move;
  232. ReWriteFile;
  233. if ReGame <> #27 then
  234. begin
  235. readln;
  236. clrscr;
  237. TextColor(White);
  238. Write('Ban co muon choi lai khong. Bam "y" neu co.');
  239. repeat until keypressed;
  240. ReGame := readkey;
  241. end;
  242. Until ReGame <> 'y';
  243. End;
  244.  
  245. BEGIN
  246. clrscr;
  247. Main;
  248. Gotoxy(20,12);
  249. Write('Bam Enter de thoat...');
  250. readln;
  251. END.
Advertisement
Add Comment
Please, Sign In to add comment