<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>飞机大战
</title>
<style>
* {
margin: 0px
}
.container {
height: 700px;
width: 500px;
background-color: black;
margin: 5px auto;
position: relative;
}
.plane {
height: 80px;
width: 80px;
background: url(images/plane.png) no-repeat center / 100% 100%;
position: absolute;
bottom: 10px;
left: calc(50% - 40px);
}
.bullet {
height: 10px;
width: 5px;
border-radius: 45% 45% 0 0;
box-shadow: 0px 2px 10px orange;
background: gold;
position: absolute;
}
.enemy {
height: 34.4px;
width: 32.5px;
background: url(images/enemy.png) no-repeat center / 100% 100%;
transform: rotate(180deg);
position: absolute;
overflow: hidden;
top: 0px;
}
h2 {
height: 40px;
display: table;
border-color: deepskyblue;
border-radius: 5px;
background-color: deepskyblue;
text-align: center;
padding: 5px;
position: relative;
float: right;
right: 300px;
}
</style>
<script src="jquery-3.4.1.js"></script>
</head>
<body>
<h2>得分:
<span id="score" style="color: white;">0
</span>
</h2>
<div class="container">
<div class="plane">
</div>
</div>
<script type="text/javascript">
$(function () {
var bulletCreateInterval = 300;
var bulletMoveInterval = 100;
var bulletSpeed = 10;
var enemyCreateInterval = 2000;
var enemyMoveInterval = 100;
var enemySpeed = 5;
var bGamePlaying = false;
var score = 0;
var endTime = new Date();
var calcPosition = (left, top, maxLeft, maxTop, minLeft = 0, minTop = 0) => {
left = left < minLeft ? minLeft : left > maxLeft ? maxLeft : left;
top = top < minTop ? minTop : top > maxTop ? maxTop : top;
return {left, top};
}
var getDomTRBL = (dom) => {
var bounds = {};
bounds.left = dom.offsetLeft;
bounds.right = dom.offsetLeft + dom.offsetWidth;
bounds.top = dom.offsetTop;
bounds.bottom = dom.offsetTop + dom.offsetHeight;
return bounds;
}
var calcHit = (div1, div2) => {
var bounds1 = getDomTRBL(div1);
var bounds2 = getDomTRBL(div2);
if (bounds1.left >= bounds2.left && bounds1.left <= bounds2.right) {
if (bounds1.top >= bounds2.top && bounds1.top <= bounds2.bottom) {
return true;
}
else if (bounds1.bottom >= bounds2.top && bounds1.bottom <= bounds2.bottom) {
return true;
}
}
else if (bounds1.right >= bounds2.left && bounds1.right <= bounds2.right) {
if (bounds1.top >= bounds2.top && bounds1.top <= bounds2.bottom) {
return true;
}
else if (bounds1.bottom >= bounds2.top && bounds1.bottom <= bounds2.bottom) {
return true;
}
}
return false;
}
var shoot = () => {
if (new Date() - endTime < bulletCreateInterval) {
return false;
}
var planeLF = $(".plane").position().left;
var planeTP = $(".plane").position().top;
var bullet = $("<div></div>").addClass("bullet");
$(".container").append(bullet);
var bulletLF = planeLF + $(".plane").innerWidth() / 2 - bullet.innerWidth() / 2;
var bulletTP = planeTP - $(".plane").innerHeight() / 2 + 20;
bullet.css("left", bulletLF).css("top", bulletTP);
endTime = new Date();
return true;
}
$(window).keydown(function (e) {
if (e.keyCode == 13) {
bGamePlaying = true;
console.log("game start!")
}
if (!bGamePlaying) return;
var tp = $(".plane").position().top;
var lf = $(".plane").position().left;
switch (e.keyCode) {
case 87:// w
tp -= 10;
break;
case 83:// s
tp += 10;
break;
case 65:// a
lf -= 10;
break;
case 68:// d
lf += 10;
break;
case 74:// j
shoot();
break;
}
var maxLeft = $(".container").innerWidth() - $(".plane").innerWidth();
var maxTop = $(".container").innerHeight() - $(".plane").innerHeight();
var position = calcPosition(lf, tp, maxLeft, maxTop);
$(".plane").css("left", position.left).css("top", position.top);
});
var containerBounds = getDomTRBL($(".container")[0]);
$(document).mousemove(function (e) {
if (!bGamePlaying) return;
var tp = e.pageY;
var lf = e.pageX;
if (tp >= containerBounds.top && tp <= containerBounds.bottom && lf >= containerBounds.left && lf <= containerBounds.right) {
tp -= containerBounds.top;
lf -= containerBounds.left;
}
else return;
tp -= $(".plane").innerHeight() / 2;
lf -= $(".plane").innerWidth() / 2;
var maxLeft = $(".container").innerWidth() - $(".plane").innerWidth();
var maxTop = $(".container").innerHeight() - $(".plane").innerHeight();
var position = calcPosition(lf, tp, maxLeft, maxTop);
$(".plane").css("left", position.left).css("top", position.top);
});
$(window).click(() => {
if (!bGamePlaying) {
bGamePlaying = true;
}
shoot();
});
var enemyCreateTimer = setInterval(() => {
var enemy = $("<div></div>").addClass("enemy").css("top", 0);
$(".container").append(enemy);
var left = Math.round(Math.random() * ($(".container").innerWidth() - $(".enemy").innerWidth()));
enemy.css("left", left);
}, enemyCreateInterval);
var bulletTimer = setInterval(() => {
$(".bullet").each((index, element) => {
var bullet = $(element);
bullet.css("top", bullet.position().top - bulletSpeed);
if (bullet.position().top < 0) {
bullet.remove();
}
});
}, bulletMoveInterval);
var enemyTimer = setInterval(() => {
$(".enemy").each((index, element) => {
var enemy = $(element);
enemy.css("top", enemy.position().top + enemySpeed);
if (enemy.position().top > $(".container").innerHeight()) {
enemy.remove();
}
});
}, enemyMoveInterval);
var mainTimer = setInterval(() => {
var plane = $(".plane").get(0);
$(".enemy").each(function (index, enemy) {
if (calcHit(plane, enemy) || calcHit(enemy, plane)) {
stopGame();
return;
}
$(".bullet").each((index, bullet) => {
if (calcHit(enemy, bullet) || calcHit(bullet, enemy)) {
enemy.remove();
bullet.remove();
score += 10;
$("#score").text(score);
}
});
});
}, 50);
var stopGame = () => {
bGamePlaying = false;
clearInterval(enemyCreateTimer);
clearInterval(enemyTimer);
clearInterval(bulletTimer);
clearInterval(mainTimer);
alert("游戏结束!你的积分为" + score);
}
});
</script>
</body>
</html>
转载请注明原文地址: https://win8.8miu.com/read-26275.html