Log In  


Cart #najirosege-0 | 2019-09-02 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA


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

1

Errm ...

cls()
for i=0,127 do
line(i,127,i,rnd(128))
end


@dw817 The code you posted draws random vertical lines to the screen. If a game creator only needs to draw a set of lines to the screen they should use the code you supplied.


Just saying, that's what I saw, RPW. I tried the arrow keys and everything. Nothing changed. I program what I see or experience - and often rate accordingly.


@dw817 Correct. This is by design. There is no way to update the terrain. This post is a "Code Snippet" demoing one way to create terrain using a heightmap. The value of the post is the code, not in the interactivity of the cartridge.


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.


I didn't want to say that, Guard, ... but, there ya go. :)


@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]