-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosu_game_of_life.rb
65 lines (52 loc) · 1.81 KB
/
gosu_game_of_life.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Gosu File
require 'gosu'
require_relative 'game_of_life.rb'
class GameOfLifeWindow < Gosu::Window
def initialize(height = 1200, width = 800)
@height = height
@width = width
super height, width, false
self.caption = 'Our Game of Life'
# Color
@background_color = Gosu::Color.new(0xffdedede)
@alive_color = Gosu::Color.new(0xff121212)
@dead_color = Gosu::Color.new(0xffededed)
# Game itself
@cols = width / 10
@rows = height / 10
@col_width = width / @cols
@row_height = height / @rows
@world = World.new(@cols, @rows)
@game = Game.new(@world)
@game.world.randomly_populate
end
def update
update_interval = 1000
@game.tick!
end
def draw
# Background
draw_quad(0, 0, @background_color,
width, 0, @background_color,
width, height, @background_color,
0, height, @background_color)
# Drawing cells
@game.world.cells.each do |cell|
if cell.alive?
draw_quad(cell.x * @col_width, cell.y * @row_height, @alive_color,
cell.x * @col_width + (@col_width - 1), cell.y * @row_height, @alive_color,
cell.x * @col_width + (@col_width - 1), cell.y * @row_height + (@row_height - 1), @alive_color,
cell.x * @col_width, cell.y * @row_height + (@row_height - 1), @alive_color)
else
draw_quad(cell.x * @col_width, cell.y * @row_height, @dead_color,
cell.x * @col_width + (@col_width - 1), cell.y * @row_height, @dead_color,
cell.x * @col_width + (@col_width - 1), cell.y * @row_height + (@row_height - 1), @dead_color,
cell.x * @col_width, cell.y * @row_height + (@row_height - 1), @dead_color)
end
end
end
def needs_cursor?
true
end
end
GameOfLifeWindow.new.show