Procedural Terrain Generation from Tables
Posted: Sun Sep 25, 2022 11:10 am
Can someone help me to code a Procedural Terrain Generation from Tables?
The simplest way to do it is (not tested):KedyZ wrote: ↑Sun Sep 25, 2022 2:26 pm So you have an empty table named terrain. There are variables g and a. a has air.png, and g has ground.png. then it will put the variables in the tables like this: terrain = {a,a,a,a
g,g,g,a
g,a,a,g
g,g,g,g} when it will generate the table it will somehow draw the terrain.
Code: Select all
tileSize = 32 -- for image 32x32 pixels
a = {image = love.graphics.newImage("air.png")} -- image 32x32 pixels
g = {image = love.graphics.newImage("ground.png")} -- image 32x32 pixels
map = {
{a,a,a,a},
{g,g,a,g},
{g,a,a,g},
{g,g,g,g},
}
Code: Select all
function love.draw()
for y, xs in ipairs (map) do
for x, tile in ipairs (xs) do
love.graphics.draw(tile.image, (x-1)*tileSize, (y-1)*tileSize)
end
end
end
Code: Select all
local w, h = 4, 4
map = {}
for y = 1, h do
map[y] = {}
for x = 1, w do
local value = love.math.noise( x*10, y*10 ) -- x10 makes more thick randomness
if value > 0.8 then -- 80% air and 20% ground
map[y][x] = g -- ground
else
map[y][x] = a -- air
end
end
end
Don't forget to define a and g.
Code: Select all
-- License CC0 (Creative Commons license) (c) darkfrei, 2022
function love.load()
tileSize = 32 -- for image 32x32 pixels
a = {image = love.graphics.newImage("air.png")} -- image 32x32 pixels
g = {image = love.graphics.newImage("ground.png")} -- image 32x32 pixels
local w, h = 25, 18
map = {}
for y = 1, h do
map[y] = {}
for x = 1, w do
local value = love.math.noise( 0.08*x, 0.2*y)
if value > 0.8 then -- 80% air and 20% ground
map[y][x] = g -- ground
else
map[y][x] = a -- air
end
end
end
end
function love.update(dt)
end
function love.draw()
for y, xs in ipairs (map) do
for x, tile in ipairs (xs) do
love.graphics.draw(tile.image, (x-1)*tileSize, (y-1)*tileSize)
end
end
end