diff --git a/assets/js/script.js b/assets/js/script.js index e940a03..44542f7 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -1,42 +1,75 @@ -function move(event) { - var k = event.keyCode, - chrId = document.querySelector('#block1'), - chr = { - updown: function () { - var y = parseInt(getComputedStyle(chrId).top); - if (k == 38) { - y -= 10; - if ( y == -10 ){ - y += 10; - } - } else if (k == 40) { - y += 10; - if ( y == 460 ){ - y -= 10; - } - } - return y; - }, +var player = document.querySelector('#player'); - leftright: function () { - var x = parseInt(getComputedStyle(chrId).left); - if (k == 37) { - x -= 10; - if ( x == -10 ){ - x += 10; - } - } else if (k == 39) { - x += 10; - if ( x == 1160 ){ - x -= 10; - } - } - return x; - } - }; +var x = 0; +var y = 0; - chrId.style.top = (chr.updown()) + "px"; - chrId.style.left = (chr.leftright()) + "px"; +var controls = { + up: 'z', + down: 's', + left: 'q', + right: 'd' } -document.addEventListener('keydown', move); \ No newline at end of file +function setPosition () { + // +} + +function getPosition () { + return [x, y]; +} + +function move(event) { + var k = event.key; + + if(k == controls.up){ + y++; + setPosition(); + } else if(k == controls.down){ + y--; + setPosition(); + } else if(k == controls.left){ + x--; + setPosition(); + } else if(k == controls.right){ + x++; + setPosition(); + } +// chr = { +// updown: function () { +// var y = parseInt(getComputedStyle(player).top); +// if (k == 'ArrowUp' || k == 'z') { +// y -= 10; +// if ( y == -10 ){ +// y += 10; +// } +// } else if (k == 'ArrowDown' || k == 's') { +// y += 10; +// if ( y == 460 ){ +// y -= 10; +// } +// } +// return y; +// }, +// +// leftright: function () { +// var x = parseInt(getComputedStyle(player).left); +// if (k == 'ArrowLeft' || k == 'q') { +// x -= 10; +// if ( x == -10 ){ +// x += 10; +// } +// } else if (k == 'ArrowRight' || k == 'd') { +// x += 10; +// if ( x == 1160 ){ +// x -= 10; +// } +// } +// return x; +// } +// }; +// +// player.style.top = (chr.updown()) + "px"; +// player.style.left = (chr.leftright()) + "px"; +} + +document.addEventListener('keydown', move, false); \ No newline at end of file