class Food
Constants
- DEFAULT_SYMBOL
Attributes
points[RW]
symbol[RW]
window[RW]
x[RW]
y[RW]
Public Class Methods
new(window, x = nil, y = nil)
click to toggle source
# File lib/snake_game/food.rb, line 6 def initialize(window, x = nil, y = nil) @window = window @x = x || generate_random_x @y = y || generate_random_y @symbol = DEFAULT_SYMBOL @points = 1 end
Public Instance Methods
generate_random_x()
click to toggle source
# File lib/snake_game/food.rb, line 25 def generate_random_x Random.rand(1..@window.width - 2) end
generate_random_y()
click to toggle source
# File lib/snake_game/food.rb, line 29 def generate_random_y Random.rand(1..@window.height - 2) end
has_been_eaten_by?(snake)
click to toggle source
# File lib/snake_game/food.rb, line 14 def has_been_eaten_by?(snake) snake[0][0] == @x && snake[0][1] == @y end
relocate_without_conflict!(snake)
click to toggle source
# File lib/snake_game/food.rb, line 18 def relocate_without_conflict!(snake) if snake.include? [@x, @y] @x = generate_random_x @y = generate_random_y end end