Class: RubyTerminalGames::Snake::Apple
- Inherits:
-
Object
- Object
- RubyTerminalGames::Snake::Apple
- Defined in:
- lib/ruby_terminal_games/snake/apple.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#initialize(width:, height:) ⇒ Apple
constructor
A new instance of Apple.
- #move(snake) ⇒ Object
Constructor Details
#initialize(width:, height:) ⇒ Apple
5 6 7 8 9 |
# File 'lib/ruby_terminal_games/snake/apple.rb', line 5 def initialize(width:, height:) @width = width @height = height @position = [height - 4, width - 4] end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
4 5 6 |
# File 'lib/ruby_terminal_games/snake/apple.rb', line 4 def height @height end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
4 5 6 |
# File 'lib/ruby_terminal_games/snake/apple.rb', line 4 def position @position end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
4 5 6 |
# File 'lib/ruby_terminal_games/snake/apple.rb', line 4 def width @width end |
Instance Method Details
#move(snake) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/ruby_terminal_games/snake/apple.rb', line 11 def move(snake) begin moved = [ rand(height - 4) + 2, rand(width - 4) + 2 ] end while snake.used?(moved) @position = moved end |