Page 1 of 1

Drawing many things at once using a loop

Posted: Mon Jul 13, 2015 10:15 am
by moonkaman227
Hi, I'm new both to lua and also to love. So i have a quick question, i did a little searching before making this post but I couldn't find exactly my problem probably because its simple. Any ways i have set up a loop that generates a x and a y coordinate and using the love.graphics.draw function draws a 1 by 1 square at that coordinate, The problem is that the square doesn't stay drawn instead it updates positions and just moves i want the program to draw the square then move on to the next one and draw that and so on until it creates a large square made up of tiny ones. If anybody could help me with this i would appreciate it. Also I will be happy to clarify/provide examples, Thank you.

Re: Drawing many things at once using a loop

Posted: Mon Jul 13, 2015 1:27 pm
by micha
Hi and welcome to the forum.

Yes, please upload an example .love-file. That way we can have a look at it and give specific advice, rather than guessing.

Re: Drawing many things at once using a loop

Posted: Mon Jul 13, 2015 9:22 pm
by rok
If I understood what you want then this might help :)

Code: Select all

function love.load()
  squares = {}
  love.graphics.setColor(255,255,255)
  newX, newY = 0, 0
end

function love.update(dt)
  if newY < 20 then
    newSquare = {}
    newSquare['x'] = newX
    newSquare['y'] = newY
    table.insert(squares, newSquare)
    if newX < 20 then
      newX = newX + 1
    else
      newX = 0
      newY = newY + 1
    end
  end
end

function love.draw()
  for i, v in ipairs(squares) do
    love.graphics.rectangle("fill",squares[i]['x'],squares[i]['y'],1,1)
  end
end
I'm using a table of coordinate pairs for the squares.