@@ -18,7 +18,7 @@ function Snake:new()
18
18
self .queue = { initial_position }
19
19
self .movements_queue = {}
20
20
self .direction = " r" -- u d l r
21
- self .size = 3 -- snake size
21
+ self .size = 3 -- snake size
22
22
23
23
for _ = 1 , self .size do
24
24
self :move ()
@@ -61,10 +61,10 @@ function Snake:change_dir(dir)
61
61
local last_dir = self .movements_queue [# self .movements_queue ] or self .direction
62
62
63
63
if
64
- (new_dir == " l" and last_dir == " r" )
65
- or (new_dir == " r" and last_dir == " l" )
66
- or (new_dir == " u" and last_dir == " d" )
67
- or (new_dir == " d" and last_dir == " u" )
64
+ (new_dir == " l" and last_dir == " r" )
65
+ or (new_dir == " r" and last_dir == " l" )
66
+ or (new_dir == " u" and last_dir == " d" )
67
+ or (new_dir == " d" and last_dir == " u" )
68
68
then
69
69
return
70
70
end
@@ -73,13 +73,29 @@ function Snake:change_dir(dir)
73
73
end
74
74
75
75
function Snake :board_colision_check (height , width )
76
+ local enable_colision = self .size > 1 -- dosable colision
77
+
76
78
for _ , pos in ipairs (self .queue ) do
77
- if pos .x <= 0 or pos .x > width then
78
- return true
79
+ if not enable_colision then
80
+ -- if coliding with wall, game over
81
+ if pos .x <= 0 or pos .x > width then
82
+ return true
83
+ end
84
+
85
+ if pos .y <= 0 or pos .y > height then
86
+ return true
87
+ end
79
88
end
80
89
81
- if pos .y <= 0 or pos .y > height then
82
- return true
90
+ -- teleport to the other wall
91
+ pos .x = pos .x % width
92
+ if pos .x == 0 then
93
+ pos .x = width
94
+ end
95
+
96
+ pos .y = pos .y % height
97
+ if pos .y == 0 then
98
+ pos .y = height
83
99
end
84
100
end
85
101
0 commit comments