/* // code of keys https://gcctech.org/csc/javascript/javascript_keycodes.htm arrow left 37 arrow up 38 arrow right 39 arrow down 40 */ x1 = 0; y1 = 0; let x = 0; let y = 0; let myId = ""; let st = ""; let count = 100; let speed = 10; // generator napisów for(let i=2; i < count; i++){ x = Math.floor(Math.random() * 800); y = Math.floor(Math.random() * 600); console.log(x); console.log(y); myId = "napis" + i; st = `position:absolute;background-color:red;top:${y}px;left:${x}px;`; document.write(`

${myId}

`); } document.onkeydown = function(e) { let doc = []; for(let i=1; i < count; i++){ doc[i] = document.getElementById(`napis${i}`); } switch (e.keyCode) { case 37: //alert('left'); x1 -= speed; break; case 38: //alert('up'); y1 -= speed; break; case 39: //alert('right'); x1 += speed; break; case 40: //alert('down'); y1 += speed; break; } napis1.style.setProperty("left", x1 + "px"); napis1.style.setProperty("top", y1 + "px"); for(let i=2; i < count; i++){ if(checkCollision(napis1,doc[i])){ doc[i].style.display = "none"; //napis1.style.display = "none"; } } } // wykrywanie kolizji let checkCollision = function(napis1,napis2){ isCollision = false; let rect1 = napis1.getBoundingClientRect(); let rect2 = napis2.getBoundingClientRect(); var leftToRight = rect1.left + rect1.width > rect2.left; var rightToLeft = rect2.left + rect2.width > rect1.left; var upToDown = rect1.top + rect1.height > rect2.top; var DownToUp = rect2.top + rect2.height > rect1.top; if (leftToRight && rightToLeft && upToDown && DownToUp){ isCollision = true; } return isCollision; };