飛機大戰小游戲

1.視覺設計:

????????采用柔和的藍紫色漸變背景,營造夢幻感

????????飛機、敵機和子彈使用柔和的糖果色調

????????添加了粒子爆炸效果,增強視覺反饋

????????星星收集物增加游戲趣味性

2.游戲機制:

????????玩家使用左右方向鍵控制飛機移動

????????空格鍵發射子彈

????????P鍵暫停游戲

????????擊落敵機獲得10分

????????收集星星增加生命值

????????碰撞敵機減少生命值

3.游戲元素:

????????玩家飛機:三角形設計,帶有尾焰效果

????????敵機:隨機大小和顏色的三角形

????????子彈:圓形子彈帶有尾跡

????????星星:五角星形狀,發光效果

????????粒子:爆炸和收集時的粒子效果

4.UI界面:

????????開始界面:游戲標題、操作說明和開始按鈕

????????游戲界面:頂部顯示分數和生命值

????????結束界面:顯示最終得分和重新開始按鈕

5.操作說明:

????????使用鍵盤左右方向鍵(← →)控制飛機移動

????????按空格鍵發射子彈

????????收集星星可以增加生命值

????????避免與敵機碰撞

????????按P鍵可以暫停/繼續游戲

6.截圖展示:

7.代碼重現:

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>夢幻飛機大戰</title><style>* {margin: 0;padding: 0;box-sizing: border-box;}body {font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;background: linear-gradient(135deg, #6e8efb, #a777e3);min-height: 100vh;display: flex;justify-content: center;align-items: center;overflow: hidden;color: #fff;}.game-container {position: relative;width: 800px;height: 600px;box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);border-radius: 15px;overflow: hidden;}#gameCanvas {background: linear-gradient(160deg, #1a2980, #26d0ce);width: 100%;height: 100%;display: block;}.game-ui {position: absolute;top: 0;left: 0;width: 100%;padding: 20px;display: flex;justify-content: space-between;}.ui-panel {background: rgba(255, 255, 255, 0.15);backdrop-filter: blur(10px);padding: 12px 25px;border-radius: 50px;box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);font-size: 1.2em;font-weight: 600;}.game-screen {position: absolute;top: 0;left: 0;width: 100%;height: 100%;display: flex;flex-direction: column;justify-content: center;align-items: center;background: rgba(26, 41, 128, 0.8);backdrop-filter: blur(5px);z-index: 10;transition: opacity 0.5s;}.start-screen {text-align: center;}.game-title {font-size: 4rem;margin-bottom: 20px;background: linear-gradient(45deg, #ff9a9e, #fad0c4);-webkit-background-clip: text;background-clip: text;color: transparent;text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);}.game-instructions {max-width: 600px;margin: 25px auto;background: rgba(255, 255, 255, 0.1);padding: 20px;border-radius: 15px;line-height: 1.6;}.btn {background: linear-gradient(45deg, #ff9a9e, #fad0c4);border: none;color: #fff;padding: 15px 40px;font-size: 1.2rem;border-radius: 50px;margin-top: 20px;cursor: pointer;transition: all 0.3s;box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);}.btn:hover {transform: translateY(-3px);box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);}.game-over {display: none;}.final-score {font-size: 3rem;margin: 20px 0;color: #ff9a9e;}.particle {position: absolute;border-radius: 50%;background: rgba(255, 255, 255, 0.5);pointer-events: none;}.controls-info {display: flex;gap: 30px;margin: 20px 0;}.control-item {text-align: center;background: rgba(255, 255, 255, 0.1);padding: 15px;border-radius: 15px;width: 120px;}.key {display: inline-block;background: rgba(255, 255, 255, 0.2);padding: 8px 15px;border-radius: 8px;margin: 5px 0;font-weight: bold;}</style>
</head>
<body><div class="game-container"><canvas id="gameCanvas" width="800" height="600"></canvas><div class="game-ui"><div class="ui-panel">得分: <span id="score">0</span></div><div class="ui-panel">生命: <span id="lives">3</span></div></div><div class="game-screen start-screen" id="startScreen"><h1 class="game-title">夢幻飛機大戰</h1><div class="game-instructions"><p>駕駛你的飛機,躲避敵機并消滅它們!</p><p>收集星星可以增加你的生命值,每擊落一架敵機得10分!</p><div class="controls-info"><div class="control-item"><div class="key">←</div><div class="key">→</div><div>左右移動</div></div><div class="control-item"><div class="key">空格</div><div>發射子彈</div></div><div class="control-item"><div class="key">P</div><div>暫停游戲</div></div></div></div><button class="btn" id="startBtn">開始游戲</button></div><div class="game-screen game-over" id="gameOverScreen"><h1 class="game-title">游戲結束</h1><div class="final-score">得分: <span id="finalScore">0</span></div><button class="btn" id="restartBtn">再玩一次</button></div></div><script>// 獲取Canvas和上下文const canvas = document.getElementById('gameCanvas');const ctx = canvas.getContext('2d');// 游戲狀態const gameState = {score: 0,lives: 3,gameOver: false,paused: false};// 玩家飛機const player = {x: canvas.width / 2 - 25,y: canvas.height - 80,width: 50,height: 40,speed: 6,color: '#FFD166'};// 子彈數組let bullets = [];// 敵機數組let enemies = [];// 星星數組let stars = [];// 粒子數組let particles = [];// 按鍵狀態const keys = {};// 初始化游戲function init() {gameState.score = 0;gameState.lives = 3;gameState.gameOver = false;bullets = [];enemies = [];stars = [];particles = [];updateUI();}// 更新UI顯示function updateUI() {document.getElementById('score').textContent = gameState.score;document.getElementById('lives').textContent = gameState.lives;}// 繪制玩家飛機function drawPlayer() {ctx.save();// 飛機主體ctx.fillStyle = player.color;ctx.beginPath();ctx.moveTo(player.x + player.width/2, player.y);ctx.lineTo(player.x + player.width, player.y + player.height);ctx.lineTo(player.x, player.y + player.height);ctx.closePath();ctx.fill();// 飛機細節ctx.fillStyle = '#EF476F';ctx.fillRect(player.x + player.width/2 - 5, player.y + player.height/2, 10, 15);// 飛機機翼ctx.fillStyle = '#06D6A0';ctx.fillRect(player.x - 10, player.y + player.height - 15, 20, 10);ctx.fillRect(player.x + player.width - 10, player.y + player.height - 15, 20, 10);// 飛機火焰ctx.fillStyle = '#FF9A9E';ctx.beginPath();ctx.moveTo(player.x + player.width/2 - 10, player.y + player.height);ctx.lineTo(player.x + player.width/2 + 10, player.y + player.height);ctx.lineTo(player.x + player.width/2, player.y + player.height + 15);ctx.closePath();ctx.fill();ctx.restore();}// 繪制子彈function drawBullets() {for (let i = 0; i < bullets.length; i++) {const bullet = bullets[i];ctx.fillStyle = '#FFD166';ctx.beginPath();ctx.arc(bullet.x, bullet.y, bullet.radius, 0, Math.PI * 2);ctx.fill();// 添加子彈尾跡ctx.fillStyle = 'rgba(255, 209, 102, 0.3)';ctx.beginPath();ctx.arc(bullet.x, bullet.y + 8, bullet.radius + 2, 0, Math.PI * 2);ctx.fill();}}// 繪制敵機function drawEnemies() {for (let i = 0; i < enemies.length; i++) {const enemy = enemies[i];// 敵機主體ctx.fillStyle = enemy.color;ctx.beginPath();ctx.moveTo(enemy.x, enemy.y);ctx.lineTo(enemy.x + enemy.width, enemy.y);ctx.lineTo(enemy.x + enemy.width/2, enemy.y + enemy.height);ctx.closePath();ctx.fill();// 敵機細節ctx.fillStyle = '#073B4C';ctx.fillRect(enemy.x + enemy.width/2 - 8, enemy.y + 5, 16, 10);}}// 繪制星星function drawStars() {for (let i = 0; i < stars.length; i++) {const star = stars[i];ctx.fillStyle = '#FFD166';ctx.beginPath();// 繪制五角星const spikes = 5;const outerRadius = star.radius;const innerRadius = star.radius / 2;let rotation = Math.PI / 2 * 3;let x = star.x;let y = star.y;let step = Math.PI / spikes;ctx.beginPath();ctx.moveTo(star.x, star.y - outerRadius);for (let i = 0; i < spikes; i++) {x = star.x + Math.cos(rotation) * outerRadius;y = star.y + Math.sin(rotation) * outerRadius;ctx.lineTo(x, y);rotation += step;x = star.x + Math.cos(rotation) * innerRadius;y = star.y + Math.sin(rotation) * innerRadius;ctx.lineTo(x, y);rotation += step;}ctx.lineTo(star.x, star.y - outerRadius);ctx.closePath();ctx.fill();// 添加星星發光效果ctx.fillStyle = 'rgba(255, 209, 102, 0.3)';ctx.beginPath();ctx.arc(star.x, star.y, star.radius + 3, 0, Math.PI * 2);ctx.fill();}}// 繪制粒子function drawParticles() {for (let i = 0; i < particles.length; i++) {const p = particles[i];ctx.globalAlpha = p.alpha;ctx.fillStyle = p.color;ctx.beginPath();ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);ctx.fill();ctx.globalAlpha = 1;}}// 創建粒子效果function createParticles(x, y, color, count) {for (let i = 0; i < count; i++) {particles.push({x: x,y: y,size: Math.random() * 3 + 1,speedX: Math.random() * 6 - 3,speedY: Math.random() * 6 - 3,color: color,alpha: Math.random() * 0.5 + 0.5,life: Math.random() * 30 + 20});}}// 更新粒子function updateParticles() {for (let i = 0; i < particles.length; i++) {const p = particles[i];p.x += p.speedX;p.y += p.speedY;p.life--;p.alpha = p.life / 50;if (p.life <= 0) {particles.splice(i, 1);i--;}}}// 生成敵機function spawnEnemy() {const width = Math.random() * 40 + 30;const enemy = {x: Math.random() * (canvas.width - width),y: -40,width: width,height: 30,speed: Math.random() * 2 + 1,color: `hsl(${Math.random() * 60 + 180}, 70%, 60%)`};enemies.push(enemy);}// 生成星星function spawnStar() {if (Math.random() < 0.01) {const star = {x: Math.random() * canvas.width,y: -20,radius: Math.random() * 8 + 5,speed: Math.random() * 2 + 1};stars.push(star);}}// 發射子彈function shoot() {bullets.push({x: player.x + player.width / 2,y: player.y,radius: 4,speed: 8});// 添加射擊粒子效果createParticles(player.x + player.width/2, player.y, '#FF9A9E', 10);}// 更新子彈位置function updateBullets() {for (let i = 0; i < bullets.length; i++) {bullets[i].y -= bullets[i].speed;// 移除超出屏幕的子彈if (bullets[i].y < 0) {bullets.splice(i, 1);i--;}}}// 更新敵機位置function updateEnemies() {for (let i = 0; i < enemies.length; i++) {enemies[i].y += enemies[i].speed;// 移除超出屏幕的敵機if (enemies[i].y > canvas.height) {enemies.splice(i, 1);i--;gameState.lives--;updateUI();if (gameState.lives <= 0) {endGame();}}}}// 更新星星位置function updateStars() {for (let i = 0; i < stars.length; i++) {stars[i].y += stars[i].speed;// 移除超出屏幕的星星if (stars[i].y > canvas.height) {stars.splice(i, 1);i--;}}}// 碰撞檢測function checkCollisions() {// 子彈與敵機碰撞for (let i = 0; i < bullets.length; i++) {for (let j = 0; j < enemies.length; j++) {const bullet = bullets[i];const enemy = enemies[j];if (bullet.x > enemy.x &&bullet.x < enemy.x + enemy.width &&bullet.y > enemy.y &&bullet.y < enemy.y + enemy.height) {// 添加爆炸粒子效果createParticles(enemy.x + enemy.width/2, enemy.y + enemy.height/2,enemy.color,30);bullets.splice(i, 1);enemies.splice(j, 1);i--;j--;gameState.score += 10;updateUI();break;}}}// 玩家與敵機碰撞for (let i = 0; i < enemies.length; i++) {const enemy = enemies[i];if (player.x < enemy.x + enemy.width &&player.x + player.width > enemy.x &&player.y < enemy.y + enemy.height &&player.y + player.height > enemy.y) {// 添加爆炸粒子效果createParticles(enemy.x + enemy.width/2, enemy.y + enemy.height/2,enemy.color,50);enemies.splice(i, 1);i--;gameState.lives--;updateUI();if (gameState.lives <= 0) {endGame();}}}// 玩家與星星碰撞for (let i = 0; i < stars.length; i++) {const star = stars[i];const dx = player.x + player.width/2 - star.x;const dy = player.y + player.height/2 - star.y;const distance = Math.sqrt(dx * dx + dy * dy);if (distance < player.width/2 + star.radius) {// 添加收集粒子效果createParticles(star.x, star.y,'#FFD166',20);stars.splice(i, 1);i--;gameState.lives++;updateUI();}}}// 結束游戲function endGame() {gameState.gameOver = true;document.getElementById('finalScore').textContent = gameState.score;document.getElementById('gameOverScreen').style.display = 'flex';}// 繪制背景function drawBackground() {// 繪制漸變背景const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);gradient.addColorStop(0, '#1a2980');gradient.addColorStop(1, '#26d0ce');ctx.fillStyle = gradient;ctx.fillRect(0, 0, canvas.width, canvas.height);// 繪制星空效果ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';for (let i = 0; i < 50; i++) {const x = Math.random() * canvas.width;const y = Math.random() * canvas.height;const radius = Math.random() * 1.5;ctx.beginPath();ctx.arc(x, y, radius, 0, Math.PI * 2);ctx.fill();}}// 游戲循環function gameLoop() {if (gameState.gameOver || gameState.paused) return;// 清除畫布drawBackground();// 更新游戲對象updateBullets();updateEnemies();updateStars();updateParticles();// 生成新敵機和星星if (Math.random() < 0.02) spawnEnemy();spawnStar();// 檢測碰撞checkCollisions();// 繪制游戲對象drawStars();drawEnemies();drawBullets();drawPlayer();drawParticles();// 玩家移動if (keys['ArrowLeft'] && player.x > 0) {player.x -= player.speed;}if (keys['ArrowRight'] && player.x < canvas.width - player.width) {player.x += player.speed;}requestAnimationFrame(gameLoop);}// 事件監聽document.getElementById('startBtn').addEventListener('click', () => {document.getElementById('startScreen').style.display = 'none';init();gameLoop();});document.getElementById('restartBtn').addEventListener('click', () => {document.getElementById('gameOverScreen').style.display = 'none';init();gameLoop();});window.addEventListener('keydown', (e) => {keys[e.key] = true;// 空格鍵射擊if (e.key === ' ' && !gameState.gameOver && !gameState.paused) {shoot();}// P鍵暫停if (e.key === 'p' || e.key === 'P') {gameState.paused = !gameState.paused;if (!gameState.paused && !gameState.gameOver) {gameLoop();}}});window.addEventListener('keyup', (e) => {keys[e.key] = false;});// 初始化UIupdateUI();</script>
</body>
</html>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/91591.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/91591.shtml
英文地址,請注明出處:http://en.pswp.cn/web/91591.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux 啟動服務腳本

1. 創建命令文件# 創建可執行文件 touch 文件名稱 例&#xff1a; touch stopServer.sh2. 命令文件授權# 授權文件可執行權限 chmod 777 文件名稱 例&#xff1a; chmod 777 stopServer.sh3. 停止服務命令編寫#!/bin/bash# 獲取進程號 pidps -ef | grep -- /mnt/apache-tomcat-…

【華為機試】34. 在排序數組中查找元素的第一個和最后一個位置

文章目錄34. 在排序數組中查找元素的第一個和最后一個位置描述示例 1&#xff1a;示例 2&#xff1a;示例 3&#xff1a;提示&#xff1a;解題思路算法分析問題本質分析雙重二分查找詳解左邊界查找過程右邊界查找過程算法流程圖邊界情況分析各種解法對比二分查找變種詳解時間復…

【網絡編程】WebSocket 實現簡易Web多人聊天室

一、實現思路 Web端就是使用html JavaScript來實現頁面&#xff0c;通過WebSocket長連接和服務器保持通訊&#xff0c;協議的payload使用JSON格式封裝 服務端使用C配合第三方庫WebSocket和nlonlohmann庫來實現 二、Web端 2.1 界面顯示 首先&#xff0c;使用html來設計一個…

AI 驅動、設施擴展、驗證器強化、上線 EVM 測試網,Injective 近期動態全更新!

作為一個專注于金融應用、且具有高度可互操作性的高性能 Layer-1 區塊鏈&#xff0c;Injective 自誕生以來便為開發者提供有即插即用的技術模塊&#xff0c;以便開發者能夠更好地搭建新一代 Web3 金融類應用。談及項目發展的愿景和基本定位&#xff0c;創始團隊曾提到希望 Inje…

Qt-----初識

1. 什么是Qt定義&#xff1a;Qt是一個跨平臺的應用程序和用戶界面框架&#xff0c;主要用于開發具有圖形用戶界面的應用程序&#xff0c;同時也支持非GUI程序的開發。 編程語言&#xff1a;主要使用C&#xff0c;但也提供了對Python&#xff08;PyQt&#xff09;、JavaScript&a…

理解微信體系中的 AppID、OpenID 和 UnionID

前言: 在開發微信相關的服務(如小程序,公眾號,微信開放平臺等)時,很多人都會接觸到幾個看起來相似但實際用途不同的額ID: AppiD, OpenID,UnionID. 搞清楚這三者的區別,是微信生態開發中的基本功,本文將從開發者視角觸發,深入淺出地解釋它們的關系,區別以及實際應用場景一.什么是…

FFmpeg,如何插入SEI自定義數據

FFmpeg&#xff0c;如何插入SEI自定義數據 一、什么是SEI&#xff1f; SEI&#xff08;Supplemental Enhancement Information&#xff0c;補充增強信息&#xff09;是H.264/H.265視頻編碼標準中的一種元數據載體&#xff0c;它允許在視頻流中嵌入額外的信息&#xff0c;如時…

為什么分類任務偏愛交叉熵?MSE 為何折戟?

在機器學習的世界里&#xff0c;損失函數是模型的“指南針”——它定義了模型“好壞”的標準&#xff0c;直接決定了參數優化的方向。對于分類任務&#xff08;比如判斷一張圖片是貓還是狗&#xff09;&#xff0c;我們通常會選擇交叉熵作為損失函數&#xff1b;而在回歸任務&a…

[echarts]橫向柱狀圖

前言 接到一個需求&#xff0c;需要展示一個橫向的柱狀圖&#xff0c;按數量從大到小排序&#xff0c;并定時刷新 使用react配合echarts進行實現。 react引入echarts import React, { useEffect, useRef } from react; import * as echarts from echarts; import DeviceApi fro…

【開源項目】輕量加速利器 HubProxy 自建 Docker、GitHub 下載加速服務

??引言?? 如果你經常被 Docker 鏡像拉取、GitHub 文件下載的龜速折磨&#xff0c;又不想依賴第三方加速服務&#xff08;擔心穩定性或隱私&#xff09;&#xff0c;今天分享的 ??HubProxy?? 可能正是你需要的。這個開源工具用一行命令就能部署&#xff0c;以極低資源消…

java web jsp jstl練習

JSP 的學習。 核心功能模塊 1. 源代碼層 &#xff08; src &#xff09; HelloWorld &#xff1a;主程序入口領域模型 &#xff1a; domain 包含User.java和ceshi.java控制器 &#xff1a; servlet 包含登錄驗證和驗證碼相關ServletWeb表現層 &#xff08; web &#xff09; JS…

VSCode 完全指南:釋放你的編碼潛能

零、簡介 在當今的軟件開發領域&#xff0c;代碼編輯器的選擇至關重要&#xff0c;它就像是工匠手中的工具&#xff0c;直接影響著工作效率和成果質量。Visual Studio Code&#xff08;簡稱 VSCode&#xff09;自問世以來&#xff0c;迅速在全球開發者社區中嶄露頭角&#xff…

《n8n基礎教學》第一節:如何使用編輯器UI界面

在本課中&#xff0c;你將學習如何操作編輯器界面。我們將瀏覽畫布&#xff0c;向您展示每個圖標的含義&#xff0c;以及在 n8n 中構建工作流程時在哪里可以找到您需要的東西。本課程基于 n8n 最新版本 。在其他版本中&#xff0c;某些用戶界面可能有所不同&#xff0c;但這不會…

gcc g++ makefile CMakeLists.txt cmake make 的關系

gcc&#xff1a;C語言編譯器g&#xff1a;C編譯器makefile&#xff1a;定義編譯規則、依賴關系和構建目標。可以手動編寫&#xff0c;也可以由CMakeLists.txt生成cmake&#xff1a;讀取CMakeLists.txt文件&#xff0c;生成Makefilemake&#xff1a;構建工具&#xff0c;執行Mak…

SFT 訓練器

SFT 訓練器 “訓練時間到!” 我們現在終于可以創建一個監督微調訓練器的實例了: trainer = SFTTrainer( model=model, processing_class=tokenizer, args=sft_config, train_dataset=dataset, )SFTTrainer 已經對數據集進行了預處理,因此我們可以深入查看,了解每個小批次…

Android Material Components 全面解析:打造現代化 Material Design 應用

引言 在當今移動應用開發領域&#xff0c;用戶體驗(UX)已成為決定應用成功與否的關鍵因素之一。Google推出的Material Design設計語言為開發者提供了一套完整的視覺、交互和動效規范&#xff0c;而Material Components for Android(MDC-Android)則是將這些設計理念轉化為可重用…

Windows使用Powershell自動安裝SqlServer2025服務器與SSMS管理工具

安裝結果: 安裝前準備: 1.下載mssql server 2025安裝器 2.下載iso鏡像 3.下載好SSMS安裝程序,并放到iso同目錄下 4.執行腳本開始自動安裝

09 RK3568 Debian11 ES8388 模擬音頻輸出

1、設備樹配置 確認自己的i2c,使用sdk帶的驅動es8323 /SDK/kernel/sound/soc/codecs/es8323.c es8388_sound: es8388-sound {status = "okay";compatible = "rockchip,multicodecs-card"; rockchip,card-name = "rockchip,es8388-codec"; …

力扣-199.二叉樹的右視圖

題目鏈接 199.二叉樹的右視圖 class Solution {public List<Integer> rightSideView(TreeNode root) {List<Integer> res new ArrayList<>();Queue<TreeNode> queue new LinkedList<>();if (root null)return res;queue.offer(root);while …

Android Bitmap 完全指南:從基礎到高級優化

在 Android 開發中&#xff0c;圖像處理是一個核心且復雜的領域&#xff0c;而 Bitmap 作為 Android 中表示圖像的基本單位&#xff0c;貫穿了從簡單圖片顯示到復雜圖像編輯的各個場景。然而&#xff0c;Bitmap 處理不當往往會導致應用性能下降、內存溢出&#xff08;OOM&#…