Log In  

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


Simple terrain generation demo using a 1D "heightmap"

P#67145 2019-09-02 18:54

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
P#67147 2019-09-02 18:58
1

Errm ...

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

P#67154 2019-09-02 21:07

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

P#67205 2019-09-04 02:27

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.

P#67207 2019-09-04 02:36

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

P#67209 2019-09-04 02:43

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.

P#67211 2019-09-04 03:29

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

P#67213 2019-09-04 03:33

@Guard13007 What forum should be used for beginner-level code? I'm new to the forums.

P#67215 2019-09-04 03:38

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

P#67246 2019-09-04 19:29

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 11:35:25 | 0.069s | Q:29