Page 1 of 1

Drawing a line of tile

Posted: Tue Sep 26, 2023 9:10 pm
by Law333
Hi

I'm new to Love2d.
So here is the problem:
I tried to fill the screen with tiles but the screen went totally black when I ran my code.
The thing is, when I run the line of the code that draws the tile alone, it works.
here is my script that I've written so far:

tile = {}
tile.img = love.graphics.newImage("Assets/Green_concrete.jpg")
tile.x = 0
tile.y = 0
tile.espace = 60

function love.draw()
for i= 1, 12 do
for i= 1, 21 do
love.graphics.draw(tile.img,tile.x,tile.y,0,0.15)
tile.x = tile.x + tile.espace
end
tile.y = tile.y + tile.espace
end
end


thank you !

Re: Drawing a line of tile

Posted: Tue Sep 26, 2023 10:39 pm
by dusoft
Not sure, but you are having two loops rewriting each other using the same variable i.

Also, the syntax is:

Code: Select all

love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy, kx, ky )
And it seems you are using x scaling? That is probably unintended.

You will also need to reset tile.x after each row.

Re: Drawing a line of tile

Posted: Wed Sep 27, 2023 7:11 am
by darkfrei

Code: Select all

tile = {}
tile.img = love.graphics.newImage("Assets/Green_concrete.jpg")
tile.x = 0
tile.y = 0
tile.w, tile.h = love.grephics.getDimensions ()

function love.draw()
  for j= 0, 12 do -- vertical 
    for i= 0, 21 do
      local x = tile.x + i * tile.w
      local y = tile.y + j * tile.h
      love.graphics.draw(tile.img, x, y)
    end
  end
end

Re: Drawing a line of tile

Posted: Wed Sep 27, 2023 7:45 am
by pgimeno
dusoft wrote: Tue Sep 26, 2023 10:39 pm Not sure, but you are having two loops rewriting each other using the same variable i.
No they don't rewrite each other, despite of using the same variable. Loop control variables are implicitly local. You can check with e.g.:

Code: Select all

for i = 1, 5 do
  print(i)
  for i = 10, 12 do
    print(i)
  end
end
--[[ Output:
1
10
11
12
2
10
11
12
3
10
11
12
4
10
11
12
5
10
11
12
--]]

Re: Drawing a line of tile

Posted: Wed Sep 27, 2023 4:40 pm
by dusoft
pgimeno wrote: Wed Sep 27, 2023 7:45 am
dusoft wrote: Tue Sep 26, 2023 10:39 pm Not sure, but you are having two loops rewriting each other using the same variable i.
No they don't rewrite each other, despite of using the same variable. Loop control variables are implicitly local. You can check with e.g.:

Code: Select all

for i = 1, 5 do
  print(i)
  for i = 10, 12 do
    print(i)
  end
end
--[[ Output:
1
10
11
12
2
10
11
12
3
10
11
12
4
10
11
12
5
10
11
12
--]]
Ok, sure, Lua and thanks for the correction. Anyway, bad programming habits make the code less readable and hard to debug. I. E. Never use a variable iterator with the same name for main and subloops.