-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
155 lines (129 loc) · 3.69 KB
/
main.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
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
TILESIZE = 10
SPEED = 20
SPEEDINV = 1/SPEED
MAXWIDTH = math.floor(love.graphics.getWidth() / TILESIZE)
MAXHEIGHT = math.floor(love.graphics.getHeight() / TILESIZE)
accumulatedDelta = 0
math.randomseed(os.time())
-- gotta do this to make stuff actually random -_-
math.random()
function getRandomCoord()
xCoord = math.random(1, MAXWIDTH)
yCoord = math.random(1, MAXHEIGHT)
if x == MAXWIDTH/2 and y == MAXHEIGHT/2 then
coord = getRandomCoord()
else
coord = {x = xCoord, y = yCoord}
end
return coord
end
apple = getRandomCoord()
snake = {
velocity = {x = 0, y = -1},
body = {},
length = 10,
trudCount = 0
}
truds = {}
function love.load()
love.graphics.setBackgroundColor(0,0,0)
end
function love.keypressed(key)
if love.keyboard.isDown("left") and not(snake.velocity.x == 1)then
snake.velocity = {x = -1, y = 0}
end
if love.keyboard.isDown("right") and not(snake.velocity.x == -1) then
snake.velocity = {x = 1, y = 0}
end
if love.keyboard.isDown("up") and not(snake.velocity.y == 1) then
snake.velocity = {x = 0, y = -1}
end
if love.keyboard.isDown("down") and not(snake.velocity.y == -1) then
snake.velocity = {x = 0, y = 1}
end
end
function love.update(dt)
if accumulatedDelta < SPEEDINV then --if we aren't at an update yet, accumulate the time
accumulatedDelta = accumulatedDelta + dt
else --otherwise, move the snake forward, remove the last, reset the accumulated time
if #snake.body == 0 then
table.insert(snake.body, {x = MAXWIDTH / 2 , y = MAXHEIGHT / 2 })
else
bodySquare = snake.body[#snake.body]
newCoord = {x = bodySquare.x + snake.velocity.x, y = bodySquare.y + snake.velocity.y}
if isValid(newCoord) then --handle snake building and trud creating
table.insert(snake.body, newCoord)
if #snake.body > snake.length then
if snake.trudCount > 0 then
table.insert(truds, snake.body[1])
snake.trudCount = snake.trudCount - 1
end
table.remove(snake.body, 1)
end
--then handle item events (trud eating, to be clear)
if (apple and isOverlapping(newCoord, apple)) then
apple = nil
snake.length = snake.length + 5
snake.trudCount = snake.trudCount + 5
end
for i, trud in ipairs(truds) do
if isOverlapping(newCoord, trud) then
table.remove(truds, i)
snake.length = snake.length + 5
snake.trudCount = snake.trudCount + 5
break
end
end
else
print('ur ded')--todo make dead
love.event.quit()
end
end
accumulatedDelta = 0
end
end
function love.draw( )
--draw apple
if apple then
love.graphics.setColor(255, 0, 0)
love.graphics.rectangle("fill", apple.x * TILESIZE, apple.y * TILESIZE, TILESIZE, TILESIZE)
end
--draw snake
for i, bodySeg in ipairs(snake.body) do
if i == #snake.body then
love.graphics.setColor(224, 109, 27)
else
love.graphics.setColor(0, 255, 0)
end
love.graphics.rectangle( "fill", bodySeg.x * TILESIZE, bodySeg.y * TILESIZE, TILESIZE, TILESIZE)
end
--draw truds
love.graphics.setColor(156, 93, 26)
for _, trud in ipairs(truds) do
love.graphics.rectangle( "fill", trud.x * TILESIZE, trud.y * TILESIZE, TILESIZE, TILESIZE)
end
end
function isValid(coord)
if coord.x > MAXWIDTH or coord.x < 0 or coord.y > MAXHEIGHT or coord.y < 0 then --bounds check
return false
else
--self consumption check
for _, bodySeg in ipairs(snake.body) do
if isOverlapping(coord, bodySeg) then
return false
end
end
return true
end
end
function isOverlapping(coord1, coord2)
if coord1.x == coord2.x and coord1.y == coord2.y then
return true
else
return false
end
end
--for debug
function printCoord(coord)
print("x= " .. coord.x .. " y= " .. coord.y)
end