function move( obj, dir ) if dir == 0 then obj.x = obj.x - 1 end if dir == 1 then obj.y = obj.y - 1 end if dir == 3 then obj.x = obj.x + 1 end if dir == 4 then obj.y = obj.y + 1 end end
explanation:
Spoiler
now i will make the AI system for a part! later i will implement it in the game loop
function AInone( obj, ev, p1, p2, p3, p4, p5 ) end AIwanderTimer = os.startTimer( 5 ) function AIwander( obj, ev, p1, p2, p3, p4, p5 ) if ev == "timer" and p1 == AIwanderTimer then local nDir = math.random( 1, 4 ) obj.dir = nDir move( obj, nDir ) AIwanderTimer = os.startTimer( math.random( 1, 10 ) ) end endexplanation:
Spoiler
more AI examples:
Spoiler
ow and dont forget the most important one:
function AIkeyboard( obj, ev, p1, p2, p3, p4, p5 ) if ev == "key" then if p1 == 17 then obj.dir = 2; move( obj, 2 ) end if p1 == 30 then obj.dir = 1; move( obj, 1 ) end if p1 == 31 then obj.dir = 4; move( obj, 4 ) end if p1 == 32 then obj.dir = 3; move( obj, 3 ) end end end
now the tick function:
function tick( ev, p1, p2, p3, p4, p5 ) ticks = ticks + 1 for i = 0,#object do for x = 0,#object[i].AI do object[i].AI[x]( object[i], ev, p1, p2, p3, p4, p5 ) end end endexplanation:
Spoiler
now finnaly the game loop:
frameSleep = os.startTimer( fps ) while true do local ev, p1, p2, p3, p4, p5 = os.pullEvent() tick( ev, p1, p2, p3, p4, p5 ) if ev == "timer" and p1 == frameSleep then frameSleep = os.startTimer( fps ) end end
adding object's and labels to your game:
if you want to add a new object to the game just do this:
player = Object:new({name = "Player", AI = {AIkeyboard}, sprite = {"<","^",">","V"}, x = 2, y = 2})
BTW:
left = direction 1
right = direction 3
up = direction 2
down = direction 4
have a look at the first part i edited some code to help me out!
outro:
this was my tutorial i hope it was pretty usefull
if i forgot something just post it in the comments
please leave a comment if you liked it or just click the green UP button over there ----->
thx, wilcomega












