Sha256: 8f93a50aa489e4a5bcd28a5fb093ecd07ca43384baf89dd77de8260a62168728

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'fileutils'

require_relative 'snake'
require_relative 'apple'

class Snake
  module Model
    class Game
      WIDTH_DEFAULT = 40
      HEIGHT_DEFAULT = 40
      FILE_HIGH_SCORE = File.expand_path(File.join(Dir.home, '.glimmer-snake'))
      
      attr_reader :width, :height
      attr_accessor :snake, :apple, :over, :score, :high_score
      alias over? over
      # TODO implement scoring on snake eating apples
      
      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
      
      def score=(new_score)
        @score = new_score
        self.high_score = @score if @score > @high_score
      end
      
      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
      
      def start
        self.over = false
        self.score = 0
        self.snake.generate
        self.apple.generate
      end
      
      # inspect is overridden to prevent printing very long stack traces
      def inspect
        "#{super[0, 75]}... >"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
glimmer-dsl-libui-0.2.22 examples/snake/model/game.rb