Advertisement
Guest User

random "tile" around a position

a guest
Mar 28th, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.55 KB | None | 0 0
  1. function love.load()
  2.     tiles = { --random map with the "tiles"
  3.         {0,0,0,0,0},
  4.         {0,0,0,0,0},
  5.         {0,0,0,0,0},
  6.         {0,0,0,0,0},
  7.         {0,0,0,0,0},
  8.     }
  9.  
  10.     mouselock = false -- you can ignore this mouse lock stuff, its just a trick i use to force a single click...
  11.  
  12.     function random(mx,my)
  13.         local x1 = math.random(-1,1) --random function to get the x variation
  14.         local x2 = math.random(-1,1) --random function to get the y variation
  15.  
  16.         local rmx = mx+x1
  17.         local rmy = my+x2
  18.  
  19.         -- some checks to not crash the code with a outside position
  20.         if rmx < 1 then rmx = 1 end
  21.         if rmx > 5 then rmx = 5 end
  22.         if rmy < 1 then rmy = 1 end
  23.         if rmy > 5 then rmy = 5 end
  24.  
  25.         return rmx,rmy
  26.     end
  27. end
  28.  
  29. function love.update()
  30.     if love.mouse.isDown(1) and not mouselock then -- random stuff is triggered with a mouse click
  31.         mouselock = true
  32.  
  33.         local mx,my = love.mouse.getPosition() -- using the mouse position as the center
  34.         mx = math.floor(mx/160)+1 -- converting mouse x to a map coordenate
  35.         my = math.floor(my/120)+1 -- converting mouse y to a map coordenate
  36.  
  37.         tiles[my][mx] = 1 -- changing the center red to visualize the origin
  38.  
  39.         local rmx,rmy = random(mx,my) --function gets the mouse coordenate, and return a valid random position around it
  40.  
  41.         tiles[rmy][rmx] = 2 -- changing a random tile around the center
  42.     end
  43.  
  44.     function love.mousereleased()
  45.         mouselock = false
  46.     end
  47. end
  48.  
  49. function love.draw()
  50.     local x,y = 0,0
  51.     for j=1,5,1 do
  52.         for i=1,5,1 do
  53.             if tiles[j][i] == 0 then -- empty "tile"
  54.                 love.graphics.setColor(1,1,1)
  55.                 love.graphics.rectangle("fill",x,y,160,120)
  56.                 love.graphics.setColor(0,0,0)
  57.                 love.graphics.rectangle("line",x,y,160,120)
  58.             end
  59.             if tiles[j][i] == 1 then -- center "tile"
  60.                 love.graphics.setColor(1,0,0)
  61.                 love.graphics.rectangle("fill",x,y,160,120)
  62.                 love.graphics.setColor(0,0,0)
  63.                 love.graphics.rectangle("line",x,y,160,120)
  64.             end
  65.             if tiles[j][i] == 2 then -- random generated "tile"
  66.                 love.graphics.setColor(0,0,1)
  67.                 love.graphics.rectangle("fill",x,y,160,120)
  68.                 love.graphics.setColor(0,0,0)
  69.                 love.graphics.rectangle("line",x,y,160,120)
  70.             end
  71.             x = x+160
  72.         end
  73.         x = 0
  74.         y = y+120
  75.     end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement