User Tools

Site Tools


javascript:labirintas

This is an old revision of the document!


Labirintą taip pat galima padaryti su JavaScript. Čia pateikiu kodą kuris paišo labirintą ir judina kursorių, dar reikia pridėti tikrinima kad neleistu eiti jeigu lieti juodą sieną.

<canvas id="myCanvas" width="600" height="600">
Your browser does not support the HTML5 canvas tag.</canvas>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
 
var x = 50, y = 50, step = 5;
 
var img = new Image;
img.src = "http://wiki.coderdojo.lt/lib/exe/fetch.php?\
media=javascript:20_by_20_orthogonal_maze.png";
ctx.drawImage(img,0,0,600,600); 
 
document.onkeydown = checkKey;
 
function refresh(){
	ctx.clearRect(0, 0, c.width, c.height);
    ctx.drawImage(img,0,0,600,600); 
    ctx.beginPath();
	ctx.arc(x,y,1,0,2*Math.PI);
	ctx.stroke();
}
 
function checkKey(e) {
 
    e = e || window.event;
 
    if (e.keyCode == '38') y = y - step;
    else if (e.keyCode == '40') y = y + step;
    else if (e.keyCode == '37') x = x - step;
    else if (e.keyCode == '39') x = x + step;
 
    if (x < 0) x = 0;
    if (y < 0) y = 0;
    if (x > 600) x = 600;
    if (y > 600) y = 600;
	refresh();
}

Labirintas:

javascript/labirintas.1480965300.txt.gz · Last modified: by aistis