|
|
好的,我们来加一些代码,让我们的游戏更加完美!
我们需要做以下几点改动:
1. 检测玩家的飞机是否与敌机相撞。
2. 在碰撞时结束游戏,并显示“游戏结束”的界面。
### 修改后的JavaScript代码
我们在原来的基础上进行修改和补充:
- document.addEventListener("DOMContentLoaded", () => {
- const player = document.getElementById("player");
- const gameContainer = document.getElementById("game-container");
- const scoreDisplay = document.getElementById("score");
- const startButton = document.getElementById("start-button");
- const gameOverDisplay = document.createElement('div');
- let score = 0;
- let gameInterval;
- let bulletInterval;
- // 创建游戏结束的界面
- gameOverDisplay.id = 'game-over';
- gameOverDisplay.innerText = "游戏结束";
- gameOverDisplay.style.display = "none";
- gameContainer.appendChild(gameOverDisplay);
- startButton.addEventListener("click", startGame);
- function startGame() {
- startButton.style.display = "none";
- gameOverDisplay.style.display = "none";
- score = 0;
- scoreDisplay.innerText = "得分: " + score;
- gameLoop();
- document.addEventListener("keydown", handleKeys);
- }
- function handleKeys(e) {
- const key = e.code;
- if (key === "ArrowLeft") {
- movePlayer(-10);
- } else if (key === "ArrowRight") {
- movePlayer(10);
- } else if (key === "Space") {
- shootBullet();
- }
- }
- function movePlayer(distance) {
- let currentLeft = parseInt(window.getComputedStyle(player).left);
- let newLeft = currentLeft + distance;
- if (newLeft >= 0 && newLeft <= gameContainer.offsetWidth - player.offsetWidth) {
- player.style.left = newLeft + "px";
- }
- }
- function shootBullet() {
- const bullet = document.createElement("div");
- bullet.className = "bullet";
- bullet.style.left = parseInt(window.getComputedStyle(player).left) + player.offsetWidth / 2 - 2.5 + "px";
- gameContainer.appendChild(bullet);
- moveBullet(bullet);
- }
- function moveBullet(bullet) {
- const bulletTimer = setInterval(() => {
- const bulletTop = parseInt(window.getComputedStyle(bullet).top);
- if (bulletTop <= 0) {
- clearInterval(bulletTimer);
- bullet.remove();
- } else {
- bullet.style.top = (bulletTop - 10) + "px";
- }
- // 检查子弹是否击中敌机
- document.querySelectorAll(".enemy").forEach(enemy => {
- if (isColliding(bullet, enemy)) {
- clearInterval(bulletTimer);
- bullet.remove();
- enemy.remove();
- score++;
- scoreDisplay.innerText = "得分: " + score;
- }
- });
- }, 30);
- }
- function isColliding(a, b) {
- const aRect = a.getBoundingClientRect();
- const bRect = b.getBoundingClientRect();
- return !(
- aRect.top > bRect.bottom ||
- aRect.right < bRect.left ||
- aRect.bottom < bRect.top ||
- aRect.left > bRect.right
- );
- }
- function gameLoop() {
- gameInterval = setInterval(() => {
- createEnemy();
- }, 2000);
- }
- function createEnemy() {
- const enemy = document.createElement("div");
- enemy.className = "enemy";
- enemy.style.left = Math.random() * (gameContainer.offsetWidth - 50) + "px";
- enemy.style.top = "0px";
- gameContainer.appendChild(enemy);
- moveEnemy(enemy);
- }
- function moveEnemy(enemy) {
- const enemyTimer = setInterval(() => {
- const enemyTop = parseInt(window.getComputedStyle(enemy).top);
- if (enemyTop >= gameContainer.offsetHeight) {
- clearInterval(enemyTimer);
- enemy.remove();
- } else {
- enemy.style.top = (enemyTop + 5) + "px";
- }
- // 检查玩家是否与敌机相撞
- if (isColliding(player, enemy)) {
- clearInterval(enemyTimer);
- endGame();
- }
- }, 30);
- }
- function endGame() {
- clearInterval(gameInterval);
- document.removeEventListener("keydown", handleKeys);
- document.querySelectorAll(".bullet").forEach(bullet => bullet.remove());
- document.querySelectorAll(".enemy").forEach(enemy => enemy.remove());
- gameOverDisplay.style.display = "block";
- startButton.style.display = "block";
- }
- const style = document.createElement('style');
- style.innerHTML = `
- .bullet {
- position: absolute;
- width: 5px;
- height: 20px;
- background-color: red;
- top: calc(100% - 60px);
- }
- .enemy {
- position: absolute;
- width: 50px;
- height: 50px;
- background-color: green;
- }
- #game-over {
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- font-size: 48px;
- color: red;
- }
- `;
- document.head.appendChild(style);
- });
复制代码
### 修改后的CSS文件
我们还需要稍微调整一下CSS文件,增加`#game-over`的样式:
- /* styles.css */
- body {
- margin: 0;
- overflow: hidden;
- background-color: #000;
- color: white;
- font-family: Arial, sans-serif;
- }
- #game-container {
- position: relative;
- width: 100vw;
- height: 100vh;
- overflow: hidden;
- }
- #player {
- position: absolute;
- width: 50px;
- height: 50px;
- background-color: blue;
- bottom: 10px;
- left: calc(50% - 25px);
- }
- #score {
- position: absolute;
- top: 10px;
- left: 10px;
- }
- #start-button {
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- padding: 10px 20px;
- font-size: 18px;
- }
- #game-over {
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- font-size: 48px;
- color: red;
- }
复制代码
这样,我们就完成了一个更加完善的飞机大战游戏。当玩家的飞机与敌机相撞时,会结束游戏并显示“游戏结束”界面。
希望你喜欢这个游戏!如果有任何问题,随时问我哦!????✨ |
|