-
Notifications
You must be signed in to change notification settings - Fork 0
/
snek.lua
69 lines (61 loc) · 2.15 KB
/
snek.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
--[[
This file contain the function to show the snake
The snake is represented by a list of all the cases its body is on
and by the position of its head
The cases in the body are represented by their obsolute position
Dirrection from the head is when moving is showed like that:
1
2 4
3
]]
function createSnek(y, x)
local ret = {head = pos(y,x), body={}}
ret.show = function(snek)
nc.set_color(head.color)
nc.mvprintw(offset + snek.head.y - 1, offset + snek.head.x - 1, head.char)
for i=1,#snek.body do
nc.set_color(body.color)
nc.mvprintw(offset + snek.body[i].y - 1, offset + snek.body[i].x -1, body.char)
end
end
--the direction is relataive to the head as defined in the begining of the fine, the size of the snek is increased by one
ret.move = function(snek, direction, growth)
snek.body[#snek.body+1] = pos(nil,nil) --all cases of the body are diplaced by one
for i=0,#snek.body-2 do
snek.body[#snek.body-i]:clone(snek.body[#snek.body-i-1])
end
snek.body[1]:clone(snek.head)
if not caseDirection then --the head is moved
caseDirection = {
[1] = function(head); head.y = head.y - 1; end,
[2] = function(head); head.x = head.x - 1; end,
[3] = function(head); head.y = head.y + 1; end,
[4] = function(head); head.x = head.x + 1; end,
}
end
caseDirection[direction](snek.head)
end
--cut the last piece of the body of the snek
ret.cut = function(snek)
snek.body[#snek.body] = nil
end
--see if the snake is biting its tail or not
ret.biting = function(snek)
for i=1,#snek.body do
if snek.head:compare(snek.body[i]) then
return true
end
end
return false
end
--see if the coordonate of pos are on the snake
ret.isSnek = function(snek, pos)
for i=1,#snek.body do
if snek.body[i]:compare(pos) then
return true
end
end
return snek.head:compare(pos)
end
return ret
end