Class: Game

Inherits:
Object
  • Object
show all
Defined in:
lib/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



10
11
12
13
# File 'lib/game.rb', line 10

def initialize
  @board = Board.new
  @snake = Snake.new(board.initialize_snake)
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



9
10
11
# File 'lib/game.rb', line 9

def board
  @board
end

Instance Method Details

#playObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/game.rb', line 15

def play
  food = board.random_position
  Kernel.loop do
    change = Curses.getch
    snake.process_direction(change)
    begin
      food = snake.tick(food)
      raise unless board.is_legal?(snake.positions)
    rescue IllegalStateError
      raise 'You died!'
    end
    food ||= board.random_position
    board.draw(snake: snake, fruit: food)
    sleep 0.1
  end
end