|
|
好的!我帮你写一个简单的“飞机大战”网页游戏代码哦。这个游戏里,你可以用键盘左右键控制飞机移动,按空格键发射子弹,打击敌机。
下面是完整的代码,你可以把它复制到一个 `.html` 文件里,然后用浏览器打开就能玩:
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8" />
- <title>简单飞机大战游戏</title>
- <style>
- body {
- margin: 0;
- background: #000;
- overflow: hidden;
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- color: white;
- font-family: Arial, sans-serif;
- }
- canvas {
- background: #111;
- display: block;
- border: 2px solid #555;
- }
- #score {
- position: absolute;
- top: 10px;
- left: 10px;
- font-size: 20px;
- z-index: 10;
- }
- </style>
- </head>
- <body>
- <div id="score">得分: 0</div>
- <canvas id="gameCanvas" width="400" height="600"></canvas>
- <script>
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- // 游戏对象和参数
- const planeWidth = 40;
- const planeHeight = 40;
- const bulletWidth = 5;
- const bulletHeight = 10;
- const enemyWidth = 40;
- const enemyHeight = 40;
- let score = 0;
- const scoreDisplay = document.getElementById('score');
- // 玩家飞机位置(初始居中底部)
- let plane = {
- x: canvas.width / 2 - planeWidth / 2,
- y: canvas.height - planeHeight - 10,
- speed: 5,
- movingLeft: false,
- movingRight: false,
- };
- // 子弹数组
- let bullets = [];
- // 敌机数组
- let enemies = [];
- // 生成敌机函数
- function createEnemy() {
- const x = Math.random() * (canvas.width - enemyWidth);
- enemies.push({
- x: x,
- y: -enemyHeight,
- speed: 2 + Math.random() * 2, // 2 到 4 之间的速度
- });
- }
- // 键盘事件监听
- window.addEventListener('keydown', function(e) {
- if (e.key === 'ArrowLeft') {
- plane.movingLeft = true;
- } else if (e.key === 'ArrowRight') {
- plane.movingRight = true;
- } else if (e.key === ' ') {
- // 发射子弹
- bullets.push({
- x: plane.x + planeWidth / 2 - bulletWidth / 2,
- y: plane.y,
- speed: 7,
- });
- }
- });
- window.addEventListener('keyup', function(e) {
- if (e.key === 'ArrowLeft') {
- plane.movingLeft = false;
- } else if (e.key === 'ArrowRight') {
- plane.movingRight = false;
- }
- });
- // 检测碰撞函数
- function isCollide(a, b) {
- return !(
- a.x + a.width < b.x ||
- a.x > b.x + b.width ||
- a.y + a.height < b.y ||
- a.y > b.y + b.height
- );
- }
- // 游戏主循环
- let enemyTimer = 0;
- const enemyInterval = 60; // 每60帧产生一个敌机(大约1秒)
- function gameLoop() {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- // 移动飞机
- if (plane.movingLeft) {
- plane.x -= plane.speed;
- if (plane.x < 0) plane.x = 0;
- }
- if (plane.movingRight) {
- plane.x += plane.speed;
- if (plane.x > canvas.width - planeWidth) plane.x = canvas.width - planeWidth;
- }
- // 画飞机(简单三角形)
- ctx.fillStyle = '#0f0';
- ctx.beginPath();
- // 飞机是个向上的三角形
- ctx.moveTo(plane.x + planeWidth / 2, plane.y);
- ctx.lineTo(plane.x, plane.y + planeHeight);
- ctx.lineTo(plane.x + planeWidth, plane.y + planeHeight);
- ctx.closePath();
- ctx.fill();
- // 更新并画子弹
- for (let i = bullets.length - 1; i >= 0; i--) {
- const b = bullets[i];
- b.y -= b.speed;
- if (b.y < 0) {
- bullets.splice(i, 1);
- continue;
- }
- ctx.fillStyle = '#ff0';
- ctx.fillRect(b.x, b.y, bulletWidth, bulletHeight);
- }
- // 产生敌机
- enemyTimer++;
- if (enemyTimer >= enemyInterval) {
- createEnemy();
- enemyTimer = 0;
- }
- // 更新并画敌机
- for (let i = enemies.length - 1; i >= 0; i--) {
- const e = enemies[i];
- e.y += e.speed;
- if (e.y > canvas.height) {
- enemies.splice(i, 1); // 超出屏幕删除敌机
- continue;
- }
- ctx.fillStyle = '#f00';
- ctx.fillRect(e.x, e.y, enemyWidth, enemyHeight);
- // 检测飞机碰撞(游戏结束)
- if (
- isCollide(
- {x: plane.x, y: plane.y, width: planeWidth, height: planeHeight},
- {x: e.x, y: e.y, width: enemyWidth, height: enemyHeight}
- )
- ) {
- alert('游戏结束!你的得分是: ' + score);
- window.location.reload();
- return;
- }
- // 子弹与敌机碰撞检测
- for (let j = bullets.length - 1; j >= 0; j--) {
- const b = bullets[j];
- if (
- isCollide(
- {x: b.x, y: b.y, width: bulletWidth, height: bulletHeight},
- {x: e.x, y: e.y, width: enemyWidth, height: enemyHeight}
- )
- ) {
- bullets.splice(j, 1);
- enemies.splice(i, 1);
- score++;
- scoreDisplay.textContent = '得分: ' + score;
- break;
- }
- }
- }
- requestAnimationFrame(gameLoop);
- }
- // 开始游戏
- gameLoop();
- </script>
- </body>
- </html>
复制代码
---
###怎么玩这个小游戏?
- 打开浏览器,把上面的代码保存为 `plane-game.html`,然后用浏览器打开。
- 用 **左右箭头键** 控制飞机左右移动。
- 按 **空格键** 发射子弹。
- 消灭红色方块敌人得分。
- 飞机碰到敌人游戏结束,页面会弹出提示并重新开始。
---
这只是一个非常基础的版本哦,如果你想加更多酷炫的功能,随时告诉我,我可以帮你一步步改进!开心coding!????✈️ |
|