Creating your own game is the dream of many who start learning programming. And if you take the language Lua, then the legendary game will be an excellent starting project "Snake". And we will do without engines and libraries — only pure Lua and console.
Lua is easy to learn, has a concise syntax, and is great for creating games and prototypes. It is often used in game engines (Roblox, Love2D, Defold), but even without them, you can write a working game in Lua.

What do we need for our "Snake"?
The playing field is a two-dimensional grid in which the snake moves.
Snake - a list of coordinates of its segments.
Food is a random point on the field.
Control - arrows (or WASD).
Mechanics - the snake moves, grows when eating food, loses when colliding with a wall or itself.
The first step is the playing field
width, height = 20, 20The snake and its movement
snake = {
{x = 10, y = 10},
{x = 9, y = 10},
{x = 8, y = 10}
}
direction = "right"Food
math.randomseed(os.time())
food = {x = math.random(1, width), y = math.random(1, height)}Management
io.input():setvbuf("no")
function getInput()
local key = io.read(1)
if key == "w" then direction = "up" end
if key == "s" then direction = "down" end
if key == "a" then direction = "left" end
if key == "d" then direction = "right" end
endGame cycle
while true do
getInput()
-- перемещаем змейку
head = {x = snake[1].x, y = snake[1].y}
if direction == "up" then head.y = head.y - 1 end
if direction == "down" then head.y = head.y + 1 end
if direction == "left" then head.x = head.x - 1 end
if direction == "right" then head.x = head.x + 1 end
table.insert(snake, 1, head)
-- проверка на еду
if head.x == food.x and head.y == food.y then
food = {x = math.random(1, width), y = math.random(1, height)}
else
table.remove(snake)
end
-- проверка столкновений
if head.x < 1 or head.y < 1 or head.x > width or head.y > height then
print("Game Over!")
break
end
-- вывод поля
os.execute("clear") -- для Linux/macOS (или "cls" для Windows)
for y = 1, height do
for x = 1, width do
local symbol = " . "
if x == food.x and y == food.y then
symbol = " @ "
end
for i, segment in ipairs(snake) do
if segment.x == x and segment.y == y then
symbol = " # "
end
end
io.write(symbol)
end
io.write("\n")
end
os.execute("sleep 0.1")
endWhat can be improved
Add a score counter.
Make different speed levels.
Add the ability to exit (for example, when entering q).
Draw a snake beautifully (for example, the head "O", the body "o").
Writing "Snake" in Lua without engines is a great project for a beginner. It helps to understand:
how to work with tables,
how to implement a game cycle,
how to process user input.
And only then you can transfer this experience to Love2D, Roblox or even write your own mini-engine!
In Codice we make programming training fun and easy to understand: we have interesting courses with tasks that help you improve your skills step by step.
And we also have an active Telegram channel, where we discuss cool ideas, share experiences and analyze tasks together — learning becomes not only useful, but also fun.
