Created
October 15, 2025 07:09
-
-
Save lxchurbakov/90694fb33b3b6e99c8aa3814196015ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| size=3 | |
| blocks = { | |
| {x=10, y=10} | |
| } | |
| apples = { | |
| {x=11,y=11} | |
| } | |
| score=0 | |
| dir="l" | |
| function update_dir() | |
| if btn(0) and dir != "r" then | |
| dir = "l" | |
| end | |
| if btn(1) and dir != "l" then | |
| dir = "r" | |
| end | |
| if btn(2) and dir != "d" then | |
| dir = "u" | |
| end | |
| if btn(3) and dir != "u" then | |
| dir = "d" | |
| end | |
| end | |
| ticks=0 | |
| speed=3 | |
| function move() | |
| local nb = { | |
| x=blocks[1].x, | |
| y=blocks[1].y, | |
| } | |
| if dir=="l" then nb.x -= 1 end | |
| if dir=="r" then nb.x += 1 end | |
| if dir=="u" then nb.y -= 1 end | |
| if dir=="d" then nb.y += 1 end | |
| ticks += 1 | |
| if ticks%speed == 0 then | |
| add(blocks, nb, 1) | |
| deli(blocks, #blocks) | |
| end | |
| end | |
| function update_apple() | |
| local na = { | |
| x=flr(rnd(32)), | |
| y=flr(rnd(32)), | |
| } | |
| deli(apples, 1) | |
| add(apples, na) | |
| end | |
| function eat() | |
| local head = blocks[1] | |
| for apple in all(apples) do | |
| if apple.x == head.x and | |
| apple.y == head.y then | |
| update_apple() | |
| sfx(0) | |
| score += 1 | |
| add(blocks, { | |
| x=blocks[#blocks].x, | |
| y=blocks[#blocks].y, | |
| }, #blocks) | |
| --end | |
| end | |
| end | |
| end | |
| function restart() | |
| blocks={{x=10,y=10}} | |
| sfx(1) | |
| score = 0 | |
| end | |
| function die() | |
| local head = blocks[1] | |
| if head.x < 0 or head.y < 0 then | |
| restart() | |
| end | |
| if head.x > 43 then | |
| restart() | |
| end | |
| for i=3,#blocks do | |
| local block=blocks[i] | |
| if head.x == block.x and | |
| head.y == block.y then | |
| return restart() | |
| end | |
| end | |
| end | |
| function update_speed() | |
| if #blocks > 100 then | |
| speed = 1 | |
| return | |
| end | |
| if #blocks > 10 then | |
| speed = 2 | |
| return | |
| end | |
| speed = 3 | |
| end | |
| function _update() | |
| update_dir() | |
| move() | |
| eat() | |
| die() | |
| update_speed() | |
| end | |
| function _draw() | |
| cls() | |
| for block in all(blocks) do | |
| rectfill( | |
| block.x * size, | |
| block.y * size, | |
| (block.x + 1) * size - 1, | |
| (block.y + 1) * size - 1, | |
| 12 | |
| ) | |
| end | |
| for apple in all(apples) do | |
| rectfill( | |
| apple.x * size, | |
| apple.y * size, | |
| (apple.x + 1) * size - 1, | |
| (apple.y + 1) * size - 1, | |
| 2 | |
| ) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment