-
Notifications
You must be signed in to change notification settings - Fork 7
/
snake.lua
211 lines (185 loc) · 4.82 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
-------------------------------------------------------------
-- Classic snake game
--
-- 2009 Led Lab @PUC-Rio www.eluaproject.net
-- Dado Sutter
-- Ives Negreiros
-- Téo Benjamin
-------------------------------------------------------------
local xMax = math.floor( 128 / 6 ) - 1
local yMax = math.floor( 96 / 8 ) - 1
local game_map = {}
local Head = {}
local Tail = {}
local highscore = 0
local size = 3
Tail.x = 1
Tail.y = 1
Head.x = Tail.x + ( size - 1 )
Head.y = Tail.y
local Food = {}
Food.x = false
Food.y = false
Head.dx = 1
Head.dy = 0
Tail.dx = Head.dx
Tail.dy = Head.dy
local direction = "right"
local level = 1
local score = 0
lm3s.disp.init( 1000000 )
local kit = require( pd.board() )
local pressed = {}
local function create_food()
-- if not food then
Food.x, Food.y = math.random( xMax - 1), math.random( yMax - 1)
while game_map[ Food.x ][ Food.y ] do
Food.x, Food.y = math.random( xMax - 1 ), math.random( yMax - 1 )
end
game_map[ Food.x ][ Food.y ] = "food"
lm3s.disp.print( "@", Food.x * 6, Food.y * 8, 10 )
-- end
end
local function eat_food()
lm3s.disp.print( "@", Head.x * 6, Head.y * 8, 0 )
game_map[ Head.x ][ Head.y ] = nil
create_food()
score = score + level
end
local function check_collision()
if Head.x <= 0 or Head.x >= xMax then
return true
elseif Head.y <= 0 or Head.y >= yMax then
return true
elseif ( ( game_map[ Head.x ][ Head.y ] ) and ( game_map[ Head.x ][ Head.y ] ~= "food" ) ) then
return true
end
return false
end
local function move()
if game_map[ Tail.x ][ Tail.y ] == "right" then
Tail.dx = 1
Tail.dy = 0
elseif game_map[ Tail.x ][ Tail.y ] == "left" then
Tail.dx = -1
Tail.dy = 0
elseif game_map[ Tail.x ][ Tail.y ] == "up" then
Tail.dx = 0
Tail.dy = -1
elseif game_map[ Tail.x ][ Tail.y ] == "down" then
Tail.dx = 0
Tail.dy = 1
end
game_map[ Head.x ][ Head.y ] = direction
Head.x = Head.x + Head.dx
Head.y = Head.y + Head.dy
if game_map[ Head.x ][ Head.y ] == "food" then
eat_food()
else
lm3s.disp.print( "*", Tail.x * 6, Tail.y * 8, 0 )
game_map[ Tail.x ][ Tail.y ] = nil
Tail.x = Tail.x + Tail.dx
Tail.y = Tail.y + Tail.dy
end
lm3s.disp.print( "*", Head.x * 6, Head.y * 8, 10 )
end
local function draw_walls()
for i = 0, xMax*2, 1 do
lm3s.disp.print( "_", i * 3, yMax * 8 - 6, 11 )
lm3s.disp.print( "_", i * 3, 0, 11 )
end
for i = 0, yMax*2, 1 do
lm3s.disp.print( "|", xMax * 6, i * 4, 11 )
lm3s.disp.print( "|", 0, i * 4, 11 )
end
end
local function button_clicked( button )
if kit.btn_pressed( button ) then
pressed[ button ] = true
else
if pressed[ button ] then
pressed[ button ] = nil
return true
end
end
return false
end
function init()
food = false
lm3s.disp.clear()
draw_walls()
size = 3
score = 0
level = 1
Tail.x = 1
Tail.y = 1
Head.x = Tail.x + ( size - 1 )
Head.y = Tail.y
Head.dx = 1
Head.dy = 0
Tail.dx = Head.dx
Tail.dy = Head.dy
direction = "right"
for i = 0, xMax, 1 do
game_map[ i ] = {}
end
for i = 0, size - 1, 1 do
game_map[ Tail.x + ( i * Tail.dx ) ][ Tail.y + ( i * Tail.dy ) ] = direction
lm3s.disp.print( "*", ( Tail.x + ( i * Tail.dx ) ) * 6, ( Tail.y + ( i * Tail.dy ) ) * 8, 10 )
end
create_food()
end
--init()
--create_food()
repeat
init()
while true do
local dir = direction
for i = 1, 1000 - ( 100 * level ), 1 do
if kit.btn_pressed( kit.BTN_RIGHT ) and direction ~= "left" then
dir = "right"
Head.dx = 1
Head.dy = 0
end
if kit.btn_pressed( kit.BTN_LEFT ) and direction ~= "right" then
dir = "left"
Head.dx = -1
Head.dy = 0
end
if kit.btn_pressed( kit.BTN_UP ) and direction ~= "down" then
dir = "up"
Head.dx = 0
Head.dy = -1
end
if kit.btn_pressed( kit.BTN_DOWN ) and direction ~= "up" then
dir = "down"
Head.dx = 0
Head.dy = 1
end
if button_clicked( kit.BTN_SELECT ) and level < 10 then
level = level + 1
end
end
direction = dir
move()
if check_collision() then break end
collectgarbage( "collect" )
end
if score > highscore then
highscore = score
end
lm3s.disp.clear() -- This statements displays the game over screen
lm3s.disp.print( "Game Over :(", 30, 20, 11 )
lm3s.disp.print( "Your score was "..tostring( score ), 0, 40, 11 )
lm3s.disp.print( "Highscore: "..tostring( highscore ), 15, 50, 11 )
lm3s.disp.print( "SELECT to restart", 6, 70, 11 )
enough = true -- If the player presses select before the time reach 1000000ms, then restart the game
for i=1, 1000000 do
if kit.btn_pressed( kit.BTN_SELECT ) then
enough = false
break
end
end
lm3s.disp.clear()
until ( enough )
lm3s.disp.off()