Drawing a line of tile

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.
Post Reply
Law333
Prole
Posts: 1
Joined: Tue Sep 26, 2023 9:04 pm

Drawing a line of tile

Post 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 !
Attachments
Screen of what my code is doing.PNG
Screen of what my code is doing.PNG (6.91 KiB) Viewed 4680 times
User avatar
dusoft
Party member
Posts: 611
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Drawing a line of tile

Post 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.
User avatar
darkfrei
Party member
Posts: 1195
Joined: Sat Feb 08, 2020 11:09 pm

Re: Drawing a line of tile

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3640
Joined: Sun Oct 18, 2015 2:58 pm

Re: Drawing a line of tile

Post 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
--]]
User avatar
dusoft
Party member
Posts: 611
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Drawing a line of tile

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests