加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

简单的游戏地图生成器

发布时间:2020-12-14 22:56:02 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 -- Use this function to perform your initial setupfunction setup() print("Simple Map Sample!!") textMode(CORNER) spriteMode(CORNER) gridCoun

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

-- Use this function to perform your initial setup
function setup()
    print("Simple Map Sample!!")
    textMode(CORNER)
    spriteMode(CORNER)
    
    gridCount = 20
    
    
    scaleX = 50
    scaleY = 50
    -- number > 4
    plantSeed = 20.0
    -- number > 3
    minerialSeed = 20.0
    
    --[[
    parameter.integer("scaleX",20,100,50)
    parameter.integer("scaleY",50)
    parameter.number("plantSeed",5.0,100.0,10.0)
    parameter.number("minerialSeed",4.0,10.0,resetMapTable)
    --parameter.integer("gridCount",1,10,6)
    --]]
    
    imgMap = image((gridCount+1)*scaleX,(gridCount+1)*scaleY)
    
    mapT = {}
    
    tree1 = "松树"
    tree2 = "杨树"
    tree3 = "小草"
    mine1 = "铁矿"
    mine2 = "铜矿"
    
    imgTree1 = "Planet Cute:Tree Short"
    imgTree2 = "Planet Cute:Tree Tall"
    imgTree3 = "Platformer Art:Grass"
    imgMine1 = "Platformer Art:Mushroom"
    imgMine2 = "Small World:Treasure"
    
    itemTable = {[tree1]=imgTree1,[tree2]=imgTree2,[tree3]=imgTree3,[mine1]=imgMine1,[mine2]=imgMine2}
    
    
    -- 3*3 
    mapTable = {{pos=vec2(1,1),plant=nil,mineral=mine1},{pos=vec2(1,2),mineral=nil},3),plant=tree3,{pos=vec2(2,plant=tree1,plant=tree2,mineral=mine2},{pos=vec2(3,mineral=nil}}

    --print(randomPlant())
    --print(randomMinerial())

    --mapTable = createMap()
    --mapTable = {}
    mapTable = createMap()
end

function resetMapTable()
    mapTable = createMap()
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40,40,50)
    
    --if #mapTable == 0 then mapTable = createMap() end

    -- This sets the line thickness
    -- strokeWidth(5)

    -- Do your drawing here

    sImgMap = drawMap()
    sprite(sImgMap,0)
end

function createMap()
    for i=1,gridCount,1 do
        for j=1,1 do
            mapItem = {pos=vec2(i,j),plant=randomPlant(),mineral=randomMinerial()}
            table.insert(mapT,mapItem)
            --print(unpack(mapItem))
            --print(mapItem[plant],mapItem[mineral])
        end
    end
    return mapT
end

function randomPlant()
    local seed = math.random(1.0,plantSeed)
    local result = nil
    if seed >= 1 and seed < 2 then result = tree1 end
    if seed >= 2 and seed < 3 then result = tree2 end
    if seed >= 3 and seed < 4 then result = tree3 end
    if seed >= 4 and seed <= plantSeed then result = nil end
    
    return result
end

function randomMinerial()
    local seed = math.random(1.0,minerialSeed)
    local result = nil

    if seed >= 1 and seed < 2 then result = mine1 end
    if seed >= 2 and seed < 3 then result = mine2 end
    if seed >= 3 and seed <= minerialSeed then result = nil end
    
    return result

end

function getImg(name)
    return itemTable[name]
end

function drawMap()
    
    setContext(imgMap)
    
    for i = 1,gridCount*gridCount,1 do
        drawGround(mapTable[i].pos)
        if mapTable[i].plant ~= nil then drawTree(mapTable[i].pos,mapTable[i].plant) end
        if mapTable[i].mineral ~= nil then drawMineral(mapTable[i].pos,mapTable[i].mineral) end
    end
    
    setContext()
    return imgMap
end

function drawGround(position)
    local x,y = scaleX * position.x,scaleY * position.y
    strokeWidth(1)
    fill(5,155,255)
    fill(0,255)
    rect(x,y,scaleX,scaleY)
end

function drawTree(position,plant)
    local x,scaleY * position.y
    
    sprite(itemTable[plant],x,scaleX*4/10,scaleY)
    
    fill(100,200,255)
    --text(plant,y)

end

function drawMineral(position,mineral)
    local x,scaleY * position.y
    
    sprite(itemTable[mineral],x+scaleX/2,scaleX/2,scaleX/2)

    fill(100,255)
    text(mineral,y)
    
end

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读