Re: [help] Trying to learn Lua + LOVE doing tutorials
Posted: Fri Sep 09, 2011 8:27 am
Ok! I think I got it!
At first we declare our variables inside the function love.load(). Those variables will say the size of the grid map and size of the tiles.
Then the second part of the code does the loop part. It'll use the variables storing the grid size in the loop process, the first for will loop 16 times the second for that'll also loop 16 times that code that says "hit = math.random(0,1)" although I don't know what it mean and does. Also why the second loop has "[x][y]" other than just "[y]" on it?
The first for in this function stores the second for, that'll change the color of the rectangle based in a random number. That just clarifies me that "hit = math.random(0,1)" a bit, it does the random part and it stores it in map[x][y] I think, then every time a number is stored in map[x][y] it'll generate a new tile in the exact place where this random number designed, then the tile will be draw by the love.draw function after it get it's color set.
What I didn't understand about "hit = math.random(0,1) is that:
Wont it generate numbers over 16?
Does it have a chance to generate the same numbers more than once?
Why (0,1) not (0,0)?
Other than that, thanks for this help. It clarifies thinks a LOT for me now. Really.
Code: Select all
function love.load()
map_display_h = 15
map_display_w = 15
tile_w = 16
tile_h = 16
--NOTE: X comes before Y in the creation part so the array becomes map[X][Y] and not map[Y][X]
map = {}
for x=1, map_display_w do
map[x] = {}
for y=1, map_display_h do
map[x][y] = {
hit = math.random(0,1)
}
end
end
Then the second part of the code does the loop part. It'll use the variables storing the grid size in the loop process, the first for will loop 16 times the second for that'll also loop 16 times that code that says "hit = math.random(0,1)" although I don't know what it mean and does. Also why the second loop has "[x][y]" other than just "[y]" on it?
Code: Select all
function love.draw()
for y=1, map_display_h do -- this y=1 means that we're setting the variable y with 1 and using it on map[x][y] like x=1 in the second for?
for x=1, map_display_w do --
if map[x][y] == 1 then
love.graphics.setColor(0,0,0)
else
love.graphics.setColor(255,255,255)
end
love.graphics.rectangle("fill", x + map_offset_x, y + map_offset_y)
end
end
end
What I didn't understand about "hit = math.random(0,1) is that:
Wont it generate numbers over 16?
Does it have a chance to generate the same numbers more than once?
Why (0,1) not (0,0)?
Other than that, thanks for this help. It clarifies thinks a LOT for me now. Really.