First try at using turtle, probably overdid the variables! Code is very rough so please feel free to fork and improve.
Log in to post a comment.
// You can find the Turtle API reference here: https://turtletoy.net/syntax Canvas.setpenopacity(1); let maxStories = 3; // min=1, max=10, step=1 let storyHeight = 5; // min=1, max=20, step=0.1 let houseWidth = 8; // min=1, max=25, step=0.1 let houseBlockSize = 0; // min=0, max=20, step=1 let resetBlockNewStreet = 0; // min=0, max=1, step=1 let roofAngle = 45; // min=0, max=60, step=1 let roofLine = 0; // min=0, max=1, step=0.01 let streets = 10; // min=1, max=10, step=1 let streetGap = 10; // min=5, max=50, step=0.1 let doors = 0; // min=0, max=1, step=0.01 let doorWidth = 2.5; // min=1, max=10, step=0.1 let doorHeight = 3.5; // min=1, max=20, step=0.1 let doorBottom = 0; // min=0, max=1, step=1 let windows = 0.5; // min=0, max=1, step=0.01 // Global code will be evaluated once. const turtle = new Turtle(); turtle.penup(); turtle.goto(-95,-100+(maxStories*storyHeight)+streetGap); turtle.pendown(); let blockCounter = 0; let houseCounter = 0; let streetCounter = 1; let drawing = true; while(drawing){ if(houseCounter * houseWidth > 180){ turtle.penup(); streetCounter++; houseCounter = 0; if(resetBlockNewStreet || blockCounter == houseBlockSize){blockCounter = 0;} if(streetCounter > streets){ drawing = false; } turtle.goto(-95,turtle.y()+(maxStories*storyHeight)+streetGap); turtle.pendown(); } else { drawHouse(); } } function drawHouse() { let houseHeight = random([0,maxStories]); if(houseBlockSize && blockCounter == houseBlockSize){ turtle.penup(); turtle.forward(houseWidth); turtle.pendown(); blockCounter = -1; } else if(houseHeight == 0){ turtle.forward(houseWidth); } else{ turtle.left(90); turtle.forward(houseHeight*storyHeight); turtle.right(90-roofAngle); turtle.forward(houseWidth/2/Math.cos(Math.PI/180*roofAngle)); turtle.right((2*roofAngle)); turtle.forward(houseWidth/2/Math.cos(Math.PI/180*roofAngle)); turtle.right(90-roofAngle); drawRoofLine(); turtle.forward(houseHeight*storyHeight); turtle.left(90); drawDoor(); drawWindows(houseHeight); } blockCounter++; houseCounter++; } function drawWindows(stories){ if(stories > 1 && (windows == 1 || (windows && Math.random() < windows))){ // Get current position const currentPosn = turtle.pos(); let windowWidth = houseWidth/5; let windowHeight = storyHeight/2.5; // Move to first floor turtle.penup(); turtle.backward(houseWidth); turtle.left(90); turtle.forward(storyHeight*5/4); turtle.right(90); turtle.forward(windowWidth); // Add windows for each story for(var i=0;i<stories-1;i++){ let prevWindow = turtle.pos(); turtle.pendown(); turtle.forward(windowWidth); turtle.left(90); turtle.forward(windowHeight); turtle.left(90); turtle.forward(windowWidth); turtle.left(90); turtle.forward(windowHeight); turtle.left(90); turtle.penup(); turtle.forward(windowWidth*2); turtle.pendown(); turtle.forward(windowWidth); turtle.left(90); turtle.forward(windowHeight); turtle.left(90); turtle.forward(windowWidth); turtle.left(90); turtle.forward(windowHeight); turtle.left(90); turtle.penup(); turtle.goto(prevWindow); turtle.left(90); turtle.forward(storyHeight); turtle.right(90); } // Move back to start turtle.penup(); turtle.goto(currentPosn) turtle.pendown(); } } function drawRoofLine(){ if(roofLine == 1 || (roofLine && Math.random() < roofLine)){ turtle.right(90); turtle.forward(houseWidth); turtle.penup(); turtle.backward(houseWidth); turtle.left(90); turtle.pendown(); } } function drawDoor(){ if(doors == 1 || (doors && Math.random() < doors)){ turtle.penup(); if(doorBottom){ turtle.backward((houseWidth-doorWidth)/2); turtle.pendown(); turtle.backward(doorWidth); }else{ turtle.backward((houseWidth+doorWidth)/2); turtle.pendown(); } turtle.left(90); turtle.forward(doorHeight); turtle.right(90); turtle.forward(doorWidth); turtle.right(90); turtle.forward(doorHeight); turtle.penup(); turtle.left(90); turtle.forward((houseWidth-doorWidth)/2); turtle.pendown(); } } function random(range) { return Math.round(Math.random() * (range[1] - range[0]) ) + range[0]; }