js實現坦克移動小游戲
更新時間:2019年10月28日 11:16:51 作者:KD-倘若沒有倘若
這篇文章主要為大家詳細介紹了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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
window.setInterval()方法的定義和用法及offsetLeft與style.left的區(qū)別
window.setInterval()方法可以按照指定的周期執(zhí)行來執(zhí)行一段程序。周期是以毫秒為單位的,本文給大家介紹window.setInterval()方法的定義和用法,感興趣的朋友參考下2015-11-11