Home > Software design >  love2d: How to change value in map table?
love2d: How to change value in map table?

Time:08-03

Recently I started learning love2d and now I am making a collision map in tiles.

I want to change only index where some random sized boxes generated in the map table by loop. But somehow I can't get them changed correctly. I am quite a beginner and have no clear understang how tables work.. Could someone give me correct way to do it? currently I set mapsize width 480, height 320, and tilesize is 16.

function generateBoxes()
    for i=1,5 do
        local x=math.random (0, mapsizex*tilesize)
        local y=math.random (0, mapsizey*tilesize)
        local width=math.random(1,6)
        local height=math.random(1,4)
        
        ChangeMap(math.floor(x/tilesize),math.floor(y/tilesize),width,height,1)
    end
    PrintMap()
end


function MakeNewMap()
    for row = 1,mapsizey do
        map[row] = {}
        for column = 1,mapsizex do
            map[row][column]= 0
        end
    end
end

function ChangeMap(x,y,w,h,num)
    print("x"..x.." y"..y.." w"..w.." h"..h)
    
    for i=y, h 1 do
        for j=x,w do 
            map[i][j]=num
        end
    end
    
end

function PrintMap()
    for i = 1, #map do
        local s = ''
        for j= 1, #map[i] do
            s = (s .. ' ' .. map[i][j])
        end
        print(s)
    end
end

Sorry my english is probably strange, I wanted it to work as it first makes a map table like this

map={ 
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
 }

and then I want to swap some parts to 1 like this for example,

map={ 
{0,0,0,0,0},
{0,1,1,0,0},
{0,1,1,0,0}, 
}

CodePudding user response:

Take a look at https://www.lua.org/pil/4.3.4.html

for var=exp1,exp2,exp3 do
   something
end

That loop will execute something for each value of var from exp1 to exp2, using exp3 as the step to increment var. This third expression is optional; when absent, Lua assumes one as the step value

You however used for i=y, h do and misused exp2 as the number of loops. Change it to for i=y, y h do

Unrelated but important: You never check the bounds in ChangeMap, which produces unexpected results when the box exceeds the bounds.

CodePudding user response:

@Luke100000, Thank you for the explanation, I was trying also another way and ended now like this.And this looks working ok..

    for i=0, h-1 do
        for j=0,w-1 do
            if  (i y)>0 and (j x)>0 and (i y)<=mapsizey and (j x)<=mapsizex  then
                map[i y][j x]=num
            end
        end
   end

I attach the whole sample code to test.

function love.load()
    math.randomseed (os.clock ())
    screensizex=love.graphics.getWidth()
    screensizey=love.graphics.getHeight()
    tilesize=16
    map={}
    mapsizex = screensizex/tilesize     --30 tiles
    mapsizey = screensizey/tilesize     --20tiles
    MakeNewMap()
    --generateBoxes()
end

function love.update(dt)

end






function generateBoxes()
     
     for i=1,5 do
            local x=math.random (0, mapsizex*tilesize)
            local y=math.random (0, mapsizey*tilesize)
            local width=math.random(1,6)
            local height=math.random(1,4)
            
            ChangeMap(math.floor(x/tilesize),math.floor(y/tilesize),width,height,1)
    end
    --PrintMap()
end


function MakeNewMap()
    for row = 1,mapsizey do
        map[row] = {}
        for column = 1,mapsizex do
            map[row][column]= 0
        end
    end
end

function ChangeMap(x,y,w,h,num)
    print("x"..x.." y"..y.." w"..w.." h"..h)

    for i=0, h-1 do
        for j=0,w-1 do
            if  (i y)>0 and (j x)>0 and (i y)<=mapsizey and (j x)<=mapsizex  then
                map[i y][j x]=num
            end
        end
   end

end

function PrintMap()
    for i = 1, #map do
        --print("#map:"..#map)
        local s = ""
        for j= 1, #map[i] do
            s = (s .. " " .. map[i][j])
        end
        print(s)
    end
    print(" ")
end

function love.keypressed(key)
    if key=="space" then
        PrintMap()
    end
    if key=="g" then
        generateBoxes()
        PrintMap()
    end
    if key=="c" then
        ChangeMap(3,2,3,2,1)
    end
    if key=="n" then
        MakeNewMap()
    end
    
end

  • Related