Skip to content

Instantly share code, notes, and snippets.

@Achie72
Created March 16, 2025 10:14
Show Gist options
  • Save Achie72/8ac788de67f946d5f2d88fdb580af103 to your computer and use it in GitHub Desktop.
Save Achie72/8ac788de67f946d5f2d88fdb580af103 to your computer and use it in GitHub Desktop.
Multiplay Demo
function _init()
-- create collection for players
players = {}
-- p1
player1 = {
x = 0,
y = 2,
clr = 12,
health = 10
}
-- p2
player2 = {
x = 11,
y = 2,
clr = 8,
health = 10
}
-- add them to the player coll
add(players,player1)
add(players, player2)
end
-- we are calling them
function control_player(_id)
-- you can see that we move
-- players[id] which is
-- the index we call in update
-- btnp(0, _id-1) this weird
-- thing checks for the button
-- press of p1, p2, see keyboard
-- controls on pico wiki
-- arrows for p1
-- esdf for p2
-- using _id-1 because btn player index
-- is 0 based, but lua arrays are 1 based.
-- you can either just use _id, and index players
-- with _id+1 to the players[] array or keep it this way
-- basic movement in 4 directions
-- if you have other code you want to do
-- for your player do it here
-- but remember to call codes
-- with players[_id] instead
-- of your default p/player
-- you would do in singleplayer
-- games, alternatievly let's do
local active_player = players[_id]
-- which will hold it jsut the same, but will cost less tokens
if btnp(0, _id-1) then
active_player.x -= 1
end
if btnp(1, _id-1) then
active_player.x += 1
end
if btnp(2, _id-1) then
active_player.y -= 1
end
if btnp(3, _id-1) then
active_player.y += 1
end
-- here you can also use code how you would otherswise
-- in singleplayer, jsut call it with the current player
-- let's say there is a saw in 32,32 and if player
-- walks into it, they take damage
-- 32,32 in tiles is 4,4 (remmber pixel to tile is /8)
if active_player.x == 4 and active_player.y == 4 then
active_player.health -= 1
end
end
function _update()
-- here we loop through player
-- collection to move p1, p2
-- by passing the i as index
for i=1,2 do
control_player(i)
end
end
function _draw()
cls()
for player in all(players) do
print("웃", player.x*8, player.y*8, player.clr)
end
print("✽", 32, 32, 7)
print("arrow for p1, health"..players[1].health,2,100, 12)
print("esdf for p2, health"..players[2].health, 2,108, 8)
print("✽ is a saw that hurts", 2, 116, 7)
end
@Achie72
Copy link
Author

Achie72 commented Mar 16, 2025

@Achie72
Copy link
Author

Achie72 commented Mar 16, 2025

If you would like to read more like this, feel free to checkout my Ko-fi articles, with many devlogs and code rundowns like this!
https://github.com/Achie72/kofi-articles

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