Jump to content




How to make a Game with OOP PART 2/2

game lua

  • You cannot reply to this topic
4 replies to this topic

#1 wilcomega

  • Members
  • 466 posts
  • LocationHolland

Posted 21 May 2012 - 02:15 PM

next up i want to work on the move function:
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
end
explanation:
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
end
explanation:
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

#2 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 25 May 2012 - 11:24 PM

FIRST!
jk
But it seems easier to do this with a bunch of events and stuff..

#3 wilcomega

  • Members
  • 466 posts
  • LocationHolland

Posted 26 May 2012 - 11:00 AM

View PostNoodle, on 25 May 2012 - 11:24 PM, said:

FIRST!
jk
But it seems easier to do this with a bunch of events and stuff..

i argree with you,
but when i make a game i dont feel like making 5 functions for every object
so this is what comes in handy

#4 EatenAlive3

  • New Members
  • 53 posts

Posted 27 May 2012 - 04:23 PM

The post seems a little unclear, maybe you should include a sample file. Also, you should combine this with the first thread, rather than make it separate.

And these are called 'prototypes,' not classes. Lua doesn't support classes.

#5 kazagistar

  • Members
  • 365 posts

Posted 11 June 2012 - 04:58 PM

Sure lua supports classes. If by support you mean supports the syntactic sugar required to create a class system yourself, using some advanced metatable voodoo. Check it out: http://loop.luaforge.net/





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users