Class: GameOfLife::GameOfLifeWindow

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/game_of_life.rb

Instance Method Summary collapse

Constructor Details

#initialize(width = 640, height = 480) ⇒ GameOfLifeWindow

Returns a new instance of GameOfLifeWindow.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/game_of_life.rb', line 9

def initialize(width=640, height=480)
  @width, @height = width, height
  super(@width, @height, false)
  self.caption = 'The Game of Life'

  # Variables
  @num_columns = @width/9
  @num_rows = @height/9
  @column_width = @width/@num_columns
  @row_height = @height/@num_rows

  @white_color = Gosu::Color.new(0xffffffff)
  @black_color = Gosu::Color.new(0xff000000)
  @dead_color = Gosu::Color.new(0xff808080)

  @world = World.new(@num_columns,@num_rows)
  @world.seed!
end

Instance Method Details

#drawObject



32
33
34
# File 'lib/game_of_life.rb', line 32

def draw
  draw_cells
end

#draw_cellsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/game_of_life.rb', line 40

def draw_cells
  @world.cells.each do |cell|
    if cell.alive?
      draw_quad(
        cell.x * @column_width, cell.y * @row_height, @white_color,
        cell.x * @column_width + @column_width, cell.y * @row_height, @white_color,
        cell.x * @column_width + @column_width, cell.y * @row_height + @row_height, @white_color,
        cell.x * @column_width, cell.y * @row_height + @row_height, @white_color
      )
    else
      draw_quad(
        cell.x * @column_width, cell.y * @row_height, @black_color,
        cell.x * @column_width + @column_width, cell.y * @row_height, @black_color,
        cell.x * @column_width + @column_width, cell.y * @row_height + @row_height, @black_color,
        cell.x * @column_width, cell.y * @row_height + @row_height, @black_color
      )
    end
  end
end

#needs_cursor?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/game_of_life.rb', line 36

def needs_cursor?
  true
end

#updateObject



28
29
30
# File 'lib/game_of_life.rb', line 28

def update
  @world.rotate!
end