From 767598b60c81ecebc189ecf0fdc8b37abe4fca48 Mon Sep 17 00:00:00 2001 From: sundowndev Date: Sat, 9 Dec 2017 01:33:49 +0100 Subject: [PATCH] UPDATE AHHH --- assets/css/style.css | 5 ---- assets/js/script.js | 66 ++++++++++++++++++++++++++++++++------------ index.html | 1 - 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/assets/css/style.css b/assets/css/style.css index dd3ddba..53ee379 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -41,9 +41,4 @@ body{ border: solid 0px #000; display: inline-block; -} - -.line-3 > .col-12{ - background: url(../img/tank_bg.png) no-repeat; - background-position: 0px 0px; } \ No newline at end of file diff --git a/assets/js/script.js b/assets/js/script.js index 8660229..077f2d2 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -1,7 +1,11 @@ var player = document.querySelector('#player'); -var x = 0; -var y = 0; +var maxX = document.querySelectorAll('.line-1 .col').length; +var maxY = document.querySelectorAll('.line').length; + +var x = 1; +var y = 5; + var controls = { up: 'z', @@ -10,8 +14,35 @@ var controls = { right: 'd' } -function setPosition () { - // +var rotation = { + up: '0px -50px', + down: '-50px -50px', + left: '-50px 0px', + right: '0px 0px', +} + +var direction = rotation.right; + +function setPosition (posX, posY, direction) { + gridX = x; + gridY = y; + + var grids = document.querySelectorAll('.col'); + + grids.forEach(function(e){ + e.style.background = ''; + e.style.backgroundPosition = ''; + }); + + var grid = document.querySelector('.line-'+posY+' .col-'+posX); + + grid.style.background = 'url(./assets/img/tank_bg.png) no-repeat'; + grid.style.backgroundPosition = direction; + + x = posX; + y = posY; + + console.log(posX+' pour '+maxX); } function getPosition () { @@ -21,19 +52,20 @@ function getPosition () { 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(); + if(k == controls.up && y > 1){ + var newY = y-1; + setPosition(x, newY, rotation.up); + } else if(k == controls.down && y < maxY){ + var newY = y+1; + setPosition(x, newY, rotation.down); + } else if(k == controls.left && x > 1){ + var newX = x-1; + setPosition(newX, y, rotation.left); + } else if(k == controls.right && x < maxX){ + var newX = x+1; + setPosition(newX, y, rotation.right); } } -document.addEventListener('keydown', move, false); \ No newline at end of file +document.addEventListener('keydown', move, false); +setPosition(x, y, direction); \ No newline at end of file diff --git a/index.html b/index.html index ead3e8f..6f0c30d 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,6 @@ de_tank -