-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.rb
206 lines (163 loc) · 4.11 KB
/
snake.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
require 'gosu'
class Snake
attr_accessor :direction, :xpos, :ypos, :speed, :length, :segments, :ticker
def initialize(window)
@window = window
@xpos = 200
@ypos = 200
@segments = []
@direction = "right"
@head_segment = Segment.new(self, @window, [@xpos, @ypos])
@segments.push(@head_segment)
@speed = 2
@length = segments.length
@ticker = 0
end
def draw
@segments.each do |s|
s.draw
end
end
def update_position
add_segment
@segments.shift(1) unless @ticker > 0
end
def add_segment
if @direction == "left"
xpos = @head_segment.xpos - @speed
ypos = @head_segment.ypos
new_segment = Segment.new(self, @window, [xpos, ypos])
end
if @direction == "right"
xpos = @head_segment.xpos + @speed
ypos = @head_segment.ypos
new_segment = Segment.new(self, @window, [xpos, ypos])
end
if @direction == "up"
xpos = @head_segment.xpos
ypos = @head_segment.ypos - @speed
new_segment = Segment.new(self, @window, [xpos, ypos])
end
if @direction == "down"
xpos = @head_segment.xpos
ypos = @head_segment.ypos + @speed
new_segment = Segment.new(self, @window, [xpos, ypos])
end
@head_segment = new_segment
@segments.push(@head_segment)
end
def ate_apple?(apple)
if Gosu::distance(@head_segment.xpos, @head_segment.ypos, apple.xpos, apple.ypos) < 10
return true
end
end
def hit_self?
segments = Array.new(@segments)
if segments.length > 21
segments.pop((10 * @speed))
segments.each do |s|
if Gosu::distance(@head_segment.xpos, @head_segment.ypos, s.xpos, s.ypos) < 11
puts "true, head: #{@head_segment.xpos}, #{@head_segment.ypos}; seg: #{s.xpos}, #{s.ypos}"
return true
else
next
end
end
return false
end
end
def outside_bounds?
if @head_segment.xpos < 0 or @head_segment.xpos > 630
return true
elsif @head_segment.ypos < 0 or @head_segment.ypos > 470
return true
else
return false
end
end
end
class Segment
attr_accessor :xpos, :ypos
def initialize(snake, window, position)
@window = window
@xpos = position[0]
@ypos = position[1]
end
def draw
@window.draw_quad(@xpos,@ypos,Gosu::Color::GREEN,@xpos + 10,@ypos,Gosu::Color::GREEN,@xpos,@ypos + 10,Gosu::Color::GREEN,@xpos + 10,@ypos + 10,Gosu::Color::GREEN)
end
end
class Apple
attr_reader :xpos, :ypos
def initialize(window)
@window = window
@xpos = rand(10..630)
@ypos = rand(50..470)
end
def draw
@window.draw_quad(@xpos,@ypos,Gosu::Color::RED,@xpos,@ypos + 10,Gosu::Color::RED,@xpos + 10,@ypos,Gosu::Color::RED,@xpos + 10,@ypos + 10, Gosu::Color::RED)
end
end
class GameWindow < Gosu::Window
def initialize
super 640, 480, false
self.caption = "Snake"
@snake = Snake.new(self)
@apple = Apple.new(self)
@score = 0
@text_object = Gosu::Font.new(self, 'Ubuntu Sans', 32)
end
def update
if button_down? Gosu::KbLeft and @snake.direction != "right"
@snake.direction = "left"
end
if button_down? Gosu::KbRight and @snake.direction != "left"
@snake.direction = "right"
end
if button_down? Gosu::KbUp and @snake.direction != "down"
@snake.direction = "up"
end
if button_down? Gosu::KbDown and @snake.direction != "up"
@snake.direction = "down"
end
if button_down? Gosu::KbEscape
self.close
end
if @snake.ate_apple?(@apple)
@apple = Apple.new(self)
@score += 10
@snake.length += 10
@snake.ticker += 11
if @score % 100 == 0
@snake.speed += 0.5
end
end
if @snake.hit_self?
@new_game = Gosu::Font.new(self, 'Ubuntu Sans', 32)
end
if @snake.outside_bounds?
@new_game = Gosu::Font.new(self, 'Ubuntu Sans', 32)
end
if @new_game and button_down? Gosu::KbReturn
@new_game = nil
@score = 0
@snake = Snake.new(self)
@apple = Apple.new(self)
end
@snake.ticker -= 1 if @snake.ticker > 0
end
def draw
if @new_game
@new_game.draw("Your Score was #{@score}", 5, 200, 100)
@new_game.draw("Press Return to Try Again", 5, 250, 100)
@new_game.draw("Or Escape to Close", 5, 300, 100)
else
@snake.update_position
@snake.draw
@apple.draw
@text_object.draw("Score: #{@score}",5,5,0)
end
end
end
window = GameWindow.new
window.show