<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>Document
</title
>
<style
>
.container
{
width
: 500px
;
height
: 80vh
;
margin
: 10vh auto
;
background
: #
000;
position
: relative
;
}
.plane
{
width
: 80px
;
height
: 70px
;
background
: url("./1.png") no
-repeat
;
background
-size
: cover
;
position
: absolute
;
left
: calc(50% - 40px
);
bottom
: 20px
;
}
.bullet
{
width
: 8px
;
height
: 22px
;
background
-color
: gold
;
position
: absolute
;
border
-radius
: 10px
10px
0 0 ;
box
-shadow
: 0 5px
5px green
;
}
.enemy
{
width
: 40px
;
height
: 30px
;
background
: url("./1.png") no
-repeat
;
background
-size
: cover
;
position
: absolute
;
}
</style
>
</head
>
<body
>
<div
class="container">
<div
class="plane"></div
>
</div
>
<script src
="../JQ/JQuery.js"></script
>
<script
>
$(function () {
let maxLeft
= $('.container').innerWidth() - $('.plane').innerWidth()
let maxTop
= $('.container').innerHeight() - $('.plane').innerHeight()
$(window
).keydown(function ({keyCode
}) {
let {top
: t
,left
: l
} = $('.plane').position()
switch (keyCode
) {
case 38:
t
-= 10
break;
case 40:
t
+= 10
break;
case 37:
l
-= 10
break;
case 39:
l
+= 10
break;
case 74:
shoot()
break;
default:
break;
}
if(t
< 0) t
= 0
if(t
> maxTop
) t
= maxTop
if(l
< 0) l
= 0
if(l
> maxLeft
) l
= maxLeft
$('.plane').css('top',t
).css('left',l
)
})
let entTime
= new Date()
function shoot(){
if (new Date() - entTime
< 500) return;
let bullet
= $('<div/>').addClass('bullet')
$('.container').append(bullet
)
bullet
.css('top',$('.plane').position().top
- 15).css('left',$('.plane').position().left
+ $('.plane').innerWidth()/2 - bullet
.innerWidth()/2)
entTime
= new Date()
}
setInterval(() => {
$('.bullet').each(function(){
let bullet
= $(this)
let {top
} = bullet
.position()
if(top
< 0) bullet
.remove()
else bullet
.css('top',top
- 20)
})
},100)
setInterval(() => {
$('.enemy').each(function(){
let enemy
= this
if(calc(enemy
,$('.plane').get(0)) || calc($('.plane').get(0),enemy
)){
alert('GG')
location
.reload()
}
$('.bullet').each(function(){
let bullet
= this
if(calc(enemy
,bullet
)|| calc(bullet
,enemy
)) {
bullet
.remove()
enemy
.remove()
}
})
})
},50)
setInterval(() => {
let enemy
= $('<div/>').addClass('enemy')
$('.container').append(enemy
)
enemy
.css('top',0).css('left',Math
.round(Math
.random()*($('.container').innerWidth() - enemy
.innerWidth())))
},2000)
setInterval(() => {
$('.enemy').each(function(){
let enemy
= $(this)
let {top
} = enemy
.position()
if(top
> $('.container').innerHeight()) enemy
.remove()
else enemy
.css('top',top
+ 20)
})
},200)
function getLTRB(node
) {
return {
l
: node
.offsetLeft
,
t
: node
.offsetTop
,
r
: node
.offsetLeft
+ node
.offsetWidth
,
b
: node
.offsetTop
+ node
.offsetHeight
}
}
function calc(a
, b
) {
a
= getLTRB(a
)
b
= getLTRB(b
)
if (b
.l
> a
.l
&& b
.l
< a
.r
&& b
.t
> a
.t
&& b
.t
< a
.b
) return true
else if (b
.l
> a
.l
&& b
.l
< a
.r
&& b
.b
> a
.t
&& b
.b
< a
.b
) return true
else if (b
.r
> a
.l
&& b
.r
< a
.r
&& b
.b
> a
.t
&& b
.b
< a
.b
) return true
else if (b
.r
> a
.l
&& b
.r
< a
.r
&& b
.t
> a
.t
&& b
.t
< a
.b
) return true
else return false
}
})
</script
>
</body
>
</html
>
转载请注明原文地址:https://ipadbbs.8miu.com/read-10262.html