像素風球球大作戰 HTML 游戲

像素風球球大作戰 HTML 游戲

下面是一個簡單的像素風格球球大作戰 HTML 游戲代碼:

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>像素風球球大作戰</title><style>body {margin: 0;overflow: hidden;background-color: #222;font-family: 'Courier New', monospace;text-align: center;color: white;}canvas {display: block;margin: 0 auto;background-color: #333;image-rendering: pixelated;image-rendering: crisp-edges;}#gameInfo {position: absolute;top: 10px;left: 10px;color: white;font-size: 16px;}#startScreen {position: absolute;top: 0;left: 0;width: 100%;height: 100%;background-color: rgba(0, 0, 0, 0.7);display: flex;flex-direction: column;justify-content: center;align-items: center;}button {background-color: #4CAF50;border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 10px 2px;cursor: pointer;border-radius: 5px;}</style>
</head>
<body><div id="gameInfo">分數: <span id="score">0</span> | 大小: <span id="size">10</span></div><canvas id="gameCanvas"></canvas><div id="startScreen"><h1>像素風球球大作戰</h1><p>使用鼠標移動你的球球,吃掉小點點變大,避開比你大的球球!</p><button id="startButton">開始游戲</button></div><script>const canvas = document.getElementById('gameCanvas');const ctx = canvas.getContext('2d');const scoreElement = document.getElementById('score');const sizeElement = document.getElementById('size');const startScreen = document.getElementById('startScreen');const startButton = document.getElementById('startButton');// 設置畫布大小canvas.width = window.innerWidth;canvas.height = window.innerHeight;// 游戲變量let score = 0;let gameRunning = false;let player = {x: canvas.width / 2,y: canvas.height / 2,radius: 10,color: '#4CAF50'};let dots = [];let enemies = [];let mouseX = player.x;let mouseY = player.y;// 初始化游戲function initGame() {score = 0;player.radius = 10;dots = [];enemies = [];// 創建小點點for (let i = 0; i < 50; i++) {createDot();}// 創建敵人for (let i = 0; i < 5; i++) {createEnemy();}gameRunning = true;startScreen.style.display = 'none';updateGameInfo();gameLoop();}// 創建小點點function createDot() {dots.push({x: Math.random() * canvas.width,y: Math.random() * canvas.height,radius: 5,color: getRandomColor()});}// 創建敵人function createEnemy() {const radius = Math.random() * 20 + 15;enemies.push({x: Math.random() * canvas.width,y: Math.random() * canvas.height,radius: radius,color: getRandomColor(),speed: 50 / radius,dx: (Math.random() - 0.5) * 2,dy: (Math.random() - 0.5) * 2});}// 獲取隨機顏色function getRandomColor() {const colors = ['#FF5252', '#FFEB3B', '#4CAF50', '#2196F3', '#9C27B0', '#FF9800'];return colors[Math.floor(Math.random() * colors.length)];}// 更新游戲信息function updateGameInfo() {scoreElement.textContent = score;sizeElement.textContent = Math.floor(player.radius);}// 游戲主循環function gameLoop() {if (!gameRunning) return;// 清空畫布ctx.clearRect(0, 0, canvas.width, canvas.height);// 更新玩家位置const dx = mouseX - player.x;const dy = mouseY - player.y;const distance = Math.sqrt(dx * dx + dy * dy);const speed = 200 / player.radius;if (distance > 5) {player.x += dx / distance * speed;player.y += dy / distance * speed;}// 繪制玩家drawPixelCircle(player.x, player.y, player.radius, player.color);// 更新和繪制小點點for (let i = dots.length - 1; i >= 0; i--) {const dot = dots[i];drawPixelCircle(dot.x, dot.y, dot.radius, dot.color);// 檢測碰撞if (checkCollision(player, dot)) {player.radius += 0.5;score += 1;dots.splice(i, 1);createDot();updateGameInfo();}}// 更新和繪制敵人for (let i = enemies.length - 1; i >= 0; i--) {const enemy = enemies[i];// 敵人移動enemy.x += enemy.dx * enemy.speed;enemy.y += enemy.dy * enemy.speed;// 邊界檢測if (enemy.x < enemy.radius || enemy.x > canvas.width - enemy.radius) {enemy.dx *= -1;}if (enemy.y < enemy.radius || enemy.y > canvas.height - enemy.radius) {enemy.dy *= -1;}drawPixelCircle(enemy.x, enemy.y, enemy.radius, enemy.color);// 檢測碰撞if (checkCollision(player, enemy)) {if (player.radius > enemy.radius * 0.8) {// 玩家吃掉敵人player.radius += enemy.radius * 0.2;score += Math.floor(enemy.radius);enemies.splice(i, 1);createEnemy();updateGameInfo();} else {// 游戲結束gameOver();}}}requestAnimationFrame(gameLoop);}// 檢測碰撞function checkCollision(circle1, circle2) {const dx = circle1.x - circle2.x;const dy = circle1.y - circle2.y;const distance = Math.sqrt(dx * dx + dy * dy);return distance < circle1.radius + circle2.radius;}// 繪制像素風圓形function drawPixelCircle(x, y, radius, color) {ctx.fillStyle = color;// 為了像素風效果,我們將坐標和半徑取整const pixelX = Math.floor(x);const pixelY = Math.floor(y);const pixelRadius = Math.floor(radius);ctx.beginPath();ctx.arc(pixelX, pixelY, pixelRadius, 0, Math.PI * 2);ctx.fill();// 添加像素風格的邊緣ctx.strokeStyle = '#000';ctx.lineWidth = 1;ctx.stroke();}// 游戲結束function gameOver() {gameRunning = false;startScreen.style.display = 'flex';document.querySelector('#startScreen h1').textContent = '游戲結束!';document.querySelector('#startScreen p').textContent = `你的最終分數: ${score},大小: ${Math.floor(player.radius)}`;startButton.textContent = '再玩一次';}// 事件監聽canvas.addEventListener('mousemove', (e) => {mouseX = e.clientX;mouseY = e.clientY;});startButton.addEventListener('click', initGame);// 窗口大小改變時調整畫布window.addEventListener('resize', () => {canvas.width = window.innerWidth;canvas.height = window.innerHeight;});</script>
</body>
</html>

游戲說明

這個像素風球球大作戰是一個簡單的 HTML5 游戲,靈感來自流行的 .io 類游戲。以下是游戲的主要特點:

  1. 游戲目標:控制你的球球吃掉小點點和其他較小的球球來成長,同時避免被更大的球球吃掉。

  2. 控制方式:使用鼠標移動來控制你的球球。

  3. 游戲機制

    • 吃掉彩色小點點可以緩慢增長
    • 吃掉比你小的敵人可以快速成長
    • 碰到比你大的敵人會導致游戲結束
    • 你的球球越大,移動速度越慢
  4. 像素風效果:通過 image-rendering: pixelated 和圓形的粗糙邊緣渲染實現像素風格。

相關參考鏈接

  1. HTML5 Canvas 教程 - MDN Web Docs

    • Mozilla 開發者網絡提供的 Canvas 教程,是學習 HTML5 游戲開發的基礎資源。
  2. 像素藝術游戲設計指南

    • 關于如何設計像素風格游戲的指南,包括視覺風格和設計原則。
  3. .io 類游戲開發分析

    • GitHub 上的一個開源 .io 類游戲項目,可以幫助理解這類游戲的核心機制。
      在這里插入圖片描述

這個游戲可以進一步擴展,比如添加多人游戲功能、更多類型的食物和敵人、技能系統等。當前的實現提供了一個基礎框架,你可以根據需要繼續開發。

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

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

相關文章

文件導出時無法獲取響應頭Content-Disposition的文件名

1. 為什么Content-Disposition無法獲取&#xff1f; 要拿到 Content-Disposition 里的 filename&#xff0c;可以用正則或者簡單的字符串解析。 瀏覽器默認不讓前端訪問非標準響應頭&#xff0c;Content-Disposition 需要后端顯式暴露。 在瀏覽器開發者工具 → Network → Re…

Leetcode 128. 最長連續序列 哈希

原題鏈接&#xff1a; Leetcode 128. 最長連續序列 解法1: map&#xff0c;不符合要求 class Solution { public:int longestConsecutive(vector<int>& nums) {if (nums.size()0) return 0;map<int,int> mp;for(auto x: nums){mp[x];}int pre;int l0,r0,res0;…

禾賽激光雷達AT128P/海康相機(2):基于歐幾里德聚類的激光雷達障礙物檢測

目錄 一、參考連接 二、實驗效果?編輯 三、安裝相應的 ros 依賴包 四、代碼驅動 4.1 代碼下載 4.2 代碼文件放置(請按照這個命名放置代碼) 4.3 代碼編譯 4.4 報錯 一、參考連接

Vue Router的常用API有哪些?

文章目錄一、路由配置相關二、路由實例方法&#xff08;router 實例&#xff09;三、組件內路由 API&#xff08;useRouter / useRoute&#xff09;四、導航守衛&#xff08;路由攔截&#xff09;五、路由視圖與導航組件六、其他常用 API七、history模式和hash模式有什么區別&a…

從現場到云端的“通用語”:Kepware 在工業互聯中的角色、使用方法與本土廠商(以胡工科技為例)的差異與優勢

從現場到云端的“通用語”&#xff1a;Kepware 在工業互聯中的角色、使用方法與本土廠商&#xff08;以胡工科技為例&#xff09;的差異與優勢 文章目錄從現場到云端的“通用語”&#xff1a;Kepware 在工業互聯中的角色、使用方法與本土廠商&#xff08;以胡工科技為例&#x…

深入理解Prompt構建與工程技巧:API高效實踐指南

深入理解Prompt構建與工程技巧&#xff1a;API高效實踐指南 引言 Prompt&#xff08;提示&#xff09;工程是推動大模型能力極限的關鍵手段。合理的Prompt不僅能顯著提升模型輸出的相關性與準確性&#xff0c;在實際落地的API接口開發中同樣起到舉足輕重的作用。本文將系統介…

C++之多態(從0到1的突破)

世間百態&#xff0c;每個人都扮演著不同的角色&#xff0c;都進行著不同的行為。C更是如此&#xff0c;C中也會出現有著不同行為的多種形態的出現&#xff0c;那就讓我們一起進入C的多態世界吧&#xff01;&#xff01;&#xff01; 一. 多態的概念 多態&#xff0c;顧名思義&…

路由器NAT的類型測定

目前所使用的NAT基本都是NAPT&#xff0c;即多端口的NAT技術&#xff0c;因此本文主要是設計了兩種測定路由器NAPT類型的實驗。 實驗環境 設備 主機A&#xff1a;Windows主機B&#xff1a;Windows路由器 軟件 ncWiresharkSocketTools 在局域網內部完成所有測試&#xff0c;完全…

ROS 2系統Callback Group概念筆記

核心概念 Callback Group&#xff08;回調組&#xff09;是一個管理一個或多個回調函數執行規則的容器。它決定了這些回調函數是如何被節點&#xff08;Node&#xff09;的 executor 調度的&#xff0c;特別是當多個回調函數同時就緒時&#xff0c;它們之間是并行執行還是必須串…

Qt——主窗口 mainWindow

主窗口 mainWindow 前面學習的所有代碼&#xff0c;都是基于QWidget控件&#xff0c;其更多的是作為別的窗口的部分 現在來學習QMainWindow&#xff0c;即主窗口&#xff0c;其包含以下屬性 Window Title&#xff1a;標題欄Menu Bar&#xff1a;菜單欄Tool Bar Area&#xff1a…

無訓練神經網絡影響下的智能制造

摘要 未訓練神經網絡&#xff08;Untrained Neural Networks, UNNs&#xff09;作為近年來人工智能領域的新興范式&#xff0c;正在逐步改變智能制造的發展路徑。不同于傳統深度學習依賴大規模標注數據與高性能計算資源的模式&#xff0c;UNNs 借助網絡結構自身的歸納偏置與初…

微服務自動注冊到ShenYu網關配置詳解

一、配置逐行詳解 shenyu:register:registerType: http # 注冊中心類型:使用 HTTP 協議進行注冊serverLists: ${shenyu-register-serverLists} # ShenYu Admin 的地址列表props:username: ${shenyu-register-props-username} # 注冊認證用戶名password: ${shenyu-regi…

時序數據庫IoTDB的列式存儲引擎

在大數據時代&#xff0c;工業物聯網&#xff08;IIoT&#xff09;場景正以前所未有的速度生成著海量的時間序列數據。這些數據通常由成千上萬的傳感器&#xff08;如溫度、壓力、轉速傳感器&#xff09;持續不斷采集產生&#xff0c;它們具備鮮明的特點&#xff1a;數據時間屬…

JavaScript手錄18-ajax:異步請求與項目上線部署

前言&#xff1a;軟件開發流程 AJAX&#xff1a;前端與后端的數據交互 前后端協作基礎 Web應用的核心是“數據交互”&#xff0c;前端負責展示與交互&#xff0c;后端負責處理邏輯與數據存儲&#xff0c;二者通過網絡請求協作。 &#xff08;1&#xff09;項目開發流程與崗…

HTB 賽季7靶場 - Enviroment

最近所幸得點小閑&#xff0c;補個檔嘞&#xff01;~nmap掃描 nmap -F -A 10.10.11.67dirsearch掃描發現login接口 http://environment.htb/login構造如下payload&#xff0c;讓程序報錯&#xff0c;其原理在于缺失了rember后會導致報錯&#xff0c;從而告訴我們一個新的參數ke…

源碼編譯部署 LAMP 架構詳細步驟說明

源碼編譯部署 LAMP 架構詳細步驟說明 一、環境準備 1. 關閉防火墻和SELinux [roothrz ~]# systemctl stop firewalld [roothrz ~]# systemctl disable firewalld [roothrz ~]# setenforce 02. 配置YUM網絡源 [roothrz ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://m…

機器學習----PCA降維

一、PCA是什么&#xff1f;主成分分析&#xff08;Principal Component Analysis&#xff0c;PCA&#xff09;是機器學習中最常用的降維技術之一&#xff0c;它通過線性變換將高維數據投影到低維空間&#xff0c;同時保留數據的最重要特征。PCA由卡爾皮爾遜于1901年發明&#x…

ReactNative開發實戰——React Native開發環境配置指南

一、開發前準備 1. macOS平臺基礎工具安裝 brew install node18 brew install watchman brew install cocoapods2. 代理配置 npm config set proxy http://127.0.0.1:7890 npm config set https-proxy http://127.0.0.1:7890# 新增擴展建議&#xff08;可選配置&#xff09; ec…

差速轉向機器人研發:創新驅動的未來移動技術探索

在科技日新月異的今天&#xff0c;機器人技術作為智能制造與自動化領域的核心驅動力&#xff0c;正以前所未有的速度發展。其中&#xff0c;差速轉向機器人以其獨特的運動機制和廣泛的應用前景&#xff0c;成為了科研與工業界關注的焦點。本文旨在探討差速轉向機器人研發進展&a…

Wireshark捕獲電腦與路由器通信數據,繪制波形觀察

一、準備工作 電腦發出數據的波形圖繪制在我的另一篇博客有詳細介紹&#xff1a; 根據Wireshark捕獲數據包時間和長度繪制電腦發射信號波形-CSDN博客 路由器發送給電腦數據的波形圖繪制也在我的另一篇博客有詳細介紹&#xff1a; 根據Wireshark捕獲數據包時間和長度繪制路由…