Buildings + Mountains

I spent a ridiculous amount of time on this one.

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(1.0);

// Global code will be evaluated once.
const turtle = new Turtle();
turtle.penup();
turtle.goto(0,0);
turtle.pendown();

function building(x, y, w, h){
    
    turtle.penup();
    turtle.goto(x,y);
    turtle.seth(0);
    turtle.down()
    
    turtle.left(90);
    turtle.forward(h);
    turtle.left(90);
    turtle.forward(w);
    turtle.left(90);
    turtle.forward(h);
    turtle.left(90);
    turtle.forward(w);
    
    // Windows
    
    
    let win_x = Math.floor(w/4);
    let win_y = Math.floor(h/7);
    
    turtle.penup();
    
    let win_size = 2.0;
    
    
    turtle.goto(x,y);
    
    turtle.seth(0);
    turtle.left(90);
    turtle.forward(win_size * 1);
    turtle.left(90);
    turtle.forward(win_size * 1.4);
    
    
    for(let j = 0; j < win_x; j++){
        for(let k = 0; k < win_y; k++){
            
            let t = turtle.clone();
            t.seth(0);
            t.up();
            t.left(90);
            t.forward(k / (win_y) * (h-1));
            t.left(90);
            t.forward(j / (win_x) * (w-1));
            t.seth(0);
            t.pendown();
            t.forward(win_size);
            t.left(90);
            t.forward(win_size);
            t.left(90);
            t.forward(win_size);
            t.left(90);
            t.forward(win_size);
            
            // Some darker windows
            if(not_random(k+j) < -0.4){
                t.right(45);
                t.forward(-3);
                t.right(45);
                t.forward(1);
                t.left(45);
                t.forward(2);
                t.left(45);
                t.forward(1);
                t.left(90);
                t.forward(1);
                t.left(45);
                t.forward(2);
            }
            
            t.penup();
            
        }
    }
    
}

let seed = 2;

function not_random(i){
    seed++;
    return Math.cos(i * 10 + seed * 20) + Math.sin(seed * 10);
}

let past_widths = 0;


// Will contain building height for every horizontal pixel
let building_heights = [];

// The walk function will be called until it returns false.
function walk(i) {
    let width_for_i = 10 *  Math.abs(not_random(i)) + 10;
    past_widths += width_for_i;
    
    let height = 30 * Math.abs(not_random(i)) + 10;
    
    building(
        -100 + past_widths,
        100,
        width_for_i,
        height
    );
    
    let len = building_heights.length;
    for(let j = len; j < len + width_for_i; j++){
        building_heights.push(height);
    }
    
    // Sky
    
    if(past_widths < 200){
        return true;
    } else {
        let t = turtle.clone();
        
        for(let j = 0; j < 100; j++){
            t.up();
            t.goto(-99,j);
            t.seth(0);
            
            for(let k = 0; k < 200; k+=2){
                let mountain = 8.0 * Math.cos(k / 200 * 12 + 1);
                mountain += 4.0 * Math.cos(k / 200 * 2 + 1);
                mountain += 2.0 * Math.cos(k / 200 * 20);
                mountain += 1.0 * Math.cos(k / 200 * 30);
                
                let dim = (k + j) % (8 + Math.floor(j/20 - 5 + 0.3 * Math.cos(k * 0.1)));
                
                if(mountain < j - 10 && dim < 2 && building_heights[k+4] < 100-j){
                    t.down();
                }
                
                t.forward(2);
                t.up();
            }
        }
        
        return false;
    }
}