Skip to content

Instantly share code, notes, and snippets.

@jcollard
Last active May 9, 2024 19:59
Show Gist options
  • Save jcollard/61c80932fc309d984f31ce61b56e38d3 to your computer and use it in GitHub Desktop.
Save jcollard/61c80932fc309d984f31ce61b56e38d3 to your computer and use it in GitHub Desktop.
function BOOT()
Player = CreateActor(50, 50, 12, 12, 1, 0, 0)
Enemies = {}
table.insert(Enemies, CreateActor(90, 70, 8, 8, 2, 1, 1))
table.insert(Enemies, CreateActor(10, 10, 4, 4, 3, -2, -1))
table.insert(Enemies, CreateActor(10, 30, 5, 5, 4, -3, -2))
table.insert(Enemies, CreateActor(20, 10, 6, 6, 7, -1, -2))
end
function CreateActor(x, y, w, h, c, vx, vy)
local actor = {}
actor.x = x
actor.y = y
actor.w = w
actor.h = h
actor.c = c
actor.vx = vx
actor.vy = -vy
return actor
end
function TIC()
-- handle user input
-- update game
-- draw
UserInput()
Update()
Draw()
end
function UserInput()
Player.vx = 0
if btn(2) then Player.vx = -1 end
if btn(3) then Player.vx = 1 end
Player.vy = 0
if btn(0) then Player.vy = -1 end
if btn(1) then Player.vy = 1 end
end
function Update()
MoveActor(Player)
for i, e in ipairs(Enemies) do
MoveActor(e)
end
end
function Draw()
cls(0)
DrawActor(Player)
for i, e in ipairs(Enemies) do
DrawActor(e)
end
end
function MoveActor(actor)
actor.x = actor.x + actor.vx
actor.y = actor.y + actor.vy
if actor.x > 240 then actor.x = 0 end
if actor.y > 136 then actor.y = 0 end
if actor.y < 0 then actor.y = 136 end
if actor.x < 0 then actor.x = 240 end
end
function DrawActor(actor)
rect(
actor.x,
actor.y,
actor.w,
actor.h,
actor.c)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment