用Deepseek写一个 HTML 和 JavaScript 实现一个简单的飞机游戏

embedded/2025/3/18 8:21:31/
htmledit_views">

大家好!今天我将分享如何使用 HTML 和 JavaScript 编写一个简单的飞机游戏。这个游戏的核心功能包括:控制飞机移动、发射子弹、敌机生成、碰撞检测和得分统计。代码简洁易懂,适合初学者学习和实践。

游戏功能概述

  1. 玩家控制:使用键盘的  和  键控制飞机的左右移动,按下 空格键 发射子弹。

  2. 敌机生成:每隔 1 秒生成一个敌机,敌机从屏幕顶部随机位置向下移动。

  3. 碰撞检测:子弹击中敌机后,敌机和子弹都会消失,并增加 10 分;如果玩家飞机与敌机碰撞,游戏结束。

  4. 得分统计:击中敌机后,得分会显示在屏幕左上角。

实现步骤

1. HTML 结构

我们使用 <canvas> 元素作为游戏画布,所有的游戏元素(如飞机、子弹、敌机)都将在画布上绘制。

html"><!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>飞机游戏</title><style>body {margin: 0;overflow: hidden;background-color: #000;}canvas {display: block;margin: 0 auto;background-color: #000;}</style>
</head>
<body><canvas id="gameCanvas"></canvas><script>// JavaScript 代码将在下面实现</script>
</body>
</html>
2. JavaScript 逻辑

JavaScript 部分负责实现游戏的核心逻辑,包括玩家控制、敌机生成、碰撞检测和得分统计。

javascript">const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');canvas.width = 480;
canvas.height = 640;// 飞机对象
const player = {x: canvas.width / 2 - 25,y: canvas.height - 60,width: 50,height: 50,speed: 5,color: '#00FF00',bullets: [],fire: function() {this.bullets.push({ x: this.x + this.width / 2 - 2.5, y: this.y, width: 5, height: 10, color: '#FF0000' });}
};// 敌机数组
const enemies = [];// 得分
let score = 0;// 监听键盘事件
document.addEventListener('keydown', (e) => {if (e.code === 'ArrowLeft' && player.x > 0) {player.x -= player.speed;}if (e.code === 'ArrowRight' && player.x < canvas.width - player.width) {player.x += player.speed;}if (e.code === 'Space') {player.fire();}
});// 生成敌机
function spawnEnemy() {const x = Math.random() * (canvas.width - 50);enemies.push({ x: x, y: -50, width: 50, height: 50, color: '#0000FF', speed: 2 });
}// 检测碰撞
function checkCollision(rect1, rect2) {return rect1.x < rect2.x + rect2.width &&rect1.x + rect1.width > rect2.x &&rect1.y < rect2.y + rect2.height &&rect1.y + rect1.height > rect2.y;
}// 更新游戏状态
function update() {// 移动子弹player.bullets.forEach((bullet, index) => {bullet.y -= 5;if (bullet.y < 0) {player.bullets.splice(index, 1);}});// 移动敌机enemies.forEach((enemy, index) => {enemy.y += enemy.speed;if (enemy.y > canvas.height) {enemies.splice(index, 1);}});// 检测子弹与敌机的碰撞player.bullets.forEach((bullet, bulletIndex) => {enemies.forEach((enemy, enemyIndex) => {if (checkCollision(bullet, enemy)) {player.bullets.splice(bulletIndex, 1);enemies.splice(enemyIndex, 1);score += 10;}});});// 检测玩家与敌机的碰撞enemies.forEach((enemy, index) => {if (checkCollision(player, enemy)) {alert('游戏结束!得分:' + score);document.location.reload();}});
}// 绘制游戏元素
function draw() {ctx.clearRect(0, 0, canvas.width, canvas.height);// 绘制玩家飞机ctx.fillStyle = player.color;ctx.fillRect(player.x, player.y, player.width, player.height);// 绘制子弹ctx.fillStyle = '#FF0000';player.bullets.forEach(bullet => {ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);});// 绘制敌机ctx.fillStyle = '#0000FF';enemies.forEach(enemy => {ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);});// 绘制得分ctx.fillStyle = '#FFFFFF';ctx.font = '20px Arial';ctx.fillText('得分: ' + score, 10, 30);
}// 游戏主循环
function gameLoop() {update();draw();requestAnimationFrame(gameLoop);
}// 定时生成敌机
setInterval(spawnEnemy, 1000);// 启动游戏
gameLoop();
3. 运行游戏

将上述代码保存为一个 .html 文件,然后在浏览器中打开即可运行游戏。你可以使用键盘控制飞机,体验游戏的乐趣!

扩展与优化

这个游戏是一个简单的示例,你可以在此基础上进行扩展和优化,例如:

  1. 增加敌机类型:添加不同速度和血量的敌机。

  2. 添加音效:为子弹发射、击中敌机和游戏结束添加音效。

  3. 提升游戏难度:随着时间推移,逐渐增加敌机的生成速度和数量。

  4. 添加背景音乐:为游戏增加背景音乐,提升沉浸感。

总结

通过这个简单的飞机游戏项目,你可以学习到如何使用 HTML 和 JavaScript 实现基本的游戏逻辑,包括玩家控制、碰撞检测和动态生成游戏元素。希望这篇文章对你有所帮助,也欢迎大家在评论区分享你的改进和优化建议!

如果你对游戏开发感兴趣,不妨尝试扩展这个项目,或者基于此开发更多有趣的游戏!🚀

完整代码

html"><!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>飞机游戏</title><style>body {margin: 0;overflow: hidden;background-color: #000;}canvas {display: block;margin: 0 auto;background-color: #000;}</style>
</head>
<body><canvas id="gameCanvas"></canvas><script>const canvas = document.getElementById('gameCanvas');const ctx = canvas.getContext('2d');canvas.width = 480;canvas.height = 640;// 飞机对象const player = {x: canvas.width / 2 - 25,y: canvas.height - 60,width: 50,height: 50,speed: 5,color: '#00FF00',bullets: [],fire: function() {this.bullets.push({ x: this.x + this.width / 2 - 2.5, y: this.y, width: 5, height: 10, color: '#FF0000' });}};// 敌机数组const enemies = [];// 得分let score = 0;// 监听键盘事件document.addEventListener('keydown', (e) => {if (e.code === 'ArrowLeft' && player.x > 0) {player.x -= player.speed;}if (e.code === 'ArrowRight' && player.x < canvas.width - player.width) {player.x += player.speed;}if (e.code === 'Space') {player.fire();}});// 生成敌机function spawnEnemy() {const x = Math.random() * (canvas.width - 50);enemies.push({ x: x, y: -50, width: 50, height: 50, color: '#0000FF', speed: 2 });}// 检测碰撞function checkCollision(rect1, rect2) {return rect1.x < rect2.x + rect2.width &&rect1.x + rect1.width > rect2.x &&rect1.y < rect2.y + rect2.height &&rect1.y + rect1.height > rect2.y;}// 更新游戏状态function update() {// 移动子弹player.bullets.forEach((bullet, index) => {bullet.y -= 5;if (bullet.y < 0) {player.bullets.splice(index, 1);}});// 移动敌机enemies.forEach((enemy, index) => {enemy.y += enemy.speed;if (enemy.y > canvas.height) {enemies.splice(index, 1);}});// 检测子弹与敌机的碰撞player.bullets.forEach((bullet, bulletIndex) => {enemies.forEach((enemy, enemyIndex) => {if (checkCollision(bullet, enemy)) {player.bullets.splice(bulletIndex, 1);enemies.splice(enemyIndex, 1);score += 10;}});});// 检测玩家与敌机的碰撞enemies.forEach((enemy, index) => {if (checkCollision(player, enemy)) {alert('游戏结束!得分:' + score);document.location.reload();}});}// 绘制游戏元素function draw() {ctx.clearRect(0, 0, canvas.width, canvas.height);// 绘制玩家飞机ctx.fillStyle = player.color;ctx.fillRect(player.x, player.y, player.width, player.height);// 绘制子弹ctx.fillStyle = '#FF0000';player.bullets.forEach(bullet => {ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);});// 绘制敌机ctx.fillStyle = '#0000FF';enemies.forEach(enemy => {ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);});// 绘制得分ctx.fillStyle = '#FFFFFF';ctx.font = '20px Arial';ctx.fillText('得分: ' + score, 10, 30);}// 游戏主循环function gameLoop() {update();draw();requestAnimationFrame(gameLoop);}// 定时生成敌机setInterval(spawnEnemy, 1000);// 启动游戏gameLoop();</script>
</body>
</html>


http://www.ppmy.cn/embedded/173534.html

相关文章

基于javaweb的SpringBoot校园运动会管理系统设计与实现(源码+文档+部署讲解)

技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;免费功能设计、开题报告、任务书、中期检查PPT、系统功能实现、代码编写、论文编写和辅导、论…

网络华为HCIA+HCIP数据链路层协议-以太网协议

以太网协议 以太网是当今现有局域网(Local Area Network,LAN)采用的最通用的通信协议标准&#xff0c;该标准定义了在局域网中采用的电缆类型和信号处理方法。以太网是建立在CSMA/CD(Carrier Sense Multiple Access/Collision Detection,载波监听多路访问/冲突检测)机制上的广…

Bash中小数的大小比较以及if条件中小数的大小判断

1、在Bash中对小数进行大小判断时&#xff0c;需要使用bc命令进行判断&#xff0c;用-gt、-lt、-eq等或使用>、<、运算符比较。 注意&#xff1a;用bc命令比较时&#xff0c;真返回1&#xff0c;假返回0。 [rootCentos7-4 ~]# [ echo "120.5 > 88.8" | bc…

新能源汽车IGBT电压平台与SiC器件应用

一、引言 随着全球环保意识的增强和能源危机的加剧&#xff0c;新能源汽车&#xff08;包括纯电动汽车和插电式混合动力汽车&#xff09;市场迅速崛起。作为新能源汽车的核心动力系统&#xff0c;电机控制器在提升车辆性能、降低能耗方面发挥着至关重要的作用。目前&#xff0…

蓝桥杯:信号覆盖

本题的考点是模拟&#xff0c;我们通过枚举每个点与信号塔之间的距离&#xff0c;与半径进行比较&#xff0c;如果半径大于距离&#xff0c;那么该点可以被覆盖&#xff0c;计数器加一&#xff0c;由二维空间&#xff0c;两点之间的距离公式计算每个点与信号塔之间的距离double…

蓝桥杯刷题——第十五届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组

一、0握手问题 - 蓝桥云课 算法代码&#xff1a; #include <iostream> using namespace std; int main() {int sum0;for(int i49;i>7;i--)sumi;cout<<sum<<endl;return 0; } 直接暴力&#xff0c;题意很清晰&#xff0c;累加即可。 二、0小球反弹 - 蓝…

50个常用的DeepSeek提示词

50个常用的DeepSeek提示词 今天有小伙伴反馈&#xff0c;用过我分享过的DeepSeek 提示词后&#xff0c;文章爆了多篇10w,今天先开开胃&#xff0c;来50个常用的DeepSeek提示词 建议大家点赞收藏&#xff01; 信息处理与创作类 核心功能&#xff1a;整理、分析和重构信息 新…

CSS - Pseudo-classes(伪类选择器)

目录 一、介绍二、常用种类三、案例实现案例一&#xff1a;a标签使用link/visited/hover/active案例二&#xff1a;表单元素使用focus/disabled案例三、通过其余伪类实现元素灵活选中 一、介绍 CSS 伪类&#xff08;Pseudo-classes&#xff09; 用于定义元素的特定状态或结构位…