-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.rb
161 lines (129 loc) · 2.52 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
require "ruby2d"
set background: "navy"
set fps_cap: 20
#width = 640/20 = 32
#height = 480/20 = 24
GRID_SIZE = 20
GRID_WIDTH = Window.width/ GRID_SIZE
GRID_HEIGHT = Window.height/ GRID_SIZE
class Snake
attr_writer :direction
def initialize
@positions = [[2, 0], [2, 1],[2, 2],[2, 3]]
@direction = 'down'
@growing = false
end
def draw
@positions.each do |position|
Square.new(x: position[0] * GRID_SIZE, y:position[1] * GRID_SIZE, size: GRID_SIZE - 1, color: 'white')
end
end
def move
if !@growing
@positions.shift
end
case @direction
when 'down'
@positions.push(new_coords(head[0], head[1] + 1))
when 'up'
@positions.push(new_coords(head[0], head[1] - 1))
when 'left'
@positions.push(new_coords(head[0] - 1, head[1]))
when 'right'
@positions.push(new_coords(head[0] + 1, head[1]))
end
@growing = false
end
def can_change_direction_to?(new_direction)
case @direction
when 'up' then new_direction != 'down'
when 'down' then new_direction != 'up'
when 'left' then new_direction != 'right'
when 'right' then new_direction != 'left'
end
end
def x
head[0]
end
def y
head[1]
end
def grow
@growing = true
end
def hit_itself?
@positions.uniq.length != @positions.length
end
private
def new_coords(x, y)
[x % GRID_WIDTH, y % GRID_HEIGHT]
end
private
def head
@positions.last
end
end
class Game
def initialize
@score = 0
@apple_x = rand(GRID_WIDTH)
@apple_y = rand(GRID_HEIGHT)
@finished = false
end
def draw
unless finished?
Square.new(x: @apple_x * GRID_SIZE, y:@apple_y * GRID_SIZE, size: GRID_SIZE, color: 'red')
end
Text.new(text_message, color: 'yellow', x: 10, y: 10, size: 25)
end
def snake_eat_food?(x,y)
@apple_x == x && @apple_y == y
end
def record_hit
@score += 1
@apple_x = rand(GRID_WIDTH)
@apple_y = rand(GRID_HEIGHT)
end
def finish
@finished = true
end
def finished?
@finished
end
private
def text_message
if finished?
"Game Over! Your Score was #{@score}. Press R to restart."
else
"Score: #{@score}"
end
end
end
snake = Snake.new
game = Game.new
update do
clear
unless game.finished?
snake.move
end
snake.draw
game.draw
if game.snake_eat_food?(snake.x, snake.y)
game.record_hit
snake.grow
end
if snake.hit_itself?
game.finish
end
end
on :key_down do |event|
if ['up', 'left', 'right', 'down'].include?(event.key)
if snake.can_change_direction_to?(event.key)
snake.direction = event.key
end
elsif event.key == 'r'
snake = Snake.new
game = Game.new
end
end
show