Simple game with ipairs (for school)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
benniofficial
Prole
Posts: 5
Joined: Tue Mar 07, 2017 6:28 pm

Simple game with ipairs (for school)

Post by benniofficial »

Can somebody help me??1 :o 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 :awesome:
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Simple game with ipairs (for school)

Post 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
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Simple game with ipairs (for school)

Post 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
benniofficial
Prole
Posts: 5
Joined: Tue Mar 07, 2017 6:28 pm

Re: Simple game with ipairs (for school)

Post 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!
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Simple game with ipairs (for school)

Post by Nixola »

No.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Simple game with ipairs (for school)

Post 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...
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Simple game with ipairs (for school)

Post 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

MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Simple game with ipairs (for school)

Post 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:

Code: Select all

1	b
2	d
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.
User avatar
bgordebak
Party member
Posts: 130
Joined: Thu Jul 10, 2014 2:04 am
Location: Ankara, Turkey

Re: Simple game with ipairs (for school)

Post 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.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Simple game with ipairs (for school)

Post 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

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 214 guests