<canvas id ="gameCanvas" width="800" height="600">
</canvas>
<script type="text/javascript">
document.onkeydown = checkKey;
var canvas;
var canvasContext;
window.onload = function () {
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
var fps = 7;
setInterval(function(){ draw();}, 1000/fps);
function draw(){
canvasContext.fillStyle = 'green';
canvasContext.fillRect(0,0,canvas.width,canvas.height);
//head
canvasContext.fillStyle = 'black';
canvasContext.fillRect(350,50,90,70);
canvasContext.fillStyle = 'white';
canvasContext.fillRect(355,55,80,60);
//face
canvasContext.fillStyle = 'black';
canvasContext.fillRect(360,60,5,20);
canvasContext.fillRect(420,60,5,20);
canvasContext.fillRect(390,100,30,5);
function punch() {
//arms
canvasContext.fillStyle = 'black';
canvasContext.fillRect(500,60,10,100);
canvasContext.rotate(0.1);
canvasContext.moveTo(250, 50);
canvasContext.lineTo(250, 250);
}
}
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '37') {
// left arrow
console.log("left")
punch()
}
else if (e.keyCode == '39') {
// right arrow
console.log("right")
}
else if (e.keyCode == '38') {
// up arrow
console.log("up")
}
else if (e.keyCode == '40') {
// down arrow
console.log("down")
}
}
}
</script>
برچسب:
نویسنده: آیلین رضوانی