1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| var map = document.querySelector("#map"); var n = 10; var maparray = []; var index = 0; var mouses = document.querySelector("#mouse"); var mouse1 = document.querySelector("#mouse1"); var rotatechange = 0;
var mouse = { mousepath: [], direction: 0, x: 1, y: 1, };
maparray = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 1, 1, 1, 0], [0, 1, 1, 0, 1, 1, 1, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 1, 1, 1, 1, 0, 1, 0], [0, 1, 1, 1, 0, 0, 0, 0, 1, 0], [0, 1, 0, 1, 1, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]];
createmap(n); mousemove(mouse.x, mouse.y); var times = setInterval(mousemove, 200);
function mousemove(x, y) { x = mouse.x; y = mouse.y; maparray[x][y] = 2; mouses.style.transform = `translate(${y * 50}px,${x * 50}px) rotate(${mouse.direction}deg)`; if (iswalk()) { mouse.mousepath.push({ x, y }); } function normalmove() { var path = mouse.mousepath; if (mouse.mousepath[index]) { maparray[path[index].x][path[index].y] = 3; mouse1.style.transform = `translate(${path[index].y * 50}px,${path[index].x * 50}px)`; index++; } createmap(10); } function iswalk() { var walk = true;
if (maparray[8][8] == 2) {
clearInterval(times); setInterval(normalmove, 100); } else if (maparray[x][y + 1] === 1) { mouse.direction = 270; mouse.y++; rotatechange = 0; } else if (maparray[x + 1][y] === 1) { mouse.direction = 0; mouse.x++; rotatechange = 0; } else if (maparray[x][y - 1] === 1) { mouse.direction = 90; mouse.y--; rotatechange = 0; } else if (maparray[x - 1][y] === 1) { mouse.direction = 180; mouse.x--; rotatechange = 0; } else { if (rotatechange == 0) mouse.direction -= 180;
rotatechange++; var len = mouse.mousepath.length - 1; mouse.x = mouse.mousepath[len].x; mouse.y = mouse.mousepath[len].y; mouse.mousepath.pop(); walk = false; } return walk; }
}
function createmap(n) {
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) { var div = document.createElement("div"); div.classList.add("cell"); if (maparray[i][j] == 1) { div.style.backgroundColor = "white"; div.style.top = i * 50 + "px"; div.style.left = j * 50 + "px"; map.appendChild(div);
} else if (maparray[i][j] == 3) { div.style.backgroundColor = "yellow"; div.style.top = i * 50 + "px"; div.style.left = j * 50 + "px"; map.appendChild(div);
} else if (maparray[i][j] == 0) { div.style.backgroundColor = "gray"; div.style.top = i * 50 + "px"; div.style.left = j * 50 + "px"; map.appendChild(div);
}
} } }
|