[help] Trying to learn Lua + LOVE doing tutorials

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.
User avatar
Strelok
Prole
Posts: 18
Joined: Thu Sep 08, 2011 6:44 pm
Location: Brazil - Amazonas

Re: [help] Trying to learn Lua + LOVE doing tutorials

Post by Strelok »

Ok! I think I got it! :o

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
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?

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
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. :awesome:
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: [help] Trying to learn Lua + LOVE doing tutorials

Post by Taehl »

I'm glad we could help!

By the way, Strelok, the STALKER games use Lua as their scripting language (and boy, do they have a lot of scripts). They're generally not very easy to follow, though.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [help] Trying to learn Lua + LOVE doing tutorials

Post by Robin »

Strelok wrote: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)?
hit = math.random(0,1) makes hit randomly either 0 or 1. So no, yes, because (0, 0) always gives 0.
Help us help you: attach a .love.
User avatar
Strelok
Prole
Posts: 18
Joined: Thu Sep 08, 2011 6:44 pm
Location: Brazil - Amazonas

Re: [help] Trying to learn Lua + LOVE doing tutorials

Post by Strelok »

Oh! Sorry I didn't know about how it worked. I thought it was going to generate random numbers to (0, 1) for the zero and for the one. I didn't knew. Anyway, that clarifies a lot the code. I think I understand it completely now. :awesome:
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests