-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.rb
104 lines (83 loc) · 1.53 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
require 'stucks'
java_import org.newdawn.slick.geom.Rectangle
class Snake
DELTA_BEFORE_ACTION = 40
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
def initialize game_zone
@game_zone = game_zone
init
end
def init
@stucks = Stucks.new @game_zone
@current_direction = RIGHT
@next_direction = RIGHT
@total_delta = 0
@new_tail = 0
end
def dir_up
@next_direction = UP if @current_direction != DOWN
end
def dir_down
@next_direction = DOWN if @current_direction != UP
end
def dir_left
@next_direction = LEFT if @current_direction != RIGHT
end
def dir_right
@next_direction = RIGHT if @current_direction != LEFT
end
def up
@stucks.new_up
end
def down
@stucks.new_down
end
def left
@stucks.new_left
end
def right
@stucks.new_right
end
def draw g
@stucks.draw g
end
def update delta
@total_delta += delta
if @total_delta > DELTA_BEFORE_ACTION
case @next_direction
when UP
up
when DOWN
down
when LEFT
left
when RIGHT
right
end
if new_tail?
@new_tail -= 1
@new_tail < 0 ? @new_tail = 0 : nil
else
@stucks.remove_last
end
@total_delta = 0
@current_direction = @next_direction
init if @stucks.colision?
end
end
def touch? thing
if @total_delta == 0
return true if @stucks.head.touch? thing
end
false
end
def new_tail
@new_tail += 1
end
def new_tail?
@new_tail > 0
end
end