Last active
September 21, 2020 16:14
-
-
Save DiegoPinho/18ca955e17a3d2725562ad12fc98fe21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onload = function() { | |
console.log("carregando arquivo..."); | |
var canvas = document.getElementById("canvas"); | |
var boneco = document.getElementById("boneco"); | |
var ctx = canvas ? canvas.getContext("2d") : null; | |
var bonecoCoord = { | |
x: 25, | |
y: 200 | |
} | |
if(ctx) ctx.drawImage(boneco, bonecoCoord.x, bonecoCoord.y); | |
function darUmPasso() { | |
var arg = [...arguments][0]; | |
if(arg) { | |
if(typeof arg === 'number') { | |
bonecoCoord.x += arg; | |
} else { | |
switch(arg) { | |
case "direita": bonecoCoord.x += 3; break; | |
case "esquerda": bonecoCoord.x -= 3; break; | |
case "cima": bonecoCoord.y -= 3; break; | |
case "baixo": bonecoCoord.y += 3; break; | |
default: return; | |
} | |
} | |
} else { | |
bonecoCoord.x += 3; | |
} | |
console.log("andando..."); | |
ctx.clearRect(0, 0, canvas.width, canvas.height); | |
ctx.drawImage(boneco, bonecoCoord.x, bonecoCoord.y); | |
} | |
document.addEventListener('keydown', function(e) { | |
if(e.keyCode === 39) {darUmPasso();} | |
}); | |
window.darUmPasso = darUmPasso; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment