Discord
Login
Community
DARK THEME

I'm trying to make a map grid

I want it to randomly generate (that's the proper term for what I'm doing) a grid of maps using the 3 maps I have for the background

Create several different sprites.

For example, with names corresponding to their fill color.

Run this code - if you have sprites with different names, put those names in a list called "name" .

init = function()
  name = ["red", "blue", "yellow", "green"]
  map = new Map( 20, 10, 15, 15 )
  for w = 0 to map.width -1
    for h = 0 to map.height -1
      map.set(w,h, name[ random.nextInt(4) ])
    end
  end
end

draw = function()
  screen.drawMap( map, 0, 0, 300, 150 )
end

Is there a way I could make it so it made it infanite

The Map object can be stretched dynamically after creation.

See example - but it cannot be too large.

mapRandom = function(map)
  for w = 0 to map.width -1
    for h = 0 to map.height -1
      map.set(w,h, name[ random.nextInt(4) ])
    end
  end
end

init = function()
  name = ["red", "blue", "yellow", "green"]
  map = new Map( 2, 1, 15, 15 )
  mapRandom( map )
  tick = 0
end

update = function()
  tick += 1
  if tick >= 20 then
    if map.width < 100 then 
      map.width += 1
    end
    if map.height < 50 then
      map.height += 1
    end
    tick = 0
  end
  mapRandom( map )
end
  
draw = function()
  screen.clear()
  screen.drawMap( map, 0, 0, 300, 150 )
  screen.drawText( map.width + " " + map.height + " FPS: " + system.fps , 0, screen.height/2 - 10, 20, "orange" )
end

If you need a large area

  • you immediately create a large Map, but this may affect the drawing speed
  • or you create a 2-dimensional array with data and set a window/chunk that takes data from the main large array to display data on the screen to a smaller map. Here there is a problem with managing whether the current chunks are in the view window and whether they should be changed.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community