Created
January 5, 2026 19:34
-
-
Save Achie72/c33127450812798f8b7984cbe2b3b559 to your computer and use it in GitHub Desktop.
How to push sokoban boxes in PICO-8
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
| function _init() | |
| player = { | |
| x=1, | |
| y=1, | |
| direction = 5 | |
| } | |
| poke(0x5f2c,3) | |
| end | |
| directions = { | |
| left = 1, | |
| right = 2, | |
| up = 3, | |
| down = 4, | |
| none = 5 | |
| } | |
| dir_table = { | |
| {-1,0},{1,0},{0,-1},{0,1},{0,0} | |
| } | |
| function _update() | |
| pressed = false | |
| if btnp(0) then | |
| player.direction = directions.left | |
| pressed = true | |
| elseif btnp(1) then | |
| player.direction = directions.right | |
| pressed = true | |
| elseif btnp(2) then | |
| player.direction = directions.up | |
| pressed = true | |
| elseif btnp(3) then | |
| player.direction = directions.down | |
| pressed = true | |
| end | |
| -- if we pressed move | |
| if pressed then | |
| local newx = player.x+dir_table[player.direction][1] | |
| local newy = player.y+dir_table[player.direction][2] | |
| -- not a wall | |
| if mget(newx,newy) ~= 1 then | |
| -- check if it is a box | |
| if mget(newx,newy) > 15 then | |
| -- check if the box can move as well | |
| local newbx = newx+dir_table[player.direction][1] | |
| local newby = newy+dir_table[player.direction][2] | |
| if mget(newbx,newby) == 0 or mget(newbx,newby) == 4 then | |
| -- move box | |
| -- save tile first | |
| local tile = mget(newx,newy) | |
| mset(newbx,newby,tile) | |
| mset(newx,newy,0) | |
| -- move player | |
| player.x = newx | |
| player.y = newy | |
| end | |
| else -- not box, so move | |
| player.x = newx | |
| player.y = newy | |
| end | |
| end | |
| end | |
| end | |
| function _draw() | |
| cls() | |
| -- I used tile 1 as walls, 2 as the player, 3 as the boxes, for as the spots | |
| -- to push on | |
| local tileascii = {"[]","","█","()"} | |
| local tilecolor = {1,1,4,9} | |
| print("웃", player.x*8, player.y*8,6) | |
| for i=0,8 do | |
| for j=0,6 do | |
| local tile = mget(i,j) | |
| if tile ~= 0 then | |
| print(tileascii[tile],i*8,j*8,tilecolor[tile]) | |
| end | |
| else | |
| --print(".",i*8,j*8,1) | |
| end | |
| end | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made for my Kofi article about a sokoban word puzzle game: https://ko-fi.com/post/PICO-ASCII-Jam-0--In-the-beningi-G2G11RQH04