Skip to content

Code for randomly generating trees in forest

Jake Thakur edited this page Aug 16, 2022 · 5 revisions
function Random (minimum, maximum) {
    return Math.floor((Math.random() * (maximum - minimum + 1)) + minimum);
}

let yIncrement = Random(40, 700);
for (let y = 30; y < 6000; y += yIncrement)
{
	for (let x = 30; x < 6000; x += Random(40, 400))
	{
    	let tempx = x + Random(40, 400);
        let objectY = y + Random(0, yIncrement);
        let imgRandom = Random(1, 15);
        let img;
        switch (imgRandom) {
        	case 1:
            case 2:
            case 3:
            case 4:
            	img = "tree1A";
            	break;
        	case 5:
            case 6:
            case 7:
            case 8:
            	img = "tree2A";
            	break;
        	case 9:
            case 10:
            case 11:
            case 12:
            	img = "tree3A";
            	break;
            case 13:
            	img = "tree1B";
            	break;
            case 14:
            	img = "tree2B";
            	break;
            case 15:
            	img = "tree3B";
            	break;
        }
        if (Random(0,1) == 0) {
        	img += "flipped";
        }
		document.write('{x=' + x + ',y=' + objectY + ',image="'+img+'",name="Tree"},<br>');
	}
	yIncrement = Random(40, 700);
}
Clone this wiki locally