Created
December 18, 2025 12:41
-
-
Save Achie72/30243dadd995e5827e7f8c18c47cf519 to your computer and use it in GitHub Desktop.
Connect 4 Drop Code for 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
| -- I'm using sprite 1 and 2 for drawing, so draw something there | |
| units = {} | |
| cursor = { | |
| x = 1, | |
| y = 1 | |
| } | |
| bottom_of_board = 9 | |
| is_green = true | |
| function create_new_unit(name, health, attack, is_green) | |
| local unit = { | |
| name = name, | |
| health = health, | |
| attack = attack, | |
| is_green = is_green, | |
| let_go = false, -- we haven't let it go from cursor | |
| is_placed = false -- we haven't placed it yet on the board | |
| } | |
| -- add the new unit to the tablethat holds all of them | |
| -- we know the new unit is one at the very end, but | |
| -- with our logic we can use let_go and is_placed so | |
| -- apply movement to a specific few ones | |
| add(units, unit) | |
| end | |
| function _init() | |
| -- start with a unit in hand | |
| create_new_unit("goblin", 1, 2, is_green) | |
| end | |
| function _update() | |
| if btnp(0) then cursor.x -= 1 end | |
| if btnp(1) then cursor.x += 1 end | |
| for unit in all(units) do | |
| -- if the unit was let go then | |
| -- we are gonna inlcude this is_placed, so we can | |
| -- skip all this code for units that alrady reached a valid place | |
| if unit.let_go and (not unit.is_placed) then | |
| -- is there something below the unit | |
| local next_y = unit.y + 1 | |
| -- is it the bottom of the board? | |
| if next_y == bottom_of_board then | |
| -- we cannot go lower, so set it back to the | |
| -- original y level | |
| next_y = unit.y | |
| -- now "place" the unit, so we don't have to | |
| -- run all this code again | |
| unit.is_placed = true | |
| -- flip color, does nothing so far | |
| is_green = not is_green | |
| create_new_unit("goblin", 1, 2, is_green) | |
| end | |
| -- we can move lower, so check if there is something | |
| -- under us, if there are other units | |
| if not (unit.is_placed) then | |
| for other_unit in all(units) do | |
| -- this would include a check against itself, | |
| -- so do not let that | |
| if other_unit ~= unit then | |
| -- now check if we are on the same x pos, | |
| -- aka column | |
| if other_unit.x == unit.x then | |
| -- we now need to check if we are over them | |
| -- because if so, we don't move one mor slot down | |
| -- as that would be an overlap | |
| if other_unit.y == next_y then | |
| -- now "place" the unit, so we don't have to | |
| -- run all this code again | |
| unit.is_placed = true | |
| -- set the next pos to the current, so we will not move | |
| -- more down | |
| next_y = unit.y | |
| -- flip the color, does nothing so far | |
| is_green = not is_green | |
| create_new_unit("goblin", 1, 2, is_green) | |
| end | |
| end | |
| end | |
| end | |
| end | |
| unit.y = next_y | |
| elseif (not unit.is_placed) then | |
| -- if we haven't let go yet, it follows the cursor | |
| -- we do need to check of is_placed, as we let go of those | |
| -- units already, but we did place them so we can filter those | |
| -- out and only the the ONE unit we haven't placed or let go yet | |
| unit.x = cursor.x | |
| unit.y = cursor.y | |
| end | |
| end | |
| -- we are always playing with the last unit, which is at the tables length | |
| -- if we have 5 units, the last one is the 5th, which is the lenght of the table | |
| -- #table returns the length of it | |
| if btnp(5) then units[#units].let_go = true end | |
| end | |
| function _draw() | |
| cls() | |
| map() | |
| -- draw cursor, we want map tiles so it aligns to the grid nicely | |
| spr(15, cursor.x*8, cursor.y*8) | |
| -- draw units | |
| for unit in all(units) do | |
| spr(unit.is_green and 1 or 2, unit.x*8, unit.y*8) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment