Page 1 of 2
Simple game with ipairs (for school)
Posted: Tue Mar 07, 2017 6:51 pm
by benniofficial
Can somebody help me??1
I have to make a game with ipairs in the school and i didnt understand anything... It would be very nice when somebody can help me
Re: Simple game with ipairs (for school)
Posted: Wed Mar 08, 2017 6:00 am
by xNick1
You use ipairs to iterate on a table.
It would be something like "for each element in the table, do element + 1"
That way all of your table elements would be increased by one
Re: Simple game with ipairs (for school)
Posted: Wed Mar 08, 2017 6:55 am
by raidho36
The "ipairs" is a function that generates for-loop usable parameters from a table. You use it in a generic for-loop, which takes three arguments: the iterator function, the invariant (normally a table), and initial state of iteration control variable. The "ipairs" function produces all three arguments from the table. Its counterpart is "pairs" function that, when used in for-loop, effectively will iterate over all keys, not just first consecutive integers. Generic for-loop calls iterator function and passes the invariant and current iteration state into it. The function returns some value and new iteration state. Those two values are assigned to the two for-loop construct variables.
Code: Select all
function iterator ( invariant, iteration )
iteration = iteration + 1
if invariant[ iteration ] then
return iteration, invariant[ iteration ]
end
end
Code: Select all
iterator, invariant, initialstate = ipairs ( sometable )
for index, value in iterator, invariant, initialstate do
print ( value, "at", index )
end
Code: Select all
for index, value in ipairs ( sometable ) do print ( value, "at", index ) end
Re: Simple game with ipairs (for school)
Posted: Wed Mar 08, 2017 7:23 am
by benniofficial
thanks for the help! can somebody create a game for me with the ipairs for example a simple pong clone or asteroid? it would be very nice!
Re: Simple game with ipairs (for school)
Posted: Wed Mar 08, 2017 7:36 am
by Nixola
No.
Re: Simple game with ipairs (for school)
Posted: Wed Mar 08, 2017 8:46 am
by Sir_Silver
I really do enjoy helping people and writing code examples, but to ask someone to write a simple game for you, I think, is a bit abusive. I mean, do you plan to have someone write it for you and then you will take the credit for it? This is not really what we do on these forums I'm afraid...
Re: Simple game with ipairs (for school)
Posted: Wed Mar 08, 2017 9:08 am
by xNick1
benniofficial wrote: ↑Wed Mar 08, 2017 7:23 am
thanks for the help! can somebody create a game for me with the ipairs for example a simple pong clone or asteroid? it would be very nice!
There are many examples on the internet which use ipairs =)
You can try to learn from them.
Then when you're making your own game, if you encounter any problems just ask.
Here's a quick pratical example:
For every bullet in the table, I update its position and then for every bullet I check if it's colliding with the enemy.
If the bullet exits the screen, I delete it from the table by its index "i".
Code: Select all
for i, v in ipairs(listOfBullets) do
v:update(dt)
v:checkCollision(enemy)
if v.dead then
table.remove(listOfBullets, i)
end
end
Re: Simple game with ipairs (for school)
Posted: Wed Mar 08, 2017 12:47 pm
by MasterLee
xNick1 wrote: ↑Wed Mar 08, 2017 9:08 am
Code: Select all
for i, v in ipairs(listOfBullets) do
v:update(dt)
v:checkCollision(enemy)
if v.dead then
table.remove(listOfBullets, i)
end
end
This code is so wrong. Let me show you an example:
Code: Select all
x={'a','b','c','d'}
for i,c in ipairs(x) do
table.remove(x,i)
end
for i,c in ipairs(x) do
print(i,c)
end
We expect not output but we get:
So what is wrong?
a is removed right and b is the new number one. Now continue with the second one c. Now there are only 2 elements left and we checked two already so we are finished.
Re: Simple game with ipairs (for school)
Posted: Wed Mar 08, 2017 1:56 pm
by bgordebak
I can't write the game for you, but I can give some pointers.
Pong is a really simple game, you can figure it out easily. Basically, all you do is make the ball move and bounce, and check if ball collides with a paddle or moved to the edge of the screen. Bouncing is done at the top and bottom of the screen and when collided with the paddles, so you need to figure out if the ball is at the top or bottom of the screen or collides with the paddles. Checking collision is also simple, and there are many examples on the net.
Just do something, and request help when you're stuck.
EDIT: I suggest first making a bouncing ball on the screen without the paddles. When the ball reaches an edge of the screen it bounces.
EDIT: Make everything one thing at a time, if you didn't understand the logic. Put a ball on the screen first. Then move it. Then make it bounce, etc.
Re: Simple game with ipairs (for school)
Posted: Wed Mar 08, 2017 2:29 pm
by Beelz
I'm not entirely sure with pairs/ipairs(I try to avoid it anyways
(If I recall, in terms or speed it goes: pairs -> ipairs -> numeral iteration)), but you shouldn't remove table items inside an iteration, unless you are traversing backwards.
Code: Select all
-- It would be fine for things like:
for i, e in ipairs(entities) do
e:draw()
end
-- But for updating/removing I just iterate backwards
for i = #entities, 1, -1 do
local e = entities[i]
-- Check death, only update if false
if e:isDead() then table.remove(entities, i)
else
e:update(dt)
end
end