本文实例为大家分享了js坦克移动的具体代码,供大家参考,具体内容如下先看看,js超简
本文实例为大家分享了js坦克移动的具体代码,供大家参考,具体内容如下
先看看,js超简单实现图片旋转:
var current = 180;//需要反转的角度
tank.style.transform = 'rotate('+current+'deg)';//在style里的transform赋值'rotate('+current+'deg)'
–附:简易的小坦克移动js小游戏 (注:键盘上的上下左右键 键值分别是37、38、39、40)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id='container'>
<img src="tank.png" alt="" id='tank'>
</div>
</body>
<script>
container.style="width:1000px;height:800px;border:3px solid;";
tank.style="width:200px;height:200px;position:relative;top:0px;left:0px;"
document.body.onkeydown=function(){
var current = 0;
var top = parseInt(tank.style.top);
var left = parseInt(tank.style.left);
var code = event.keyCode;
if(code == 37) {
current = 180;
tank.style.transform = 'rotate('+current+'deg)';
if(left <= 0) {
alert("您已经到最左边了!");
}
else{
tank.style.left = (left-10) +"px";
}
}
if(code == 38) {
current =270;
tank.style.transform = 'rotate('+current+'deg)';
if(top <= 0) {
alert("您已经到最上边了!");
}
else{
tank.style.top = (top-10) +"px";
}
}
if(code == 39) {
current = 0;
tank.style.transform = 'rotate('+current+'deg)';
if(left+parseInt(tank.width)+6 >= 1000) {
alert("您已经到最右边了!");
}
else{
tank.style.left = (left+10) +"px";
}
}
if(code == 40) {
current = 90;
tank.style.transform = 'rotate('+current+'deg)';
if(top+parseInt(tank.width)+6 >= 800) {
alert("您已经到最下边了!");
}
else{
tank.style.top = (top+10) +"px";
}
}
}
</script>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
js 坦克移动