Simple game with ipairs (for school)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 5
- Joined: Tue Mar 07, 2017 6:28 pm
Simple game with ipairs (for school)
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)
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
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)
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
-
- Prole
- Posts: 5
- Joined: Tue Mar 07, 2017 6:28 pm
Re: Simple game with ipairs (for school)
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)
No.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
- Sir_Silver
- Party member
- Posts: 286
- Joined: Mon Aug 22, 2016 2:25 pm
- Contact:
Re: Simple game with ipairs (for school)
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)
There are many examples on the internet which use ipairs =)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!
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)
This code is so wrong. Let me show you an example:xNick1 wrote: ↑Wed Mar 08, 2017 9:08 amCode: 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
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
Code: Select all
1 b
2 d
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)
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.
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)
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
Who is online
Users browsing this forum: Google [Bot] and 3 guests