Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
Instance Method Summary collapse
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #play ⇒ Object
Constructor Details
#initialize ⇒ Game
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
#board ⇒ Object (readonly)
Returns the value of attribute board.
9 10 11 |
# File 'lib/game.rb', line 9 def board @board end |
Instance Method Details
#play ⇒ Object
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 |