-
Notifications
You must be signed in to change notification settings - Fork 16
/
game.rb
68 lines (57 loc) · 1.53 KB
/
game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'fileutils'
require_relative 'snake'
require_relative 'apple'
class Snake
module Model
class Game
WIDTH_DEFAULT = 20
HEIGHT_DEFAULT = 20
FILE_HIGH_SCORE = File.expand_path(File.join(Dir.home, '.glimmer-snake'))
attr_reader :width, :height
attr_accessor :snake, :apple, :over, :score, :high_score, :paused
alias over? over
alias paused? paused
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
def pause
self.paused = true
end
def resume
self.paused = false
end
def toggle_pause
unless paused?
pause
else
resume
end
end
# inspect is overridden to prevent printing very long stack traces
def inspect
"#{super[0, 75]}... >"
end
end
end
end