LIFE.PAS 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. Uses MyTPU,Graph,CRT,F_mouse;
  2. Var
  3. Grid : array[1..160,1..120] of boolean;
  4. Test : array[1..160,1..120] of boolean;
  5. x,y : byte;
  6. xcell,ycell,dl1 : integer;
  7. {-------------============--------------}
  8. Function TestCell(x0,y0:byte):byte;
  9. Var
  10. counter : byte;
  11. i,j : -1..1;
  12. Begin
  13. counter := 0;
  14. for i := -1 to 1 do
  15. for j := -1 to 1 do if (x0+i in [1..xcell]) and (y0 + j in [1..ycell]) then if Grid[x0+i,y0+j] then Inc(counter);
  16. TestCell := counter;
  17. End;{TestCell}
  18. {----------------------------}
  19. Procedure Turn;
  20. Var
  21. x1,y1 :byte;
  22. Begin
  23. for x1 := 1 to xcell do
  24. for y1 := 1 to ycell do Test[x1,y1] := Grid[x1,y1];
  25. for x1 := 1 to xcell do
  26. for y1 := 1 to ycell do
  27. begin
  28. if Grid[x1,y1] then
  29. case TestCell(x1,y1) of
  30. 1..2 : Test[x1,y1] := false;
  31. 5..10: Test[x1,y1] := false
  32. end
  33. else
  34. if TestCell(x1,y1) = 3 then Test[x1,y1] := true;
  35. end;
  36. End;
  37. {-----------------------------}
  38. Procedure ShowCell(x0,y0:byte);
  39. Begin
  40. SetFillStyle(solidfill,red);
  41. SetColor(black);
  42. HideMouse;
  43. FillEllipse(x0*round(640/xcell)-round(320/xcell),
  44. y0*round(480/ycell)-round(240/ycell),
  45. round(320/xcell)-1,round(240/ycell)-1);
  46. ShowMouse;
  47. End;{ShowCell}
  48. {-----------------------------}
  49. Procedure HideCell(x0,y0:byte);
  50. Begin
  51. SetFillStyle(solidfill,black);
  52. SetColor(black);
  53. HideMOuse;
  54. FillEllipse(x0*round(640/xcell)-round(320/xcell),
  55. y0*round(480/ycell)-round(240/ycell),
  56. round(320/xcell)-1,round(240/ycell)-1);
  57. ShowMouse;
  58. End;{ShowCell}
  59. {-----------------------------}
  60. Procedure ShowAll;
  61. Var
  62. x1,y1 :byte;
  63. Begin
  64. for x1 := 1 to xcell do
  65. for y1 := 1 to ycell do
  66. begin
  67. if Test[x1,y1] <> Grid[x1,y1] then
  68. begin
  69. if Test[x1,y1] then ShowCell(x1,y1)
  70. else HideCell(x1,y1);
  71. Grid[x1,y1] := Test[x1,y1];
  72. end;
  73. end;
  74. Delay(Dl1);
  75. End;{ShowAll}
  76. {---------------------------}
  77. Procedure Init;
  78. Var
  79. j,i : byte;
  80. Begin
  81. Repeat
  82. ClrScr;
  83. GotoXY(5,10);
  84. TextColor(Magenta);
  85. Write('Введите число клеток по X, Y, задержку: ');
  86. ReadLn(Xcell,Ycell,Dl1);
  87. Until (Xcell in [1..160]) and (Ycell in [1..120]);
  88. InitVGA;
  89. SetColor(Blue);
  90. SetWriteMode(XORPut);
  91. Mouse;
  92. HideMouse;
  93. For j := 1 to ycell do Line(1,round(j*480/ycell),640,round(j*480/ycell));
  94. For j := 1 to xcell do Line(round(640/xcell)*j,1,round(640/xcell)*j,480);
  95. End;{Init}
  96. {-----------------------------}
  97. Function LastTurn: boolean;
  98. Var
  99. x,y : byte;
  100. Begin
  101. LastTurn := true;
  102. for x := 1 to xcell do
  103. for y := 1 to ycell do if Test[x,y] <> Grid[x,y] then LastTurn := false;
  104. End;{LastTurn}
  105. {-------------------}
  106. Procedure Game;
  107. Var
  108. bool : boolean;
  109. c:char;
  110. Begin
  111. Repeat
  112. Turn;
  113. bool := LastTurn;
  114. ShowAll;
  115. if keypressed then c := Readkey;
  116. Until bool or (c = #27) or (c=#13);
  117. End;{Game}
  118. {-----------------------------------------}
  119. Procedure ReadPoints;
  120. Var
  121. x0,y0 : integer;
  122. c:char;
  123. Begin
  124. ShowMouse;
  125. repeat
  126. if keypressed then c := readkey;
  127. if mousepressed then
  128. begin
  129. MouseWhereXY(x0,y0);
  130. if Grid[x0 div round(640/xcell)+1,y0 div round(480/ycell)+1] then begin
  131. HideCell(x0 div round(640/xcell)+1,y0 div round(480/ycell)+1);
  132. Grid[x0 div round(640/xcell)+1,y0 div round(480/ycell)+1] := false;
  133. end
  134. else
  135. begin
  136. ShowCell(x0 div round(640/xcell)+1,y0 div round(480/ycell)+1);
  137. Grid[x0 div round(640/xcell)+1,y0 div round(480/ycell)+1] := true;
  138. end;
  139. Delay(3000);
  140. end;
  141. until (c = #27) or (c=#13);
  142. End;
  143. {-----------------------------------------}
  144. Procedure ExitProgram;
  145. Begin
  146. HideMouse;
  147. CloseGraph;
  148. End;{ExitProgram}
  149. {-----------------------------------------}
  150. Function NotWant:boolean;
  151. Var
  152. c : char;
  153. p : pointer;
  154. s : word;
  155. LastColor : integer;
  156. Begin
  157. NotWant := false;
  158. LastColor := GetColor;
  159. HideMouse;
  160. s := ImageSize(160,160,480,320);
  161. GetMem(p,s);
  162. GetImage(160,160,480,320,p^);
  163. SetTextStyle(DefaultFont,HorizDir,3);
  164. SetTextJustify(CenterText,TopText);
  165. SetColor(Green);
  166. OutTextXY(320,180,'Life stopped');
  167. SetColor(Yellow);
  168. SetTextStyle(DefaultFont,HorizDir,1);
  169. OutTextXY(320,220,'Press Esc to Exit');
  170. OutTextXY(320,240,'Any other key to continue');
  171. c := readkey;
  172. if c = #27 then NotWant := true;
  173. PutImage(160,160,p^,NormalPut);
  174. FreeMem(p,s);
  175. Dispose(p);
  176. ShowMouse;
  177. SetColor(LastColor)
  178. End;
  179. {--------------------------------}
  180. Begin
  181. Init;
  182. repeat
  183. ReadPoints;
  184. Game;
  185. until NotWant;
  186. ExitProgram;
  187. End.