Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local totalX = 32
- local totalY = 27
- local grid = {}
- local snakeTail = {}
- local snakeSize = 0
- local snakeX = 1
- local snakeY = 1
- local output = ""
- local fruitIcon = "•"
- local snakeIcon = "@"
- local clock = os.clock
- function makeGrid()
- for x=1,totalY do
- grid[x] = {}
- for y=1,totalX do
- grid[x][y] = " "
- end
- end
- end
- function sleep(secs)
- local timePoint = clock()
- while clock() - timePoint <= secs do end
- end
- while true do
- fruitX = math.random(totalY)
- fruitY = math.random(totalX)
- snakeSize = snakeSize + 1
- while snakeX ~= fruitX or snakeY ~= fruitY do
- makeGrid()
- table.insert(snakeTail,1,{snakeX,snakeY})
- while snakeSize < #snakeTail do
- table.remove(snakeTail,#snakeTail)
- end
- grid[snakeX][snakeY] = snakeIcon
- for k=1,#snakeTail do
- grid[snakeTail[k][1]][snakeTail[k][2]] = snakeIcon
- end
- if totalX < totalY then
- if snakeX > fruitX and snakeY == fruitY then
- snakeX = snakeX - 1
- end
- if snakeX < fruitX and snakeY == fruitY then
- snakeX = snakeX + 1
- end
- if snakeY > fruitY then
- snakeY = snakeY - 1
- end
- if snakeY < fruitY then
- snakeY = snakeY + 1
- end
- else
- if snakeY > fruitY and snakeX == fruitX then
- snakeY = snakeY - 1
- end
- if snakeY < fruitY and snakeX == fruitX then
- snakeY = snakeY + 1
- end
- if snakeX > fruitX then
- snakeX = snakeX - 1
- end
- if snakeX < fruitX then
- snakeX = snakeX + 1
- end
- end
- grid[fruitX][fruitY] = fruitIcon
- for l=1,#snakeTail do
- if snakeTail[l][1] == snakeX and snakeTail[l][2] == snakeY then
- snakeSize = 1
- end
- end
- output = ""
- os.execute("cls")
- for i=1,totalY do
- for j=1,totalX do
- output = output .. grid[i][j]
- end
- output = output .. "\n"
- end
- print(output)
- sleep(0.3)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment