You are viewing a single comment's thread from:

RE: Pixel forest: Randomly generated trees

in #gamedev8 years ago

It goes something like this:

<body>
<font face="verdana" size=2>


<canvas id="screen" width="1200" height="800"></canvas> 
<script type="text/javascript">

var c=document.getElementById("screen");
var ctx=c.getContext("2d");
var CANVAS_WIDTH = 1200;
var CANVAS_HEIGHT = 800;
var FPS = 30;


ctx.clearRect(0,0,CANVAS_WIDTH,CANVAS_HEIGHT);

//make screen
ctx.fillStyle="#EEEEEE";
ctx.fillRect(0,0,1200,800);
ctx.moveTo(0,0);
ctx.lineTo(1200,0);
ctx.lineTo(1200,800);
ctx.lineTo(0,800);
ctx.lineTo(0,0);
ctx.stroke();

var nx=100;
var ny=100;
var tiles=0;

function Draw() {

ctx.fillStyle="#EEEEEE";
ctx.fillRect(0,0,1200,800);
ctx.moveTo(0,0);
ctx.lineTo(1200,0);
ctx.lineTo(1200,800);
ctx.lineTo(0,800);
ctx.lineTo(0,0);
ctx.stroke();
    if (month == 1){
        for(w=0;w<=32;w++){
            for(z=0;z<=32;z++){
                ctx.drawImage(grass_m02,nx+32*z,ny+16*w);
                if(Math.random() > (1 - tree_freq)){ctx.drawImage(oak_grass_m02,nx+32*z,ny+16*w -42);}
                if(Math.random() > (1 - tree_freq)){ctx.drawImage(oak2_grass_m02,nx+32*z,ny+16*w -42);}
                if(Math.random() > (1 - tree_freq/6)){ctx.drawImage(pine2_grass_m02,nx+32*z,ny+16*w -42);}
                if(Math.random() > (1 - tree_freq/4)){ctx.drawImage(pine_grass_m02,nx+32*z,ny+16*w -42);}
                if(Math.random() > (1 - tree_freq/4)){ctx.drawImage(pine3_grass_m02,nx+32*z,ny+16*w -42);}
                if(Math.random() > (1 - tree_freq/4)){ctx.drawImage(pine4_grass_m02,nx+32*z,ny+16*w -42);}
                if(Math.random() > (1 - tree_freq/6)){ctx.drawImage(dead_pine_grass_m02,nx+32*z,ny+16*w -42);}
                tiles++;
            }
            for(z=0;z<=32;z++){
                ctx.drawImage(grass_m02,nx+32*z-16,ny+16*w+8);
                if(Math.random() > 0.99){ctx.drawImage(grass_m02_twig2,nx+32*z-16,ny+16*w+8);}
                if(Math.random() > 0.98){ctx.drawImage(grass_m02_twig,nx+32*z-16,ny+16*w+8);}
                if(Math.random() > 0.99){ctx.drawImage(grass_m02_twigs,nx+32*z-16,ny+16*w+8);}
                tiles++;
            }
        }
    }