-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.lua
72 lines (62 loc) · 1.69 KB
/
snake.lua
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
local snake = {}
snake.x = 25
snake.y = 25
snake.dir_x = 0
snake.dir_y = 0
snake.tail_length = 0
snake.tail = {}
snake.cords = {}
function snake.changeDir(new_x, new_y)
snake.dir_x = new_x
snake.dir_y = new_y
end
function snake.tick(map)
old_x = snake.x
old_y = snake.y
snake.x = snake.x + snake.dir_x
snake.y = snake.y + snake.dir_y
if snake.x > map.rows - 1 then
snake.x = 0
elseif snake.x < 0 then
snake.x = map.rows - 1
elseif snake.y > map.columns - 1 then
snake.y = 0
elseif snake.y < 0 then
snake.y = map.columns - 1
end
if snake.tail_length > 0 then
for _, cords in ipairs(snake.tail) do
--print(cords[1],cords[2])
snake.cords = cords
--print(snake.cords[1])
local body_X, body_Y = cords[1], cords[2]
cords[1], cords[2] = old_x, old_y
old_x, old_y = body_X, body_Y
end
end
end
function snake.draw(map)
love.graphics.setColor(1, 0, 0) --Head Snake Color
love.graphics.rectangle("fill", snake.x * map.cellSize, snake.y * map.cellSize, map.cellSize, map.cellSize, 5, 5) -- Snake body
love.graphics.setColor(0.5, 0, 0)
for _, value in ipairs(snake.tail) do
love.graphics.rectangle(
"fill",
value[1] * map.cellSize,
value[2] * map.cellSize,
map.cellSize - 2,
map.cellSize - 2,
5,
5
)
end
end
function snake.restart()
snake.x = 25
snake.y = 25
snake.dir_x = 0
snake.dir_y = 0
snake.tail = {}
snake.tail_length = 0
end
return snake