Class: RubyTerminalGames::Snake::Game
- Defined in:
- lib/ruby_terminal_games/snake/game.rb
Instance Attribute Summary collapse
-
#apple ⇒ Object
readonly
Returns the value of attribute apple.
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#counter ⇒ Object
readonly
Returns the value of attribute counter.
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#points ⇒ Object
readonly
Returns the value of attribute points.
-
#snake ⇒ Object
readonly
Returns the value of attribute snake.
-
#speed ⇒ Object
readonly
Returns the value of attribute speed.
Instance Method Summary collapse
- #about ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #play! ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ruby_terminal_games/snake/game.rb', line 11 def initialize @direction = RIGHT @board = Board.new @apple = Apple.new( width: @board.width, height: @board.height ) @snake = Snake.new( width: @board.width, height: @board.height ) @points = 0 @speed = 0 @counter = 0 end |
Instance Attribute Details
#apple ⇒ Object (readonly)
Returns the value of attribute apple.
8 9 10 |
# File 'lib/ruby_terminal_games/snake/game.rb', line 8 def apple @apple end |
#board ⇒ Object (readonly)
Returns the value of attribute board.
8 9 10 |
# File 'lib/ruby_terminal_games/snake/game.rb', line 8 def board @board end |
#counter ⇒ Object (readonly)
Returns the value of attribute counter.
8 9 10 |
# File 'lib/ruby_terminal_games/snake/game.rb', line 8 def counter @counter end |
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
8 9 10 |
# File 'lib/ruby_terminal_games/snake/game.rb', line 8 def direction @direction end |
#points ⇒ Object (readonly)
Returns the value of attribute points.
8 9 10 |
# File 'lib/ruby_terminal_games/snake/game.rb', line 8 def points @points end |
#snake ⇒ Object (readonly)
Returns the value of attribute snake.
8 9 10 |
# File 'lib/ruby_terminal_games/snake/game.rb', line 8 def snake @snake end |
#speed ⇒ Object (readonly)
Returns the value of attribute speed.
8 9 10 |
# File 'lib/ruby_terminal_games/snake/game.rb', line 8 def speed @speed end |
Instance Method Details
#about ⇒ Object
48 49 50 51 52 53 |
# File 'lib/ruby_terminal_games/snake/game.rb', line 48 def about [ "Remember the good old times. Use the keyboard ", "arrows to control the snake." ].join end |
#play! ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ruby_terminal_games/snake/game.rb', line 30 def play! @playing = true Keyboard.capture(detect_direction: true) do |key| exit if key =~ /q/i next unless direction_allowed?(key) @direction = key end while @playing eat? snake.move!(direction) snake.died? and @playing = false game_interval! board.print_world!(self) end end |