Class: RubyTerminalGames::Snake::Apple

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_terminal_games/snake/apple.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#heightObject (readonly)

Returns the value of attribute height.



4
5
6
# File 'lib/ruby_terminal_games/snake/apple.rb', line 4

def height
  @height
end

#positionObject (readonly)

Returns the value of attribute position.



4
5
6
# File 'lib/ruby_terminal_games/snake/apple.rb', line 4

def position
  @position
end

#widthObject (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