js實(shí)現(xiàn)彈幕飛機(jī)效果
更新時間:2020年08月27日 14:08:46 作者:彎月liang
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)彈幕飛機(jī)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了js實(shí)現(xiàn)彈幕飛機(jī)效果的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
body{
width: 70vw;/*長寬最好是obj的倍數(shù)*/
height: 90vh;
border-width: 10px;
border-style: solid;
border-color: blue;
line-height:600px;/*文本垂直居中*/
text-align: center;/*文本水平居中*/
position: relative;/*相對定位*/
left: 0px;
top: 0px;
}
/*開場動畫*/
@-webkit-keyframes mymove
{
from {top:50vh;}
to {top:100px;}
}
#obj{
-webkit-animation-name:mymove;
-webkit-animation-duration:1s;
-webkit-animation-timing-function:linear;
position: absolute;
left: 30vw;
top: 50vh;
width: 0px;
height: 0px;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-bottom: 10px solid red;
}
div{
text-align: center;
line-height:30px;
}
</style>
</head>
<body>
<!--彈幕飛機(jī)
1.飛機(jī)可以移動
2.屏幕頂部隨機(jī)彈幕雨
3.彈幕雨碰到飛機(jī)-失敗
4.記錄分?jǐn)?shù)
-->
<div id='obj'>飛機(jī)</div>
<button id='start'>開始</button> |
<button onclick="stop()">暫停</button>
</body>
<script type="text/javascript">
var key = document.body.onkeydown =f; //注冊keydown事件處理函數(shù)
var clientH= document.body.clientHeight;//獲取body高
var clientW= document.body.clientWidth;//獲取body寬
var obj=document.getElementById('obj');//飛機(jī)對象
var borderX=parseInt(getComputedStyle(obj,null).getPropertyValue('border-left'));
var borderY=parseInt(getComputedStyle(obj,null).getPropertyValue('border-bottom'));
var movePx=10;//飛機(jī)每次移動的距離
var speed=500;//雨下落速度
var distance=10;//雨下落距離
var rainleft=0;//彈幕雨x坐標(biāo)
var raintop=0;//彈幕雨y坐標(biāo)
//生成雨
function setrain(){
rainleft=parseInt(Math.random()*clientW);
raintop=0;//parseInt(Math.random()*clientH);
let div=document.createElement('div');
div.className ='div';
div.style.borderRadius='50%';
div.style.width='6px';
div.style.height='10px';
div.style.backgroundColor='pink';
div.style.position = 'absolute';
div.style.left=rainleft + 'px';
div.style.top=raintop + 'px';
document.body.appendChild(div);
}
//雨下落
function downrain(){
var myTop=parseInt(getComputedStyle(obj,null).getPropertyValue('top'));//獲取精靈y坐標(biāo)
var myLeft=parseInt(getComputedStyle(obj,null).getPropertyValue('left'));//獲取精靈x坐標(biāo)
let div=document.getElementsByClassName('div');
//遍歷all雨滴
for(let i=0;i<div.length-1;i++){
let divleft=parseInt(div[i].style.left);
let divtop=parseInt(div[i].style.top);
div[i].style.top=divtop+distance+'px';
//判斷飛機(jī)是否被擊中
if(Math.abs(divtop-myTop)<borderY && Math.abs(divleft-myLeft)<borderX){
console.log('被擊中了 borderY:'+borderY+' borderX:'+borderX);
console.log('------- myTop:'+myTop+' myLeft:'+myLeft);
console.log('------- rainY:'+divtop+' rainX:'+divleft);
stop();
alert('被擊中了');
}
}
}
//清除落地的雨
function delrain(){
let div=document.getElementsByClassName('div');
//遍歷all雨滴
for(let i=0;i<div.length-1;i++){
// div[i].style.left
if(parseInt(div[i].style.top)>clientH){
div[i].parentNode.removeChild(div[i]);
};
}
}
//開始
document.getElementById('start').onclick=start;
function start(e){
var e = e || window.event; //標(biāo)準(zhǔn)化事件處理
inter=setInterval((setrain),speed);
inter1=setInterval((downrain),speed);
inter2=setInterval((delrain),speed);
}
//暫停
function stop(){
clearInterval(inter);
clearInterval(inter1);
clearInterval(inter2);
}
//移動飛機(jī)
function f (va) {
var e = e || window.event; //標(biāo)準(zhǔn)化事件處理
let s = '';//val.type + " " + val.key; //獲取鍵盤事件類型和按下的值
let key=va.key;
var myTop=parseInt(getComputedStyle(obj,null).getPropertyValue('top'));//獲取精靈y坐標(biāo) parseInt(obj.style.top);
var myLeft=parseInt(getComputedStyle(obj,null).getPropertyValue('left'));//獲取精靈x坐標(biāo) parseInt(obj.style.left);
var myWidth=borderX;
var myHeight=borderY;
var move=0;
if(key=='w'){
move=myTop-movePx;//每次移動10
if(move<0 || move>clientH){
return false;//不能超過邊界
}
obj.style.top=move+'px';
s='上';
}
if(key=='s'){
move=myTop+movePx;
if(move<0 || move>clientH-myHeight){
return false;
}
obj.style.top=move+'px';
s='下';
}
if(key=='a'){
move=myLeft-movePx;
if(move<0 || move>clientW){
return false;
}
obj.style.left=move+'px';
s='左';
}
if(key=='d'){
move=myLeft+movePx;
if(move<0 || move>clientW-myWidth){
return false;
}
obj.style.left=move+'px';
s='右';
}
// obj.innerText=s;//設(shè)置文本 & 清楚之前的元素
// console.log(move+' top:'+myTop+' left:'+myLeft);
} /*f() end--*/
</script>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
JS實(shí)現(xiàn)在狀態(tài)欄顯示打字效果完整實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)在狀態(tài)欄顯示打字效果的方法,涉及JavaScript中字符遍歷結(jié)合時間函數(shù)對狀態(tài)欄顯示進(jìn)行操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
腳本吧 - 幻宇工作室用到j(luò)s,超強(qiáng)推薦base.js
腳本吧 - 幻宇工作室用到j(luò)s,超強(qiáng)推薦base.js...2006-12-12
uniapp中實(shí)現(xiàn)App自動檢測版本升級的示例代碼
本文主要介紹了uniapp中實(shí)現(xiàn)App自動檢測版本升級的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
原生js實(shí)現(xiàn)autocomplete插件
這篇文章主要介紹了原生js實(shí)現(xiàn)autocomplete插件的相關(guān)資料,需要的朋友可以參考下2016-04-04
使用JavaScript實(shí)現(xiàn)獲取audio時長
這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript實(shí)現(xiàn)獲取audio時長,并且轉(zhuǎn)換為分鐘00:00:00格式,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04

