|
|
好的,我帮你把护盾技能完整地结合到你的代码里,方便你直接使用。为了更清楚,我会把完整的JavaScript部分给你,你只要替换原来的JS代码就可以啦!
- const canvas = document.getElementById("gameCanvas");
- const ctx = canvas.getContext("2d");
- const planeWidth = 40;
- const planeHeight = 40;
- let planeX = (canvas.width - planeWidth) / 2;
- const planeY = canvas.height - planeHeight - 10;
- let hp = 3;
- let score = 0;
- // 新增护盾变量
- let shieldOn = false;
- let shieldDuration = 5000; // 护盾持续时间5秒
- let shieldTimer = null;
- // 敌机数组
- let enemies = [];
- // 子弹数组
- let bullets = [];
- // 控制左右移动
- let moveLeft = false;
- let moveRight = false;
- // 监听键盘按下
- window.addEventListener("keydown", function(e) {
- if (e.key === "a" || e.key === "A") moveLeft = true;
- if (e.key === "d" || e.key === "D") moveRight = true;
- if (e.key === "s" || e.key === "S") activateShield(); // 护盾技能
- });
- // 监听松开键盘
- window.addEventListener("keyup", function(e) {
- if (e.key === "a" || e.key === "A") moveLeft = false;
- if (e.key === "d" || e.key === "D") moveRight = false;
- if (e.key === " ") shoot();
- });
- // 画飞机
- function drawPlane() {
- ctx.fillStyle = "lightblue";
- ctx.beginPath();
- // 画三角形代表飞机
- ctx.moveTo(planeX + planeWidth / 2, planeY);
- ctx.lineTo(planeX, planeY + planeHeight);
- ctx.lineTo(planeX + planeWidth, planeY + planeHeight);
- ctx.closePath();
- ctx.fill();
- // 护盾开启时画光环
- if (shieldOn) {
- ctx.strokeStyle = "cyan";
- ctx.lineWidth = 3;
- ctx.beginPath();
- ctx.arc(planeX + planeWidth / 2, planeY + planeHeight / 2, planeWidth, 0, Math.PI * 2);
- ctx.stroke();
- }
- }
- // 生成敌机
- function createEnemy() {
- let x = Math.random() * (canvas.width - 30);
- enemies.push({x: x, y: -30, width: 30, height: 30});
- }
- // 画敌机
- function drawEnemies() {
- ctx.fillStyle = "red";
- enemies.forEach(enemy => {
- ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
- });
- }
- // 移动敌机
- function moveEnemies() {
- enemies.forEach(enemy => {
- enemy.y += 2; // 敌机向下移动速度
- });
- enemies = enemies.filter(enemy => enemy.y < canvas.height);
- }
- // 发射子弹
- function shoot() {
- bullets.push({x: planeX + planeWidth / 2 - 2.5, y: planeY, width: 5, height: 10});
- }
- // 画子弹
- function drawBullets() {
- ctx.fillStyle = "yellow";
- bullets.forEach(bullet => {
- ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
- });
- }
- // 移动子弹
- function moveBullets() {
- bullets.forEach(bullet => {
- bullet.y -= 5; // 子弹向上飞
- });
- bullets = bullets.filter(bullet => bullet.y + bullet.height > 0);
- }
- // 碰撞检测函数,检测两个矩形是否重叠
- function isColliding(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 checkBulletEnemyCollision() {
- for (let i = bullets.length - 1; i >= 0; i--) {
- let b = bullets[i];
- for (let j = enemies.length - 1; j >= 0; j--) {
- let e = enemies[j];
- if (isColliding(b, e)) {
- bullets.splice(i, 1);
- enemies.splice(j, 1);
- score++;
- updateScore();
- break;
- }
- }
- }
- }
- // 检查敌机和飞机碰撞
- function checkEnemyPlaneCollision() {
- let planeRect = {x: planeX, y: planeY, width: planeWidth, height: planeHeight};
- for (let i = enemies.length - 1; i >= 0; i--) {
- let e = enemies[i];
- if (isColliding(e, planeRect)) {
- enemies.splice(i,1);
- if (!shieldOn) { // 护盾没开才扣血
- hp--;
- updateHP();
- if (hp <= 0) {
- alert("游戏结束!你的得分是:" + score);
- document.location.reload();
- }
- }
- }
- }
- }
- // 激活护盾
- function activateShield() {
- if (shieldOn) return; // 已经激活了就不再激活
- shieldOn = true;
- // 5秒后关闭护盾
- shieldTimer = setTimeout(() => {
- shieldOn = false;
- }, shieldDuration);
- }
- function updateHP() {
- document.getElementById("hp").textContent = hp;
- }
- function updateScore() {
- document.getElementById("score").textContent = score;
- }
- // 游戏主循环
- function gameLoop() {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- if (moveLeft && planeX > 0) planeX -= 5;
- if (moveRight && planeX < canvas.width - planeWidth) planeX += 5;
- drawPlane();
- if (Math.random() < 0.03) createEnemy();
- moveEnemies();
- drawEnemies();
- moveBullets();
- drawBullets();
- checkBulletEnemyCollision();
- checkEnemyPlaneCollision();
- requestAnimationFrame(gameLoop);
- }
- // 开始游戏
- updateHP();
- updateScore();
- gameLoop();
复制代码
---
### 你页面中需要有这段HTML显示血量和分数:
- <div id="scoreboard">
- 血量:<span id="hp">3</span> 分数:<span id="score">0</span>
- </div>
- <canvas id="gameCanvas" width="400" height="600"></canvas>
复制代码
---
现在你按:
- A / D 键控制飞机左右移动
- 空格键发射子弹
- S 键启动5秒护盾,护盾期间飞机不会被敌机撞击损伤,并且会显示蓝色光圈提醒你
你可以试试看,有什么问题告诉我,我帮你继续优化!????????✈️ |
|