Skip to content

Instantly share code, notes, and snippets.

@ilmoralito
Last active September 22, 2024 04:20
Show Gist options
  • Save ilmoralito/e1ad13f10e61ee408484bd93a2aa6044 to your computer and use it in GitHub Desktop.
Save ilmoralito/e1ad13f10e61ee408484bd93a2aa6044 to your computer and use it in GitHub Desktop.
This function handles player movement, animation, and collision detection in a grid-based game. The move function updates the player's position based on directional input, while anim cycles through sprite frames to create an animation effect. The collide function checks if the player's new position intersects with any solid objects, reverting th…
function move(o)
local lx=o.x
local ly=o.y
if btn(⬅️) then
o.x-=o.speed
end
if btn(➡️) then
o.x+=o.speed
end
if btn(⬆️) then
o.y-=o.speed
end
if btn(⬇️) then
o.y+=o.speed
end
if collide(o) then
o.x=lx
o.y=ly
end
end
function anim(o)
if o.anim_timer<o.anim_speed then
o.anim_timer+=1
else
if o.sprite<o.last_sprite then
o.sprite+=1
else
o.sprite=o.initial_sprite
end
o.anim_timer=0
end
end
function collide(o)
local x1=o.x/8
local y1=o.y/8
local x2=(o.x+7)/8
local y2=(o.y+7)/8
local a=fget(mget(x1,y1),0)
local b=fget(mget(x1,y2),0)
local c=fget(mget(x2,y2),0)
local d=fget(mget(x2,y1),0)
if a or b or c or d then
return true
end
return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment