|
|
你好!我来帮你做一个简单的“飞机大战”小游戏网页代码哦~
这个游戏里,你可以用键盘的方向键控制飞机左右移动,按空格键发射子弹,打掉上方飞来的敌机。我们会用HTML、CSS和JavaScript来做它。
下面是完整代码,你可以直接复制到电脑上的记事本,保存成 `feiji.html`,然后用浏览器打开玩玩看!
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8" />
- <title>简易飞机大战游戏</title>
- <style>
- body {
- margin: 0;
- background: black;
- overflow: hidden;
- }
- #gameCanvas {
- background: #000;
- display: block;
- margin: 0 auto;
- border: 2px solid white;
- }
- </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 - 20,
- y: canvas.height - 60,
- width: 40,
- height: 40,
- speed: 5
- };
- // 按键状态
- const keys = {};
- // 子弹数组
- const bullets = [];
- // 敌机数组
- const enemies = [];
- // 生成敌机函数
- function createEnemy() {
- const enemy = {
- x: Math.random() * (canvas.width - 40),
- y: -40,
- width: 40,
- height: 40,
- speed: 2 + Math.random() * 2
- };
- enemies.push(enemy);
- }
- // 画飞机
- function drawPlane() {
- ctx.fillStyle = 'deepskyblue';
- // 飞机像个三角形,我们画简单点
- 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();
- }
- // 画子弹
- function drawBullets() {
- ctx.fillStyle = 'yellow';
- bullets.forEach(bullet => {
- ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
- });
- }
- // 画敌机
- function drawEnemies() {
- ctx.fillStyle = 'red';
- enemies.forEach(enemy => {
- ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
- });
- }
- // 更新游戏状态
- function update() {
- // 根据按键移动飞机
- if (keys['ArrowLeft'] && plane.x > 0) {
- plane.x -= plane.speed;
- }
- if (keys['ArrowRight'] && plane.x < canvas.width - plane.width) {
- plane.x += plane.speed;
- }
- // 更新子弹位置
- bullets.forEach((bullet, i) => {
- bullet.y -= bullet.speed;
- if (bullet.y < 0) {
- bullets.splice(i, 1);
- }
- });
- // 更新敌机位置
- enemies.forEach((enemy, i) => {
- enemy.y += enemy.speed;
- if (enemy.y > canvas.height) {
- enemies.splice(i, 1);
- }
- });
- // 子弹和敌机碰撞检测
- bullets.forEach((bullet, bIndex) => {
- enemies.forEach((enemy, eIndex) => {
- 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(bIndex, 1);
- enemies.splice(eIndex, 1);
- }
- });
- });
- }
- // 画面刷新
- function draw() {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- drawPlane();
- drawBullets();
- drawEnemies();
- }
- // 游戏主循环
- function gameLoop() {
- update();
- draw();
- requestAnimationFrame(gameLoop);
- }
- // 控制按键按下
- window.addEventListener('keydown', (e) => {
- keys[e.key] = true;
- // 空格键发射子弹
- if (e.key === ' ') {
- bullets.push({
- x: plane.x + plane.width/2 - 2,
- y: plane.y,
- width: 4,
- height: 10,
- speed: 7
- });
- }
- });
- // 放开按键
- window.addEventListener('keyup', (e) => {
- keys[e.key] = false;
- });
- // 每隔一秒生成一个敌机
- setInterval(createEnemy, 1000);
- // 开始游戏
- gameLoop();
- </script>
- </body>
- </html>
复制代码
**怎么玩:**
- 用键盘左箭头和右箭头控制飞机左右移动。
- 按空格键可以发射子弹打敌机。
- 敌机会从上往下飞,打中敌机敌机会消失。
希望你喜欢这个小飞机大战!如果你想加得分或者碰撞后游戏结束,我也可以帮你改进哦~有什么问题随时问我!????✈️ |
|