Guest User

Untitled

a guest
May 27th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1.  
  2. class GameOfLife < Chingu::GameState
  3. CELL_SIZE = 4
  4. @@tick =0
  5. def initialize
  6. super
  7. @grid = generate_grid
  8. self.input={:left_mouse_button=>:set_grid_block,:space=>:toggle_running,
  9. :right_mouse_button=>:unset_grid_block,:z=>:reset,:n=>:update_grid}
  10. @running = false
  11.  
  12. end
  13.  
  14. def update
  15. super
  16. update_grid if @running
  17.  
  18. $window.caption = "conway generation #{@@tick}"
  19. end
  20.  
  21. def draw
  22. super
  23. draw_grid
  24. draw_mouse
  25. end
  26.  
  27. private
  28.  
  29.  
  30. def generate_grid
  31. width = $window.width/CELL_SIZE
  32. height = $window.height/CELL_SIZE
  33.  
  34. grid = Array.new(width)
  35. col = Array.new(height)
  36. col.map!{false}
  37. grid.map!{Array.new(col)}
  38. grid
  39. end
  40.  
  41. def draw_grid
  42. @grid.each_with_index do |a,x|
  43. a.each_with_index do |c,y|
  44. if c === true
  45. $window.fill_rect([x*CELL_SIZE,y*CELL_SIZE,CELL_SIZE,CELL_SIZE],0xff000000,0)
  46. end
  47. end
  48. end
  49. end
  50.  
  51. def reset
  52. @grid = generate_grid
  53. @@tick =0
  54. @running = false
  55. end
  56.  
  57. def update_grid
  58. @new_grid = Marshal.load(Marshal.dump(@grid))
  59.  
  60. @grid.each_with_index do |a,x|
  61. a.each_with_index do |c,y|
  62. minus_x =x-1
  63. minus_y = y-1
  64. plus_x = x+1
  65. plus_y = y+1
  66. minus_x = @grid.length-1 if minus_x <0
  67. minus_y = a.length-1 if minus_y <0
  68. plus_y = 0 if plus_y >= a.length
  69. plus_x = 0 if plus_x >= @grid.length
  70.  
  71. live_neighbours = 0
  72.  
  73. @grid[minus_x][y] == true ? live_neighbours+=1 : nil
  74. @grid[plus_x][y] == true ? live_neighbours+=1 : nil
  75. @grid[x][minus_y] == true ? live_neighbours+=1 : nil
  76. @grid[x][plus_y] == true ? live_neighbours+=1 : nil
  77. @grid[minus_x][plus_y] == true ? live_neighbours+=1 : nil
  78. @grid[plus_x][minus_y] == true ? live_neighbours+=1 : nil
  79. @grid[minus_x][minus_y] == true ? live_neighbours+=1 : nil
  80. @grid[plus_x][plus_y] == true ? live_neighbours+=1 : nil
  81.  
  82. case live_neighbours
  83. when 0..1 then @new_grid[x][y] = false
  84. when 2 then @new_grid[x][y] = true if @new_grid[x][y] == true
  85. when 3 then @new_grid[x][y] = true
  86. when 4..8 then @new_grid[x][y] = false
  87. end
  88.  
  89. end
  90. end
  91.  
  92. @grid = @new_grid
  93. @@tick+=1
  94. end
  95.  
  96. def toggle_running
  97. @running = !@running
  98. end
  99.  
  100. def set_grid_block
  101.  
  102. x = ($window.mouse_x/CELL_SIZE).floor
  103. y = ($window.mouse_y/CELL_SIZE).floor
  104.  
  105. @grid[x][y]=true
  106.  
  107. end
  108.  
  109. def unset_grid_block
  110. x = ($window.mouse_x/CELL_SIZE).floor
  111. y = ($window.mouse_y/CELL_SIZE).floor
  112.  
  113. @grid[x][y]=false
  114. end
  115. def draw_mouse
  116. $window.fill_rect([($window.mouse_x/CELL_SIZE).floor*CELL_SIZE,($window.mouse_y/CELL_SIZE).floor*CELL_SIZE,CELL_SIZE,CELL_SIZE],0xaa0000ff,0)
  117. end
  118. end
Add Comment
Please, Sign In to add comment