Simple terrain generation demo using a 1D "heightmap"
Annotated Source Code
-- terrain generation 1d heightmap
-- by rpwjanzen
-- width of pico-8
-- screen is 128
local window_width = 128
-- height of pico-8
-- screen is 128
local window_height = 128
-- the colour of the terrain
-- when it is drawn to the
-- screen
local terrain_colour = 6
-- the table that contains the
-- height of each position on
-- the screen
local terrain = {}
-- the maximum height that
-- terrain should be
local max_terrain_height = 128
function _init()
-- create terrain/heightmap
for x = 0, window_width do
local height =
rnd(max_terrain_height)
-- each spot is random
-- height up to 128 tall
terrain[x] = height
end
end
function _draw()
cls()
-- draw terrain/heightmap
for x = 0, window_width do
local top = terrain[x]
-- draw from bottom-up
line(
-- bottom of screen
x, window_height,
-- top of terrain
x, top,
terrain_colour
)
end
end |
I think the problem is this isn't particularly useful.. This code might be good for a beginner's demo.. but that's about it.
@Guard13007 What forum should be used for beginner-level code? I'm new to the forums.
@rpwjanzen: Beginner stuff is more than welcome around here, people new to pico-8 come by everyday after all. Even if you're a little off-base, a nice informative discussion can ensue and benefit everybody.
Regarding your cart, it's just that purely random heights don't really qualify as "terrain generation". For instance you could try averaging neighboring values to get something better looking. Also have a look here for a more usual solution. It doesn't look bad in your moon lander though, keep up the good work!
[Please log in to post a comment]



