/
snake.lua
411 lines (349 loc) · 12.6 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
--ANSI Snake game in Lua
--By RamiLego4Game (Rami Sabbagh)
--== ANSI Interface ==--
--Reference: http://ascii-table.com/ansi-escape-sequences.php
local ANSI = {}
do
--Set the cursor position
--[[Moves the cursor to the specified position (coordinates).
If you do not specify a position, the cursor moves to the home position at the upper-left
corner of the screen (line 0, column 0). This escape sequence works the same way as the
following Cursor Position escape sequence.]]
local fCursorPosition = "\27[%d;%dH"
function ANSI.setCursorPosition(x, y)
io.write(string.format(fCursorPosition, y, x))
end
--Move the cursor up
--[[Moves the cursor up by the specified number of lines without changing columns. If the
cursor is already on the top line, ANSI.SYS ignores this sequence.]]
local fCursorUp = "\27[%dA"
function ANSI.moveCursorUp(amount)
io.write(string.format(fCursorUp, amount or 1))
end
--Move the cursor down
--[[Moves the cursor down by the specified number of lines without changing columns. If the
cursor is already on the bottom line, ANSI.SYS ignores this sequence.]]
local fCursorDown = "\27[%dB"
function ANSI.moveCursorDown(amount)
io.write(string.format(fCursorDown, amount or 1))
end
--Move the cursor forward
--[[Moves the cursor forward by the specified number of columns without changing lines. If
the cursor is already in the rightmost column, ANSI.SYS ignores this sequence.]]
local fCursorForward = "\27[%dC"
function ANSI.moveCursorForward(amount)
io.write(string.format(fCursorForward, amount or 1))
end
--Move the cursor backward
--[[Moves the cursor back by the specified number of columns without changing lines. If the
cursor is already in the leftmost column, ANSI.SYS ignores this sequence.]]
local fCursorBackward = "\27[%dD"
function ANSI.moveCursorBackward(amount)
io.write(string.format(fCursorBackward, amount or 1))
end
--Save the cursor position
--[[Saves the current cursor position. You can move the cursor to the saved cursor position
by using the Restore Cursor Position sequence.]]
function ANSI.saveCursorPosition() io.write("\27[s") end
--Restore the cursor position
--[[Returns the cursor to the position stored by the Save Cursor Position sequence.]]
function ANSI.restoreCursorPosition() io.write("\27[u") end
--Erase display
--[[Clears the screen and moves the cursor to the home position (line 0, column 0).]]
function ANSI.eraseDisplay() io.write("\27[2J") end
--Erase line
--[[Clears all characters from the cursor position to the end of the line (including the
character at the cursor position).]]
function ANSI.eraseLine() io.write("\27[K") end
--Sets the graphics mode
--[[Calls the graphics functions specified by the following values. These specified functions
remain active until the next occurrence of this escape sequence. Graphics mode changes
the colors and attributes of text (such as bold and underline) displayed on the screen.]]
--[[
Text attributes
0 All attributes off
1 Bold on
4 Underscore (on monochrome display adapter only)
5 Blink on
7 Reverse video on
8 Concealed on
Foreground colors
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White
Background colors
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White
]]
local fSetGraphicsMode = "\27[%sm"
function ANSI.setGraphicsMode(...)
local modes = {...}
for k,v in pairs(modes) do modes[k] = tostring(v) end
io.write(string.format(fSetGraphicsMode, table.concat(modes, ";")))
end
--Set mode
--[[Changes the screen width or type to the mode specified by one of the following values:]]
--[[
Screen resolution
0 40 x 25 monochrome (text)
1 40 x 25 color (text)
2 80 x 25 monochrome (text)
3 80 x 25 color (text)
4 320 x 200 4-color (graphics)
5 320 x 200 monochrome (graphics)
6 640 x 200 monochrome (graphics)
7 Enables line wrapping
13 320 x 200 color (graphics)
14 640 x 200 color (16-color graphics)
15 640 x 350 monochrome (2-color graphics)
16 640 x 350 color (16-color graphics)
17 640 x 480 monochrome (2-color graphics)
18 640 x 480 color (16-color graphics)
19 320 x 200 color (256-color graphics)
]]
local fSetMode = "\27[=%dh"
function ANSI.setMode(mode)
io.write(string.format(fSetMode, mode))
end
--Reset mode
--[[Resets the mode by using the same values that Set Mode uses, except for 7, which
disables line wrapping]]
local fResetMode = "\27[=%dl"
function ANSI.resetMode(mode)
io.write(string.format(fResetMode, mode))
end
--Set keyboard strings
--[[Redefines a keyboard key to a specified string.]]
local fSetKeyboardStrings = "\27[%sm"
function ANSI.setKeybaordStrings(...)
local keys = {...}
for k,v in pairs(keys) do keys[k] = tostring(v) end
io.write(string.format(fSetKeyboardStrings, table.concat(keys, ";")))
end
end
--== Load external libraries ==--
local curses = require("curses")
local sleep = require("socket").sleep
--== Snake Game ==--
local Window = curses.initscr()
local TWidth, THeight = curses.cols(), curses.lines()
local MWidth, MHeight = TWidth, THeight-1
local TickTime = 0.05 --The time between each movement tick
local HorizentalTick = TickTime --Tick time when moving horizentally
local VerticalTick = HorizentalTick*2 --Tick time when moving vertically
local StartTime = os.time() --The time when the game started
local SnakePieces = {} --A table containing sub-tables which are the coordinations of each snake piece
local SnakeDirection = 1 --0: Up, 1: Right, 2: Down, 3: Left
local InitialFruits = 6 --The count of fruits to spawn at start
local Fruits = {} --A table containing sub-tables which are the coordinates and the id of each fruit.
local AvailableFruits = {
{"*", 32, 41}, --Apple, Green FG, Red BG
{"'", 30, 43}, --Banana
{"*", 32, 45} --Eggplant
}
local Dead = false --If the snake is dead or not
local function DirectionToVector(direction)
if direction == 0 then return 0, -1
elseif direction == 1 then return 1, 0
elseif direction == 2 then return 0, 1
elseif direction == 3 then return -1, 0
else return 0,0 end
end
--Draw terminal background
local function DrawBackground()
ANSI.setCursorPosition(1, 1)
ANSI.setGraphicsMode(0, 1, Dead and 31 or 30, 40)
local lineString = string.rep(".", MWidth)
for y=1, MHeight do
ANSI.setCursorPosition(0, y)
io.write(lineString)
end
end
--Draw the snake
local function DrawSnake()
ANSI.setGraphicsMode(0, 1, 42)
for k, piece in pairs(SnakePieces) do
ANSI.setCursorPosition(piece[1], piece[2])
io.write(" ")
end
end
--Draw the fruits
local function DrawFruits()
for id, Fruit in pairs(Fruits) do
ANSI.setCursorPosition(Fruit[1], Fruit[2])
local fType = Fruit[3]
local fInfo = AvailableFruits[fType]
ANSI.setGraphicsMode(1, fInfo[2], fInfo[3])
io.write(fInfo[1])
end
end
--Draw the info bar
local function DrawInfoBar()
ANSI.setGraphicsMode(0, 1, 33, 40)
ANSI.setCursorPosition(1, MHeight+1)
io.write("Length: ")
ANSI.setGraphicsMode(37)
io.write(#SnakePieces)
local time = tostring(math.floor((Dead or os.time()) - StartTime)).."s"
local timestr = "Time: "
ANSI.setCursorPosition(MWidth - (#timestr + #time), MHeight+1)
ANSI.setGraphicsMode(33)
io.write(timestr)
ANSI.setGraphicsMode(37)
io.write(time)
end
--Draw GameOver
local function DrawGameOver()
if not Dead then return end
local GameOver = {
" ",
" #-----------# ",
" | GAME OVER | ",
" #-----------# ",
" "}
ANSI.setGraphicsMode(0, 1, 31, 40)
ANSI.setCursorPosition(math.floor((TWidth-string.len(GameOver[1]))/2), math.floor((THeight-#GameOver)/2))
for k, line in ipairs(GameOver) do
ANSI.saveCursorPosition()
io.write(line)
ANSI.restoreCursorPosition()
ANSI.moveCursorDown(1)
end
end
--Render the game
local function RenderGame()
ANSI.setGraphicsMode(0, 37, 40)
ANSI.eraseDisplay()
DrawBackground()
DrawSnake()
DrawFruits()
DrawInfoBar()
DrawGameOver()
io.flush()
end
--Spawn a new fruit
local function NewFruit()
while true do
local FruitX, FruitY = math.random(1, MWidth), math.random(1, MHeight)
local continue = true
--Check if any fruit exists at this location
for id, Fruit in pairs(Fruits) do
if Fruit[1] == FruitX and Fruit[2] == FruitY then
continue = false
break
end
end
if continue then
--Check if any snake piece exists at this location
for id, Piece in pairs(SnakePieces) do
if Piece[1] == FruitX and Piece[2] == FruitY then
continue = false
break
end
end
end
if continue then
--Spawn the new fruit
Fruits[#Fruits + 1] = {FruitX, FruitY, math.random(1, #AvailableFruits)}
break
end
end
end
--Move the snake
local function MoveSnake()
local mx, my = DirectionToVector(SnakeDirection)
local HeadX, HeadY = SnakePieces[1][1], SnakePieces[1][2]
HeadX, HeadY = (HeadX + mx -1)%MWidth +1, (HeadY + my-1)%MHeight +1
local keepTail = false
--Check if the new head location hits a fruit
for id, Fruit in pairs(Fruits) do
if HeadX == Fruit[1] and HeadY == Fruit[2] then
keepTail = true --Make the snake longer
table.remove(Fruits, id) --Remove the eaten fruit
break
end
end
table.insert(SnakePieces, 1, {HeadX, HeadY})
if not keepTail then SnakePieces[#SnakePieces] = nil else
NewFruit() --Spawn a new fruit
end
--Check if the new head location hits a snake piece
for i=2, #SnakePieces do
local PieceX, PieceY = SnakePieces[i][1], SnakePieces[i][2]
if HeadX == PieceX and HeadY == PieceY then
Dead = os.time()
break
end
end
end
--Check if there's any input
local function CheckInput()
while true do
local input = Window:getch()
if not input then break end --No more input queue
if input == 27 then --Escape sequence
if Window:getch() == 91 then --[
local char = Window:getch()
if char and char <= 255 then
char = string.char(char)
if char == "A" and SnakeDirection ~= 2 then --Up
SnakeDirection = 0
break
elseif char == "B" and SnakeDirection ~= 0 then --Down
SnakeDirection = 2
break
elseif char == "C" and SnakeDirection ~= 3 then --Right
SnakeDirection = 1
break
elseif char == "D" and SnakeDirection ~= 1 then --Left
SnakeDirection = 3
break
end
end
end
end
end
end
--Run the main game loop
local function RunGame()
--Set the random seed
math.randomseed(os.time())
--Setup the terminal
ANSI.setCursorPosition(0, 0)
curses.curs_set(0) --Make the cursor invisible
curses.cbreak(true)
curses.echo(false)
assert(Window:nodelay(true), "Failed to make getch non-blocking")
--Set the starting snake pieces
local HeadX, HeadY = math.floor(TWidth/2), math.floor(THeight/2)
SnakePieces[1] = {HeadX, HeadY}
SnakePieces[2] = {HeadX-1, HeadY}
SnakePieces[3] = {HeadX-2, HeadY}
SnakePieces[4] = {HeadX-3, HeadY}
SnakePieces[5] = {HeadX-4, HeadY}
--Spawn starting fruits
for i=1, InitialFruits do NewFruit() end
while true do
CheckInput()
if not Dead then MoveSnake() end
RenderGame()
sleep((SnakeDirection == 0 or SnakeDirection == 2) and VerticalTick or HorizentalTick)
end
--Termination
ANSI.setCursorPosition(0, THeight+1)
end
--== Run the game ==--
RunGame()
--Reset graphics mode
ANSI.setGraphicsMode(0, 37)