Class: LifeGameWindow

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

Overview

Keybinds: s - Start and stop (pause) the game c - Clear the grid r - Randomize the grid Left Click - Invert the state of the cell clicked q/Esc - Quit

Instance Method Summary collapse

Constructor Details

#initializeLifeGameWindow

Initialize Gosu window and LifeGrid



23
24
25
26
27
28
29
30
31
32
# File 'lib/NanoLife.rb', line 23

def initialize
  # Gosu window
  super WIN_WIDTH, WIN_HEIGHT, false, 1000.0 / MAX_FPS
  self.caption = 'NanoLife - Conway\'s Game of Life'
  # Create a game of life grid and start randomly
  @grid = LifeGrid.new(self)
  @grid.randomize
  # Set running to true. Game can be paused with keyboard
  @running = true
end

Instance Method Details

#button_down(id) ⇒ Object

Override callback for a button pressed



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/NanoLife.rb', line 50

def button_down(id)
  if id == Gosu::KbEscape or id  == Gosu::KbQ
    close
  elsif id == Gosu::KbS
    @running = !@running
  elsif id == Gosu::KbC
    @grid.clear
  elsif id == Gosu::KbR
      @grid.randomize
  elsif id == Gosu::MsLeft
    @grid.invert_cell(
      mouse_x.to_i / CELL_SIZE,
      mouse_y.to_i / CELL_SIZE
    )
  end
end

#drawObject

Draw screen



45
46
47
# File 'lib/NanoLife.rb', line 45

def draw
  @grid.draw
end

#needs_cursor?Boolean

Turn on cursor

Returns:

  • (Boolean)


35
# File 'lib/NanoLife.rb', line 35

def needs_cursor?; true; end

#updateObject

Update everything each frame before drawing



38
39
40
41
42
# File 'lib/NanoLife.rb', line 38

def update
  if @running # and delta is met
    @grid.update
  end
end