Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | None | 0 0
  1. /*compile with
  2. valac --pkg sdl game_of_life.vala -o game_of_life
  3. */
  4. using SDL;
  5.  
  6. bool mouse_is_down = false;
  7.  
  8. public class Cell
  9. {
  10. public Rect on_screen_rect;
  11. public Rect self_rect;
  12. public Surface surface;
  13. public weak Screen screen;
  14. public bool alive {get;set;}
  15. public bool updated;
  16. public int16 size;
  17. private uint32 life_color;
  18. private uint32 dead_color;
  19. public Cell(Screen s, uint32 flags,int16 x, int16 y, int16 size,uint32 lcolor,uint32 bg_color )
  20. {
  21. life_color = lcolor;
  22. dead_color = bg_color;
  23. updated=false;
  24. screen = s;
  25. on_screen_rect.w = size;
  26. on_screen_rect.h = size;
  27. on_screen_rect.x = x;
  28. on_screen_rect.y = y;
  29. self_rect.w = size;
  30. self_rect.h = size;
  31. self_rect.x=0;
  32. self_rect.y=0;
  33. this.size = (int16)size;
  34. alive=false;
  35. //create the surface
  36. surface = new Surface.RGB(flags, size, size, 32, 0,0,0, 255);
  37. }
  38. public void set_life(bool life)
  39. {
  40. alive=life;
  41. if (alive)
  42. {
  43. surface.fill(null,life_color);
  44. }else{
  45. surface.fill(null,dead_color);
  46. }
  47. }
  48. public void draw()
  49. {
  50. surface.blit(self_rect,screen,on_screen_rect);
  51. }
  52. }
  53.  
  54. public class Game
  55. {
  56. private const int SCREEN_WIDTH = 800;
  57. private const int SCREEN_HEIGHT = 600;
  58. private const int SCREEN_BPP = 32;
  59. private const int DELAY = 100;
  60. private weak SDL.Screen screen;
  61. private bool do_loop;
  62. private Cell[,] cells;
  63. private int16 num_cols;
  64. private int16 num_rows;
  65. private uint32 bg_color;
  66. private int random_max;
  67. private uint16 cell_size;
  68.  
  69. public Game(int16 cell_size, int random_max)
  70. {
  71. this.cell_size = cell_size; //game object needs to know this to update on position
  72. this.random_max = random_max;
  73. //initialize the video
  74. uint32 surface_flags = SurfaceFlag.DOUBLEBUF |
  75. SurfaceFlag.HWACCEL
  76. | SurfaceFlag.HWSURFACE;
  77.  
  78. screen = Screen.set_video_mode (SCREEN_WIDTH, SCREEN_HEIGHT,
  79. SCREEN_BPP, surface_flags);
  80. if (screen == null) {
  81. GLib.error ("Could not set video mode.");
  82. }
  83. //we have a screen, define some colors
  84. bg_color=screen.format.map_rgb(0,0,0);
  85. uint32 life_color=screen.format.map_rgb(0,255,0);
  86. SDL.WindowManager.set_caption ("Game of Life: Vala, SDL", "");
  87. //make all of the cells
  88. //'''fill the screen with rectangles'''
  89. num_cols = (int16)SCREEN_WIDTH/cell_size;
  90. num_rows = (int16)SCREEN_HEIGHT/cell_size;
  91. uint32 cells_to_create = num_cols*num_rows;
  92. int16 y;
  93. int16 x;
  94. cells = new Cell[num_rows,num_cols];
  95. stdout.printf( "%0.f cells to create\n",cells_to_create);
  96. for ( int16 row=0; row<num_rows; row++)
  97. {
  98. y = row*cell_size;
  99. for ( int16 column=0; column<num_cols; column++)
  100. {
  101. x=column*cell_size;
  102. //set the column index to a rect that is cell_size by cell_size
  103. cells[row,column] = new Cell(screen,surface_flags,x,y,cell_size,life_color,bg_color);
  104. }
  105. cells_to_create-=num_cols;
  106. }
  107. stdout.printf( "cells created\n" );
  108. do_loop = true;
  109. }
  110.  
  111. private void set_cell_alive(uint16 x, uint16 y)
  112. {
  113. cells[(int16)(y / cell_size), (int16)(x / cell_size)].set_life(true);
  114. }
  115.  
  116. private void seed_life(bool reseed=false)
  117. {
  118. Rand rand = new Rand();
  119. stdout.printf( "adding random life\n" );
  120. //loop through the rows
  121. for(int r=0; r<num_rows;r++)
  122. {
  123. //loop through the columns
  124. for(int c=0; c<num_cols ; c++)
  125. {
  126. //generate a number
  127. int i = rand.int_range(0,random_max);
  128. if (i==0)
  129. {
  130. //r,c is alive
  131. cells[r,c].set_life(true);
  132. }else if (reseed)
  133. {
  134. cells[r,c].set_life(false);
  135. }
  136. }
  137. }
  138. }
  139.  
  140. public void run()
  141. {
  142. //seed life into the cells
  143. seed_life();
  144. //do stuff
  145. while (do_loop) {
  146. this.draw ();
  147. this.process_events ();
  148. detect_life();
  149. SDL.Timer.delay (DELAY);
  150. }
  151. }
  152. private void quit()
  153. {
  154. do_loop = false;
  155. }
  156. private void process_events()
  157. {
  158. Event event = Event ();
  159. while (Event.poll (event) == 1) {
  160. switch (event.type) {
  161. case EventType.QUIT:
  162. quit();
  163. break;
  164. case EventType.KEYDOWN:
  165. this.on_keyboard_event (event.key);
  166. break;
  167. case EventType.MOUSEMOTION:
  168. if (mouse_is_down) {
  169. set_cell_alive(event.motion.x, event.motion.y);
  170. }
  171. break;
  172. case EventType.MOUSEBUTTONDOWN:
  173. if (event.button.button == 1) { //left click
  174. mouse_is_down = true;
  175. }
  176. break;
  177. case EventType.MOUSEBUTTONUP:
  178. if (event.button.button == 1) {
  179. mouse_is_down = false;
  180. }
  181. break;
  182. }
  183. }
  184. }
  185.  
  186. private void on_keyboard_event (KeyboardEvent event)
  187. {
  188. if(event.keysym.sym==KeySymbol.q)
  189. {
  190. quit();
  191. }
  192. if(event.keysym.sym==KeySymbol.ESCAPE)
  193. {
  194. quit();
  195. }
  196. if(event.keysym.sym==KeySymbol.r)
  197. {
  198. seed_life(true);
  199. }
  200. }
  201.  
  202. private void draw()
  203. {
  204. screen.fill (null, bg_color);
  205. foreach( Cell c in cells )
  206. {
  207. c.draw();
  208. }
  209. screen.update_rect(0,0,screen.w,screen.h);
  210. }
  211.  
  212. private int living_neighbors(int r, int c)
  213. {
  214. int alive = 0;
  215. int r_min = r-1;
  216. int r_max = r+1;
  217. int c_min = c-1;
  218. int c_max = c+1;
  219. if(r==0)
  220. {
  221. r_min=r;
  222. }
  223. else if (r == num_rows-1)
  224. {
  225. r_max=r;
  226. }
  227. if ( c==0 )
  228. {
  229. c_min=c;
  230. }
  231. else if (c == num_cols-1)
  232. {
  233. c_max=c;
  234. }
  235. int tr = r_min;
  236. int tc = c_min;
  237. while(tr <= r_max)
  238. {
  239. tc = c_min;
  240. while(tc <= c_max)
  241. {
  242. if ( tr!=r || tc!=c )
  243. {
  244. if ( cells[tr,tc].alive)
  245. {
  246. alive+=1;
  247. }
  248. }
  249. tc+=1;
  250. }
  251. tr+=1;
  252. }
  253. return alive;
  254. }
  255.  
  256. public void detect_life()
  257. {
  258. int r;
  259. int c;
  260. int index;
  261. Array<int>? dead_array = new Array<int>(false,false,(uint)sizeof(int));
  262. Array<int>? alive_array = new Array<int>(false,false,(uint)sizeof(int));
  263. bool alive;
  264. int ln;
  265. for(r=0; r<num_rows; r++)
  266. {
  267. for(c=0; c<num_cols; c++)
  268. {
  269. alive = cells[r,c].alive;
  270. ln = living_neighbors(r,c);
  271. if( alive && ( ln<2 || ln>3 ))
  272. {
  273. //this cell will die; boohoo
  274. dead_array.append_val(r);
  275. dead_array.append_val(c);
  276. }
  277. else if(!alive && ln==3)
  278. {
  279. //this cell is now alive; damn zombies
  280. alive_array.append_val(r);
  281. alive_array.append_val(c);
  282. }
  283. }
  284. }
  285. //kill what needs killing
  286. uint final_index = dead_array.length-1;
  287. for(index=0 ; index<final_index ; index+=2)
  288. {
  289. r = dead_array.index(index);
  290. c= dead_array.index(index+1);
  291. cells[r,c].set_life(false);
  292. }
  293. //frankenstein what needs life
  294. final_index = alive_array.length-1;
  295. for(index=0 ; index<final_index ; index+=2)
  296. {
  297. r = alive_array.index(index);
  298. c= alive_array.index(index+1);
  299. cells[r,c].set_life(true);
  300. }
  301. }
  302. }
  303.  
  304. public static void main(string[] args)
  305. {
  306. int16 cell_size = 2;
  307. int random_max = 15;
  308. SDL.init (InitFlag.VIDEO);
  309. Game game = new Game(cell_size,random_max);
  310. game.run();
  311. SDL.quit();
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement