class Snake::Model::Game

Constants

FILE_HIGH_SCORE
HEIGHT_DEFAULT
WIDTH_DEFAULT

Attributes

apple[RW]
height[R]
high_score[RW]
over[RW]
over?[RW]
paused[RW]
paused?[RW]
score[RW]
snake[RW]
width[R]

Public Class Methods

new(width = WIDTH_DEFAULT, height = HEIGHT_DEFAULT) click to toggle source
# File examples/snake/model/game.rb, line 18
def initialize(width = WIDTH_DEFAULT, height = HEIGHT_DEFAULT)
  @width = width
  @height = height
  @snake = Snake.new(self)
  @apple = Apple.new(self)
  FileUtils.touch(FILE_HIGH_SCORE)
  @high_score = File.read(FILE_HIGH_SCORE).to_i rescue 0
end

Public Instance Methods

high_score=(new_high_score) click to toggle source
# File examples/snake/model/game.rb, line 32
def high_score=(new_high_score)
  @high_score = new_high_score
  File.write(FILE_HIGH_SCORE, @high_score.to_s)
rescue => e
  puts e.full_message
end
inspect() click to toggle source

inspect is overridden to prevent printing very long stack traces

# File examples/snake/model/game.rb, line 63
def inspect
  "#{super[0, 75]}... >"
end
pause() click to toggle source
# File examples/snake/model/game.rb, line 46
def pause
  self.paused = true
end
resume() click to toggle source
# File examples/snake/model/game.rb, line 50
def resume
  self.paused = false
end
score=(new_score) click to toggle source
# File examples/snake/model/game.rb, line 27
def score=(new_score)
  @score = new_score
  self.high_score = @score if @score > @high_score
end
start() click to toggle source
# File examples/snake/model/game.rb, line 39
def start
  self.over = false
  self.score = 0
  self.snake.generate
  self.apple.generate
end
toggle_pause() click to toggle source
# File examples/snake/model/game.rb, line 54
def toggle_pause
  unless paused?
    pause
  else
    resume
  end
end