当前位置: 首页 > news >正文

html小游戏-飞机大战

在这里插入图片描述

敌机图片:在这里插入图片描述


子弹图片:在这里插入图片描述


我方飞机:在这里插入图片描述


目录结构
在这里插入图片描述

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>飞机大战</title><style>* {margin: 0;padding: 0;}#gameContainer {width: 400px;height: 600px;background: #000033;position: relative;overflow: hidden;margin: 20px auto;}#plane {width: 50px;height: 50px;position: absolute;bottom: 50px;left: 175px;}.bullet {width: 10px;height: 20px;position: absolute;z-index: 10;}.enemy {width: 40px;height: 40px;position: absolute;}#score {position: absolute;top: 10px;left: 10px;color: white;font-size: 20px;z-index: 100;font-family: Arial, "Microsoft YaHei", sans-serif;}#instructions {text-align: center;margin: 10px;font-family: Arial, "Microsoft YaHei", sans-serif;}</style>
</head>
<body>
<div id="instructions">方向键移动,按住空格键连续发射三发子弹
</div>
<div id="gameContainer"><div id="score">得分: <span id="scoreValue">0</span></div><img id="plane" src="1.png" alt="飞机">
</div><script>const plane = document.getElementById('plane');const gameContainer = document.getElementById('gameContainer');const scoreElement = document.getElementById('scoreValue');let planeLeft = 175;let planeTop = 500;let score = 0;const horizontalSpeed = 2.5;const verticalSpeed = 3.5;const bullets = [];const enemies = [];const keys = {};let isSpacePressed = false;let lastShotTime = 0;const shootCooldown = 150;document.addEventListener('keydown', (e) => {keys[e.key] = true;if(e.key === ' ') {isSpacePressed = true;e.preventDefault();}});document.addEventListener('keyup', (e) => {keys[e.key] = false;if(e.key === ' ') {isSpacePressed = false;}});// 创建子弹组function createBullets() {// 子弹间距const spacing = 15;// 创建三发子弹for(let i = -1; i <= 1; i++) {const bullet = document.createElement('img');bullet.src = '2.png';bullet.className = 'bullet';// 计算子弹位置,中间子弹在飞机正上方,两侧子弹略微偏移const bulletLeft = planeLeft + plane.offsetWidth/2 - 5 + (i * spacing);bullet.style.left = bulletLeft + 'px';bullet.style.top = (planeTop - 20) + 'px';gameContainer.appendChild(bullet);// 为两侧子弹添加横向运动const horizontalSpeed = i * 0.5; // 子弹横向扩散速度bullets.push({element: bullet,top: planeTop - 20,left: bulletLeft,horizontalSpeed: horizontalSpeed // 新增横向速度属性});}}function createEnemy() {const enemy = document.createElement('img');enemy.src = '3.png';enemy.className = 'enemy';enemy.style.left = Math.random() * (gameContainer.offsetWidth - 40) + 'px';enemy.style.top = '-40px';gameContainer.appendChild(enemy);enemies.push({element: enemy,top: -40});}function isColliding(rect1, rect2) {return !(rect1.right < rect2.left ||rect1.left > rect2.right ||rect1.bottom < rect2.top ||rect1.top > rect2.bottom);}function gameLoop() {const currentTime = Date.now();if (isSpacePressed && currentTime - lastShotTime >= shootCooldown) {createBullets();lastShotTime = currentTime;}if(keys['ArrowLeft'] && planeLeft > 0) {planeLeft -= horizontalSpeed;}if(keys['ArrowRight'] && planeLeft < gameContainer.offsetWidth - plane.offsetWidth) {planeLeft += horizontalSpeed;}if(keys['ArrowUp'] && planeTop > 0) {planeTop -= verticalSpeed;}if(keys['ArrowDown'] && planeTop < gameContainer.offsetHeight - plane.offsetHeight) {planeTop += verticalSpeed;}plane.style.left = planeLeft + 'px';plane.style.top = planeTop + 'px';// 更新子弹位置for(let i = bullets.length - 1; i >= 0; i--) {const bullet = bullets[i];bullet.top -= 8; // 垂直移动速度bullet.left += bullet.horizontalSpeed; // 添加横向移动// 更新子弹位置bullet.element.style.top = bullet.top + 'px';bullet.element.style.left = bullet.left + 'px';// 移除超出边界的子弹if(bullet.top <= -20 || bullet.left < -10 || bullet.left > gameContainer.offsetWidth) {bullet.element.remove();bullets.splice(i, 1);continue;}// 子弹碰撞检测for(let j = enemies.length - 1; j >= 0; j--) {const enemy = enemies[j];if(isColliding(bullet.element.getBoundingClientRect(),enemy.element.getBoundingClientRect())) {bullet.element.remove();bullets.splice(i, 1);enemy.element.remove();enemies.splice(j, 1);score += 100;scoreElement.textContent = score;break;}}}// 敌机移动for(let i = enemies.length - 1; i >= 0; i--) {const enemy = enemies[i];enemy.top += 2;enemy.element.style.top = enemy.top + 'px';if(enemy.top >= gameContainer.offsetHeight) {enemy.element.remove();enemies.splice(i, 1);continue;}if(isColliding(enemy.element.getBoundingClientRect(),plane.getBoundingClientRect())) {alert('游戏结束!\n最终得分:' + score);location.reload();return;}}requestAnimationFrame(gameLoop);}setInterval(createEnemy, 1500);gameLoop();
</script>
</body>
</html>

http://www.mrgr.cn/news/58022.html

相关文章:

  • 机器学习——数据隐私与安全学习
  • Android 中的串口开发
  • Hallo2 长视频和高分辨率的音频驱动的肖像图像动画 (数字人技术)
  • 论文《Text2SQL is Not Enough: Unifying AI and Databases with TAG》
  • 关于java继承(深入解析父类属性的抽取与构造函数的作用)
  • 力扣4:寻找两个正序数的中位数
  • C++之模板进阶
  • 洞察前沿趋势!2024深圳国际金融科技大赛——西丽湖金融科技大学生挑战赛技术公开课指南
  • DOTween动画插件超详解(保姆级巨细)
  • Java项目-基于springboot框架的人职匹配推荐系统项目实战(附源码+文档)
  • QImage和QPixmap
  • 大数据Azkaban(二):Azkaban简单介绍
  • Redis缓存技术 基础第三篇(Redis实现短信验证)
  • 1.CentOS安装
  • NSSCTF(PWN)16
  • 6SV辐射传输模型编译和加入自定义传感器参数
  • 【模型部署】python中socket编程入门知识-系列1
  • 【Linux学习】(4)Linux权限
  • MyBatis-Plus(三):使用自定义目录下的xml时踩坑记录
  • CMake中的List关键词:详细指南
  • 数据结构与算法——Java实现 46. 从前序与中序遍历序列构造二叉树
  • 【多线程和高并发】多线程和高并发提纲
  • Gradle核心概念总结
  • 论软件著作权
  • 线性可分支持向量机代码实现
  • Python 代码的主要功能是从给定的日志文件和设备列表中提取特定设备(华为和华三)的用户账号信息