|
|
好的,小朋友!我们来一起做一个有趣的飞机大战游戏。这个游戏会用到HTML、CSS和JavaScript三种语言。别担心,我会一步一步地教你,保证你能理解每一部分内容哦!
### 1. HTML文件
首先,我们先写个简单的HTML文件,这是游戏的结构:
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>林奕锦的飞机大战</title>
- <link rel="stylesheet" href="styles.css">
- </head>
- <body>
- <div id="game-container">
- <div id="player"></div>
- <div id="score">得分: 0</div>
- <button id="start-button">开始游戏</button>
- </div>
- <script src="script.js"></script>
- </body>
- </html>
复制代码
这个文件中,我们定义了游戏的容器(`game-container`),玩家的飞机(`player`),得分显示(`score`)和开始按钮(`start-button`)。
### 2. CSS文件
接下来,我们来设计一下游戏的样子:
- /* 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;
- }
复制代码
这段CSS代码让背景变黑,飞机是蓝色的,并且设置了一个得分显示的位置。
### 3. JavaScript文件
最后,我们来编写控制游戏逻辑的JavaScript代码:
- // script.js
- 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");
- let score = 0;
- let gameInterval;
- let bulletInterval;
- startButton.addEventListener("click", startGame);
- function startGame() {
- startButton.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 = player.style.left;
- 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";
- checkCollision(bullet, bulletTimer);
- }
- }, 30);
- }
- function checkCollision(bullet, bulletTimer) {
- const enemies = document.querySelectorAll(".enemy");
- enemies.forEach(enemy => {
- if (isColliding(bullet, enemy)) {
- clearInterval(bulletTimer);
- bullet.remove();
- enemy.remove();
- score++;
- scoreDisplay.innerText = "得分: " + score;
- }
- });
- }
- function isColliding(bullet, enemy) {
- const bulletRect = bullet.getBoundingClientRect();
- const enemyRect = enemy.getBoundingClientRect();
- return !(
- bulletRect.top > enemyRect.bottom ||
- bulletRect.right < enemyRect.left ||
- bulletRect.bottom < enemyRect.top ||
- bulletRect.left > enemyRect.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";
- }
- }, 30);
- }
- 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;
- }
- `;
- document.head.appendChild(style);
- });
复制代码
这个JavaScript代码实现了以下功能:
- 按下“开始游戏”按钮时,游戏开始。
- 玩家可以用左右箭头键移动飞机,用空格键发射子弹。
- 子弹会向上飞,并摧毁敌机。
- 敌机会从上方随机位置出现并向下移动。
- 玩家得分会实时更新。
这样我们就完成了一个简单的飞机大战游戏啦!你可以把这些代码分别保存到三个文件(`index.html`、`styles.css`、`script.js`)中,然后在浏览器中打开`index.html`文件,就可以开始玩了。
有问题随时问我哦!祝你玩得开心!????✨ |
|