Skip to content

Instantly share code, notes, and snippets.

@keith-kurak
Created October 30, 2022 00:14
Show Gist options
  • Save keith-kurak/01966ad68f5df50ebcce90a3f7d171b0 to your computer and use it in GitHub Desktop.
Save keith-kurak/01966ad68f5df50ebcce90a3f7d171b0 to your computer and use it in GitHub Desktop.
PICO-02: maps and collision
-- Create a map and get a sprite moving on it
function _init()
x = 64
y = 64
spd = 1
end
function _draw()
cls()
camera(x-24,0)
map()
spr(0,x,y)
end
function _update()
if (btn(➡️)) then
x+=spd
end
if (btn(⬅️)) then
x-=spd
end
if (btn(⬆️)) then
y-=spd
end
if (btn(⬇️)) then
y+=spd
end
end
-- apply jump physics and collision detection
-- constants
max_speed = 1.5
function _init()
player = {
x = 64,
y = 64,
dx = 0,
dy = 0,
spd = 1,
jump = 1,
on_ground = 0
}
end
function _draw()
cls()
camera(player.x-24,0)
map()
spr(0,player.x,player.y)
end
function _update60()
local p = player
-- apply gravity
p.dy=p.dy+0.05
-- up/ down
if (btn(➡️)) then
p.dx=p.dx+0.05
end
if (btn(⬅️)) then
p.dx=p.dx-0.05
end
-- jump
if btnp(🅾️) and
(p.on_ground or p.jump<1) then
p.on_ground=false
p.jump+=1
p.dy=p.dy-2
end
-- check wall coll
if hit(p.x+p.dx,p.y,7,7,1) then
player.dx=0
end
-- check ground coll
if hit(p.x,p.y+p.dy,7,7,1) then
if p.dy>0 then
p.on_ground=true
end
p.dy=0
end
-- decel if on ground
if p.on_ground then
p.jump=0
end
-- max acceleration
if p.dx > max_speed then
p.dx = max_speed
end
-- update forward/ upward motion
p.y=p.y+p.dy
p.x=p.x+p.dx
end
-- collision function
function hit(x,y,w,h,flag)
collide=false
for i=x,x+w,w do
if (fget(mget(i/8,y/8))==flag) or
(fget(mget(i/8,(y+h)/8))==flag) then
collide=true
end
end
return collide
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment