Posted By

iLLUMIN on 11/24/09


Tagged

game pascal finished


Versions (?)

Snake 2.0


 / Published in: Pascal
 

URL: www.sa1t.ru

New ver.

  1. uses crt;
  2. const
  3. MAX_LENGTH = 10;
  4. SNAKE_LENGTH = 5;
  5. WALL_LENGTH = 3;
  6. START_SPEED = 800;
  7. type
  8. Snake_Part = record
  9. x,y : integer;
  10. path: integer; { 0 - nothing; 1 - up; 2 - down; 3 - left; 4 - right}
  11. end;
  12. Other_Part = record
  13. x,y : integer;
  14. end;
  15. var
  16. snake : array [1..SNAKE_LENGTH] of Snake_Part;
  17. map : array [1..MAX_LENGTH,1..MAX_LENGTH] of integer;
  18. wall : array [1..WALL_LENGTH] of Other_Part;
  19. eate : Other_Part;
  20. YesNo : boolean = false;
  21. sch,sch2,sch3 : integer;
  22. score : longint;
  23. path : char = #0;
  24. q : text;
  25. name : string = '';
  26. speed : integer = 0;
  27.  
  28. begin
  29. RandomIze;
  30. {set stats}
  31. {start score}
  32. score := 0;
  33. {/start score}
  34. {start eate}
  35. eate.x := (Random(MAX_LENGTH-1)+1);
  36. eate.y := (Random(MAX_LENGTH-1)+1);
  37. {/start eate}
  38. {start snake}
  39. snake[1].x := 5;
  40. for sch := 1 to SNAKE_LENGTH do begin
  41. snake[sch].y := 4;
  42. if sch > 1 then snake[sch].x := snake[sch-1].x-1;
  43. snake[sch].path := 4;
  44. end;
  45. {/start snake}
  46. {start wall}
  47. wall[1].x := Random(MAX_LENGTH-1)+1;
  48. wall[1].y := Random(MAX_LENGTH-1)+1;
  49. for sch := 2 to WALL_LENGTH do begin
  50. wall[sch].y := wall[1].y;
  51. wall[sch].x := wall[sch-1].x + 1;
  52. end;
  53. {/start wall}
  54. {/set stats}
  55. delay(400);
  56. repeat {start main prog}
  57. {wait for key (readkey)} {OLOLOLOLOLOL PPC *WALL*} {HE TPOrATb!!! PA6OTAET u JIAdHO}
  58. delay(START_SPEED-speed);
  59. if keypressed then path := ReadKey;
  60. if path = 'w' then snake[1].path := 1;
  61. if path = 's' then snake[1].path := 2;
  62. if path = 'a' then snake[1].path := 3;
  63. if path = 'd' then snake[1].path := 4;
  64. if path = #27 then halt;
  65. if path = 'p' then begin
  66. repeat
  67. until keypressed;
  68. end;
  69. {/wait for key (readkey)}
  70. {move}
  71. {1}
  72. if snake[1].path = 1 then begin
  73. snake[1].y := snake[1].y - 1;
  74. end;
  75. if snake[1].path = 2 then begin
  76. snake[1].y := snake[1].y + 1;
  77. end;
  78. if snake[1].path = 3 then begin
  79. snake[1].x := snake[1].x -1;
  80. end;
  81. if snake[1].path = 4 then begin
  82. snake[1].x := snake[1].x + 1;
  83. end;
  84. {/1}
  85. {other}
  86. for sch := 2 to SNAKE_LENGTH do begin
  87. if snake[sch].path = 1 then begin
  88. snake[sch].y := snake[sch].y - 1;
  89. end;
  90. if snake[sch].path = 2 then begin
  91. snake[sch].y := snake[sch].y + 1;
  92. end;
  93. if snake[sch].path = 3 then begin
  94. snake[sch].x := snake[sch].x -1;
  95. end;
  96. if snake[sch].path = 4 then begin
  97. snake[sch].x := snake[sch].x + 1;
  98. end;
  99. end;
  100. for sch := SNAKE_LENGTH downto 2 do snake[sch].path := snake[sch-1].path;
  101. {/other}
  102. {speed}
  103. if score >= 100 then speed := 40;
  104. if score >= 200 then speed := 60;
  105. if score >= 300 then speed := 110;
  106. if score >= 500 then speed := 170;
  107. if score >= 660 then speed := 200;
  108. if score >= 740 then speed := 300;
  109. if score >= 1000 then speed := 350;
  110. if score >= 1500 then speed := 400;
  111. {/speed}
  112. {/move}
  113. {pick eate}
  114. if ((snake[1].x = eate.x) and (snake[1].y = eate.y)) then begin
  115. score := score + 20;
  116. eate.x := (Random(MAX_LENGTH-1)+1);
  117. eate.y := (Random(MAX_LENGTH-1)+1);
  118. end;
  119. {/pick eate}
  120. {die}
  121. {FTB}
  122. for sch := 2 to SNAKE_LENGTH do if ((snake[1].x = snake[sch].x) and (snake[1].y = snake[sch].y)) then begin
  123. clrscr;
  124. writeln('End of game');
  125. readln;
  126. Assign(q,'record'); reset(q);
  127. readln(q,sch2);
  128. Close(q);
  129. if score > sch2 then begin
  130. writeln('You win! New record! Enter your name:');
  131. readln(name);
  132. Assign(q,'record'); ReWrite(q);
  133. writeln(q,score);
  134. writeln(q,name);
  135. Close(q);
  136. end;
  137. halt;
  138. end;
  139. {/FTB}
  140. {out of range}
  141. if ( ( (snake[1].x < 1) or (snake[1].x > MAX_LENGTH) ) or ( (snake[1].y < 1) or (snake[1].y > MAX_LENGTH) ) ) then begin
  142. clrscr;
  143. writeln('End of game');
  144. readln;
  145. Assign(q,'record'); reset(q);
  146. readln(q,sch2);
  147. Close(q);
  148. if score > sch2 then begin
  149. writeln('You win! New record! Enter your name:');
  150. readln(name);
  151. Assign(q,'record'); ReWrite(q);
  152. writeln(q,score);
  153. writeln(q,name);
  154. Close(q);
  155. end;
  156. halt;
  157. end;
  158. {/out of range}
  159. {wall}
  160. for sch := 1 to WALL_LENGTH do if ( ((snake[1].x = wall[sch].x) and (snake[1].y = wall[sch].y)) ) then begin
  161. clrscr;
  162. writeln('End of game');
  163. readln;
  164. Assign(q,'record'); reset(q);
  165. readln(q,sch2);
  166. Close(q);
  167. if score > sch2 then begin
  168. writeln('You win! New record! Enter your name:');
  169. readln(name);
  170. Assign(q,'record'); ReWrite(q);
  171. writeln(q,score);
  172. writeln(q,name);
  173. Close(q);
  174. end;
  175. halt;
  176. end;
  177. {/wall}
  178. {/die}
  179. {draw}
  180. begin
  181. clrscr;
  182. {generate matrix} {TyT TO>|<E OCO6O HE JIA3uTb}
  183. for sch := 1 to MAX_LENGTH do begin
  184. for sch2 := 1 to MAX_LENGTH do begin
  185. for sch3 := 1 to SNAKE_LENGTH do if ((snake[sch3].x = sch2) and (snake[sch3].y = sch)) then begin
  186. map[sch,sch2] := 1;
  187. YesNo := true;
  188. end;
  189. if ((eate.x = sch2) and (eate.y = sch)) then begin
  190. map[sch,sch2] := 2;
  191. YesNo := true;
  192. end;
  193. for sch3 := 1 to WALL_LENGTH do if ((wall[sch3].x = sch2) and (wall[sch3].y = sch)) then begin
  194. map[sch,sch2] := 4;
  195. YesNo := true;
  196. end;
  197. if YesNo = false then begin
  198. map[sch,sch2] := 0;
  199. YesNo := false;
  200. end;
  201.  
  202. end;
  203. end;
  204. {/generate matrix}
  205. {draw matrix}
  206. for sch := 1 to MAX_LENGTH do begin
  207. for sch2 := 1 to MAX_LENGTH do write(map[sch,sch2]);
  208. writeln('');
  209. end;
  210. writeln('Score: ',score);
  211. {/draw matrix}
  212. {clean up}
  213. for sch := 1 to MAX_LENGTH do
  214. for sch2 := 1 to MAX_LENGTH do map[sch,sch2] := 0;
  215. {/clean up}
  216. end;
  217. {/draw}
  218. {clean up}
  219. path := #0;
  220. {/clean up}
  221. until false;{end main prog}
  222. end.

Report this snippet  

You need to login to post a comment.