Skip to content

Instantly share code, notes, and snippets.

@J4n1X
Created December 19, 2022 11:28
Show Gist options
  • Save J4n1X/8c029077d9fa32344963ab55f99210ea to your computer and use it in GitHub Desktop.
Save J4n1X/8c029077d9fa32344963ab55f99210ea to your computer and use it in GitHub Desktop.
A bouncing ball demo for the pico-8
pico-8 cartridge // http://www.pico-8.com
version 39
__lua__
-- pico-8 bouncing balls demonstration
-- 2022, by j4n1x
actors={}
pstartx = 1
pendx = 128
pstarty = 11
pendy = 128
info = "press ❎ to add a ball"
cntinfo = "balls on screen: "
-- xa = x position
-- ya = y position
-- sa = sprite index
-- pa = transparency colours
-- as bitfield
-- propsa = other properties
function make_actor(xa, ya, sa, pa, propsa)
act = {
x = xa,
y = ya,
s = sa,
p = pa or 0,
props = propsa or {}
}
add(actors, act)
return act
end
function make_ball()
make_actor(
rnd(pendx-pstartx-8)+pstartx,
rnd(pendy-pstarty-8)+pstarty,
1,
0b11 << 14,
{
hvel = rnd(10),
vvel = rnd(10),
fcount = 0,
aspeed = 4,
exec = function(act)
if act.x + act.props.hvel + 8 > pendx or
act.x + act.props.hvel < pstartx then
act.props.hvel = -act.props.hvel
else
act.x = act.x + act.props.hvel
end
if act.y + act.props.vvel + 8 > pendy or
act.y + act.props.vvel < pstarty then
act.props.vvel = -act.props.vvel
else
act.y = act.y + act.props.vvel
end
act.props.fcount = act.props.fcount + 1
if act.props.fcount == act.props.aspeed then
act.s= ((act.s)%4)+1
act.props.fcount = 0
end
end
})
end
function draw_actor(act)
palt(act.p)
spr(act.s, act.x, act.y)
if act.props["exec"] ~= nil then
act.props["exec"](act)
end
palt()
end
function _init()
infow=(128-print(info,0,-10))/2
end
function _draw()
cls(1)
-- help text
rectfill(0,0,128,pstarty,0)
print(info,infow, 0, 7)
local cntinfow=(128-print(
cntinfo..#actors,0, -10))/2
print(cntinfo..#actors,
cntinfow, 6, 7)
-- all actors
foreach(actors, draw_actor)
-- accept input
if btnp(❎) then
make_ball()
end
end
__gfx__
00000000008888000088880000888800008888000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000088887700888888008888880077888800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700888887788888888888888888877888880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000888888888888888888888888888888880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000888888888888888888888888888888880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700888888888888877887788888888888880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000088888800888877007788880088888800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000008888000088880000888800008888000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment