Snake 500
Just for a personal challenge, I tried to fit snake into 500 bytes of PICO-8 code! It's a little rough, but it works! I'm sure some PICO-8 compression geniuses could take this further, but I'm proud that I was able to squeeze in sound, difficulty that increases with score, and a score display, bringing it right up to 500 characters!
Known issues:
- There's no way of restarting the game outside of reloading the cart/webpage.
- Fruit (the red circle) can appear in the top left corner, partially obscured by the score.
- It's possible to change direction multiple times in each animation 'tick', resulting in you potentially facing backwards and instantly dying. So don't do that!
Here's the code in its entirety, for anyone curious:
l=3s={}a={}for i=1,257do
s[i]=24a[i]=32end
x=0y=0v=8z=0u=1f=8t=1r=8p=1::a::if u>15 then
x=s[l]+v
y=a[l]+z
if x==f and y==r then
l+=1f=flr(rnd(17))*8r=flr(rnd(17))*8t+=.25
?"\acg"
else
for i=1,l-1do
s[i]=s[i+1]a[i]=a[i+1]end
end
s[l]=x
a[l]=y
if(pget(x,y)>1 or x<0 or x>128 or y<0 or y>128)p=0
u=0
else
if(p>0)u+=t
end
if(btn(0)and v==0)v=-8z=0
if(btn(1)and v==0)v=8z=0
if(btn(2)and z==0)v=0z=-8
if(btn(3)and z==0)v=0z=8
cls(1)circ(f,r,4,8)for i=1,l do
circfill(s[i],a[i],4,3)end
?t*4-4,8
flip()goto a
Here are a few of the strategies I used to keep size limits down:
- I used both the single line and traditional Lua 'if' statements, depending on which would use less code
- X and Y co-ordinates for the snake are stored in separate tables, which is less intuitive, but saves space whenever they need to be called
- Some operations are carried out on shorter variables when possible (for instance, the x position of the snake head, s[l], is temporarily stored in x, so where possible, calculations are carried out on x rather than s[l])
- I avoided line-breaks unless necessary, and structured the order of declarations to minimise the total amount of line-breaks
- The game speed and your score are calculated with the same variable (t), which is incremented by 0.25 whenever you collect fruit. Your score is t*4-4, and the game animation updates when u exceeds 15 (u is incremented by t every frame)
- I chose colours which could be stored in a single byte (<10)
- Collision checks with the screen boundary or your own tail are both in a single-line 'if' statement, and tail collisions are based on a pixel colour check rather than location table co-ordinate comparisons
Bring on the next tiny-code game jam!
Download
Install instructions
Run in browser, or download and unzip the appropriate file for your platform, and run the executable.

Leave a comment
Log in with itch.io to leave a comment.