User Tools

Site Tools


programmering:maze2-js-code
<script>

function rand(start,end) {
    return start+Math.round((Math.random()*(end-start)));
}

var window_height=window.innerHeight*0.9;
var window_width=window.innerWidth*0.9;
var width=10*Math.floor(window_width/10);
var height=10*Math.floor(window_height/10);
document.getElementById("screen").width=width;
document.getElementById("screen").height=height;

var canvas=document.getElementById("screen");
var ctx=canvas.getContext("2d");
var pause_time=2500;

function drawOne(size) {
    ctx.beginPath();
    ctx.arc(centerX,centerY,radius,0,Math.PI*2,true);
    ctx.stroke();
    for (var i=0; i<24; i++) {
	var angle=(2*Math.PI/24)*i;
	var x=centerX+Math.cos(angle)*(radius-size);
	var y=centerY+Math.sin(angle)*(radius-size);
	ctx.beginPath();
	ctx.arc(x,y,size,0,Math.PI*2,true);
	ctx.stroke();
    }
}

function drawBorder() {
    ctx.clearRect(0,0,width,height);
    ctx.fillStyle="#000000";
    ctx.fillRect(0,0,10,height-1); // left
    ctx.fillRect(width-11,0,10,height-1); // right
    ctx.fillRect(0,height-11,width-21,10); // bottom
    ctx.fillRect(19,0,width-21,10); // top
}

function drawWall(startpos,endpos,where,hole,direction) {
    if (direction=='h') { 
	ctx.beginPath();
	ctx.moveTo(startpos*10,where*10);
	ctx.lineTo(hole*10,where*10);
	ctx.moveTo((hole+1)*10,where*10);
	ctx.lineTo((endpos+1)*10,where*10);
	ctx.stroke();
    } 
    if (direction=='v') { 
	ctx.beginPath();
	ctx.moveTo(where*10,startpos*10);
	ctx.lineTo(where*10,hole*10);
	ctx.stroke();
	ctx.moveTo(where*10,(hole+1)*10);
	ctx.lineTo(where*10,(endpos+1)*10);
	ctx.stroke();
    }
}

function makeWalls(xstart,xend,ystart,yend,direction) {
    if (yend==ystart || xend==xstart) return;
    if (direction=='h') {
	var wall=rand(ystart+1,yend);
	var hole=rand(xstart,xend-1);
	drawWall(xstart,xend,wall,hole,'h');
	  makeWalls(xstart,xend,ystart,wall-1,'v');
	  makeWalls(xstart,xend,wall,yend,'v');
    }
    if (direction=='v') {
	var wall=rand(xstart+1,xend);
	var hole=rand(ystart,yend-1);
	drawWall(ystart,yend,wall,hole,'v');
	    makeWalls(xstart,wall-1,ystart,yend,'h');
	    makeWalls(wall,xend,ystart,yend,'h');
    }
}

function draw() {
    drawBorder();
    makeWalls(1,width/10-2,1,height/10-2,'v');
    setTimeout(draw,pause_time);
}

draw();

</script>
programmering/maze2-js-code.txt · Last modified: 2023/10/21 19:19 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki