Skip to content

Instantly share code, notes, and snippets.

@Achie72
Last active May 20, 2024 09:53
Show Gist options
  • Save Achie72/4d61a59ec7c775b067809fb7b9343d00 to your computer and use it in GitHub Desktop.
Save Achie72/4d61a59ec7c775b067809fb7b9343d00 to your computer and use it in GitHub Desktop.
Pickups in PICO-8
-- Or if you don't want to bother with drawing each pickup with every tile aviable to you, you can make them "objects".
--For this, create an array, that will hold all our pickups:
pickupCollection = {}
-- and a function that you can call to add pickups.
-- we include a _type so later you can differentiate them
-- by checking the this. For ex: _type = 1 is a healing item, _type = 2 is a bomb etc...
function add_pickup(_xPosition, _yPosition, _type)
-- we create a local variable to hold the item
local pickup = {
-- holds x position
x = _xPosition,
-- holds y position
y = _yPosition,
type = _type
}
-- now that we have the data we need
-- add it to the global array that holds them
add(pickupCollection, pickup)
end
-- now you can add a pickup anywhere by calling
-- add_pickup(xPosYouWant, yPosYouWant, typeYouWant)
-- after all this we just need to handle the collision
-- and the logic inside our _update() or a function that is called
-- from _update()
-- Loop through all of them, one-by-one they are checked from the array
-- and put into the item variable. We do a backwards for loop so you can
-- delete items and nothing can shift over to screw you
for i=#pickupCollection,1,-1 do
local item = pickupCollection[i]
-- handle collision. If you are tiled based, then what sugarvoid sent works
-- but we don't need an mget() cuz we know the exact position. If you are
-- doing AABB collision, put it here instead
if item.x == player.x and item.y == player.y then
-- let's check the types I mention to know what to do
if item.type == 1 then
-- heal the player for ex
player.lives += 1
elseif item.type == 2 then
-- here you can do another one
end
-- if we picked up the item, delete it from the array
del(pickupCollection, item)
end
end
-- now all you need is to draw them somewhere on the screen
-- so inside _draw() or a function called from _draw()
-- usually I align my pickups in a row on the spritesheet.
-- if you can do that, you can use _type to grab the sprite you need
-- ex: you have item starting from id:16 then the first item is 16, second
-- is 17, third 18 ... etc. This is just 16 + (pickup.type-1)
for pickup in all(pickupCollection) do
-- if you are doing tile based, then multiply x,y by 8
-- so they are tiled aligned. Remember tiles are 8x8
spr(16 + (pickup.type-1), pickup.x*8, pickup.y*8)
-- if not, then just draw them normally on their position
end
-- bam you are set
@Achie72
Copy link
Author

Achie72 commented May 19, 2024

Development Video explaining all this: https://www.youtube.com/watch?v=Vd9DPIzlFGg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment