|
|
好的!我来帮你做一个简单的“飞机大战”网页游戏。你可以用它在浏览器里玩。这里会有一个小飞机,你可以用键盘左右移动,按空格键发射子弹,击中敌机得分。代码不复杂,我会加上注释,方便你理解。
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8" />
- <title>简单飞机大战</title>
- <style>
- body {
- margin: 0;
- overflow: hidden;
- background: #000; /* 黑色背景 */
- }
- canvas {
- display: block;
- margin: 0 auto;
- background: #111;
- border: 1px solid #555;
- }
- </style>
- </head>
- <body>
- <canvas id="gameCanvas" width="400" height="600"></canvas>
- <script>
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- // 小飞机
- const plane = {
- x: canvas.width / 2 - 15,
- y: canvas.height - 60,
- width: 30,
- height: 40,
- speed: 5,
- moveLeft: false,
- moveRight: false
- };
- // 子弹数组
- const bullets = [];
- // 敌机数组
- const enemies = [];
- let score = 0;
- let gameOver = false;
- // 监听键盘事件
- document.addEventListener('keydown', e => {
- if (e.code === 'ArrowLeft') plane.moveLeft = true;
- if (e.code === 'ArrowRight') plane.moveRight = true;
- if (e.code === 'Space') shootBullet();
- });
- document.addEventListener('keyup', e => {
- if (e.code === 'ArrowLeft') plane.moveLeft = false;
- if (e.code === 'ArrowRight') plane.moveRight = false;
- });
- // 发射子弹函数
- function shootBullet() {
- bullets.push({
- x: plane.x + plane.width / 2 - 2.5,
- y: plane.y,
- width: 5,
- height: 10,
- speed: 7
- });
- }
- // 生成敌机
- function spawnEnemy() {
- const size = 30;
- enemies.push({
- x: Math.random() * (canvas.width - size),
- y: -size,
- width: size,
- height: size,
- speed: 2 + Math.random() * 2
- });
- }
- // 每隔一秒生成一个敌机
- setInterval(() => {
- if (!gameOver) spawnEnemy();
- }, 1000);
- // 检测两个矩形是否相交(碰撞检测)
- function isColliding(a, b) {
- return a.x < b.x + b.width &&
- a.x + a.width > b.x &&
- a.y < b.y + b.height &&
- a.y + a.height > b.y;
- }
- // 游戏主循环
- function update() {
- if (gameOver) {
- ctx.fillStyle = 'white';
- ctx.font = '30px Arial';
- ctx.fillText('游戏结束', canvas.width / 2 - 70, canvas.height / 2);
- ctx.fillText('得分: ' + score, canvas.width / 2 - 50, canvas.height / 2 + 40);
- return;
- }
- // 清空画布
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- // 移动飞机
- if (plane.moveLeft && plane.x > 0) {
- plane.x -= plane.speed;
- }
- if (plane.moveRight && plane.x + plane.width < canvas.width) {
- plane.x += plane.speed;
- }
- // 画飞机(用绿色三角形表示)
- ctx.fillStyle = 'lime';
- ctx.beginPath();
- ctx.moveTo(plane.x + plane.width / 2, plane.y);
- ctx.lineTo(plane.x, plane.y + plane.height);
- ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
- ctx.closePath();
- ctx.fill();
- // 移动子弹
- for (let i = bullets.length - 1; i >= 0; i--) {
- let b = bullets[i];
- b.y -= b.speed;
- // 超出屏幕就删掉
- if (b.y + b.height < 0) {
- bullets.splice(i, 1);
- continue;
- }
- // 画子弹(黄色矩形)
- ctx.fillStyle = 'yellow';
- ctx.fillRect(b.x, b.y, b.width, b.height);
- }
- // 移动敌机
- for (let i = enemies.length - 1; i >= 0; i--) {
- let e = enemies[i];
- e.y += e.speed;
- // 如果敌机碰到飞机,游戏结束
- if (isColliding(e, plane)) {
- gameOver = true;
- }
- // 如果敌机跑到底部,游戏也结束
- else if (e.y > canvas.height) {
- gameOver = true;
- }
- // 画敌机(红色矩形)
- ctx.fillStyle = 'red';
- ctx.fillRect(e.x, e.y, e.width, e.height);
- }
- // 碰撞检测:子弹和敌机
- for (let i = enemies.length - 1; i >= 0; i--) {
- for (let j = bullets.length - 1; j >= 0; j--) {
- if (isColliding(enemies[i], bullets[j])) {
- enemies.splice(i, 1);
- bullets.splice(j, 1);
- score += 10;
- break;
- }
- }
- }
- // 显示分数
- ctx.fillStyle = 'white';
- ctx.font = '20px Arial';
- ctx.fillText('得分: ' + score, 10, 30);
- requestAnimationFrame(update);
- }
- update();
- </script>
- </body>
- </html>
复制代码
---
**怎么玩呢?**
- 用键盘左箭头和右箭头控制自己的飞机向左或向右移动。
- 按空格键发射子弹,射击从屏幕上方飞下来的红色方块敌人。
- 击中敌人能得分,没被敌人撞到,游戏继续。
- 如果敌人碰到你,或者敌人飞到屏幕底部,游戏结束,会显示你的得分。
你只要把上面的代码复制到记事本,保存成`plane.html`,然后用浏览器打开,就可以玩游戏啦!
如果你想玩得更精彩,可以告诉我,我来帮你加更炫的特效或者新的功能哦。怎么样? |
|