【HTML——简易 飞机大战 小游戏(效果+代码)】,高级前端开发工程师

}

/* 爆炸图片 */

img.boom {

position: absolute;

animation: opa 1s;

}

@keyframes opa {

0% {

opacity: 1;

}

25% {

opacity: 0.5;

}

50% {

opacity: 1;

}

100% {

opacity: 0;

}

}

/* prize */

.prize {

position: absolute;

width: 50px;

height: 50px;

font-size: 50px;

text-align: center;

line-height: 50px;

border: 1px dashed red;

color: red;

font-weight: bold;

animation: prize 1s infinite;

}

@keyframes prize {

0% {

transform: scale(1);

}

50% {

transform: scale(1.2);

}

100% {

transform: scale(1);

}

}

/* 得分板 */

.score {

position: absolute;

top: 0;

left: 0;

width: 70px;

height: 30px;

color: #fff;

font-size: 20px;

}

/* 记录得分 */

.record {

width: 60%;

height: 100px;

margin: 100px auto;

background: #000;

padding: 10px;

border: 4px double #fff;

color: #fff;

}

.record p {

height: 50px;

font-size: 40px;

font-weight: bold;

text-align: center;

color: skyblue;

}

/* 再来一次 */

.btn {

position: absolute;

bottom: 50px;

right: 0;

left: 0;

margin: auto;

width: 150px;

height: 40px;

text-align: center;

line-height: 40px;

color: #fff;

font-size: 20px;

font-weight: bold;

border-radius: 10px;

background: #000;

cursor: pointer;

}

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 HTML 飞机大战游戏代码: ```html <!DOCTYPE html> <html> <head> <title>飞机大战</title> <style> canvas { border: 1px solid black; } </style> </head> <body> <canvas id="gameCanvas" width="800" height="600"></canvas> <script> // 获取画布和上下文 var canvas = document.getElementById("gameCanvas"); var ctx = canvas.getContext("2d"); // 定义飞机属性 var plane = { x: canvas.width / 2, y: canvas.height - 50, width: 50, height: 50, speed: 5 }; // 监听键盘事件 document.addEventListener("keydown", function(event) { if (event.keyCode === 37) { // 左箭头 plane.x -= plane.speed; } else if (event.keyCode === 39) { // 右箭头 plane.x += plane.speed; } }); // 定义子弹属性 var bullets = []; var bulletSpeed = 10; function createBullet() { bullets.push({ x: plane.x + plane.width / 2, y: plane.y, width: 5, height: 10, speed: bulletSpeed }); } // 定义敌机属性 var enemies = []; var enemySpeed = 3; function createEnemy() { enemies.push({ x: Math.random() * (canvas.width - 50), y: 0, width: 50, height: 50, speed: enemySpeed }); } // 游戏循环 var score = 0; setInterval(function() { // 清除画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 绘制飞机 ctx.fillStyle = "blue"; ctx.fillRect(plane.x, plane.y, plane.width, plane.height); // 绘制子弹 ctx.fillStyle = "red"; bullets.forEach(function(bullet) { ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height); bullet.y -= bullet.speed; }); // 绘制敌机 ctx.fillStyle = "green"; enemies.forEach(function(enemy) { ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height); enemy.y += enemy.speed; }); // 检测子弹和敌机是否碰撞 bullets.forEach(function(bullet) { enemies.forEach(function(enemy, i) { if (bullet.x < enemy.x + enemy.width && bullet.x + bullet.width > enemy.x && bullet.y < enemy.y + enemy.height && bullet.y + bullet.height > enemy.y) { bullets.splice(bullets.indexOf(bullet), 1); enemies.splice(i, 1); score++; } }); }); // 检测敌机是否撞到飞机 enemies.forEach(function(enemy) { if (enemy.x < plane.x + plane.width && enemy.x + enemy.width > plane.x && enemy.y < plane.y + plane.height && enemy.y + enemy.height > plane.y) { alert("游戏结束!得分:" + score); location.reload(); } }); // 创建子弹和敌机 createBullet(); if (score >= 10) { createEnemy(); } }, 30); </script> </body> </html> ``` 这只是一个简单的示例,你可以根据自己的需求和技能来进行改进和扩展,比如添加更多的敌机、增加游戏难度等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值