用html5仿造nes游戲敲玻璃寫一個敲玻璃游戲

<!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>
body {
margin: 0;
padding: 0;
background: #2c3e50;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
font-family: Arial, sans-serif;
}

#gameContainer {
background: #34495e;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0,0,0,0.5);
}

#gameBoard {
display: grid;
grid-template-columns: repeat(8, 60px);
grid-template-rows: repeat(6, 60px);
gap: 5px;
background: #1a252f;
padding: 10px;
border-radius: 5px;
}

.glass {
background: linear-gradient(135deg, #87CEEB 0%, #4682B4 100%);
border: 2px solid #5F9EA0;
border-radius: 5px;
cursor: pointer;
position: relative;
transition: all 0.3s ease;
box-shadow: inset 0 0 10px rgba(255,255,255,0.3);
}

.glass:hover {
transform: scale(1.05);
box-shadow: 0 0 15px rgba(135, 206, 235, 0.8);
}

.glass.cracked {
background: linear-gradient(135deg, #B0C4DE 0%, #708090 100%);
animation: crack 0.5s ease-out;
}

.glass.broken {
background: transparent;
border: 2px dashed #7f8c8d;
pointer-events: none;
}

@keyframes crack {
0% { transform: scale(1); }
50% { transform: scale(1.1) rotate(5deg); }
100% { transform: scale(1); }
}

.crack-lines {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}

.crack-lines::before,
.crack-lines::after {
content: '';
position: absolute;
background: #2c3e50;
opacity: 0.7;
}

.crack-lines::before {
width: 2px;
height: 80%;
top: 10%;
left: 30%;
transform: rotate(45deg);
}

.crack-lines::after {
width: 2px;
height: 60%;
top: 20%;
left: 60%;
transform: rotate(-30deg);
}

#scoreBoard {
text-align: center;
color: white;
margin-bottom: 20px;
}

#score {
font-size: 24px;
font-weight: bold;
color: #3498db;
}

#message {
font-size: 18px;
margin-top: 10px;
height: 30px;
}

button {
background: #3498db;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
margin-top: 10px;
transition: background 0.3s ease;
}

button:hover {
background: #2980b9;
}

.hammer {
position: absolute;
width: 40px;
height: 40px;
pointer-events: none;
z-index: 1000;
display: none;
}

.hammer::before {
content: '🔨';
font-size: 30px;
}
</style>
</head>
<body>
<div id="gameContainer">
<div id="scoreBoard">
<h1>敲玻璃游戲</h1>
<div id="score">得分: 0</div>
<div id="message"></div>
<button onclick="resetGame()">重新開始</button>
</div>
<div id="gameBoard"></div>
</div>
<div class="hammer" id="hammer"></div>

? ? <script>
let score = 0;
let gameBoard = document.getElementById('gameBoard');
let scoreDisplay = document.getElementById('score');
let messageDisplay = document.getElementById('message');
let hammer = document.getElementById('hammer');
let glasses = [];

// 創建游戲板
function createBoard() {
gameBoard.innerHTML = '';
glasses = [];

for (let i = 0; i < 48; i++) {
let glass = document.createElement('div');
glass.className = 'glass';
glass.dataset.index = i;
glass.dataset.state = 'intact';

glass.addEventListener('click', hitGlass);
glass.addEventListener('mouseenter', showHammer);
glass.addEventListener('mouseleave', hideHammer);

gameBoard.appendChild(glass);
glasses.push(glass);
}
}

// 敲玻璃
function hitGlass(e) {
let glass = e.target;
let state = glass.dataset.state;

if (state === 'broken') return;

if (state === 'intact') {
glass.dataset.state = 'cracked';
glass.classList.add('cracked');

let crackLines = document.createElement('div');
crackLines.className = 'crack-lines';
glass.appendChild(crackLines);

score += 10;
showMessage('裂開了!+10分');

// 添加破碎音效(使用Web Audio API模擬)
playBreakSound();
} else if (state === 'cracked') {
glass.dataset.state = 'broken';
glass.classList.add('broken');
glass.innerHTML = '';

score += 20;
showMessage('破碎了!+20分');
playShatterSound();
}

updateScore();
checkWin();
}

// 顯示錘子光標
function showHammer(e) {
hammer.style.display = 'block';
hammer.style.left = e.pageX - 20 + 'px';
hammer.style.top = e.pageY - 20 + 'px';
}

// 隱藏錘子光標
function hideHammer() {
hammer.style.display = 'none';
}

// 鼠標移動時更新錘子位置
document.addEventListener('mousemove', (e) => {
if (hammer.style.display === 'block') {
hammer.style.left = e.pageX - 20 + 'px';
hammer.style.top = e.pageY - 20 + 'px';
}
});

// 更新分數
function updateScore() {
scoreDisplay.textContent = `得分: ${score}`;
}

// 顯示消息
function showMessage(msg) {
messageDisplay.textContent = msg;
setTimeout(() => {
messageDisplay.textContent = '';
}, 1000);
}

// 檢查是否獲勝
function checkWin() {
let brokenCount = glasses.filter(g => g.dataset.state === 'broken').length;
if (brokenCount === glasses.length) {
showMessage('恭喜!所有玻璃都被敲碎了!');
setTimeout(() => {
alert(`游戲結束!最終得分:${score}`);
}, 500);
}
}

// 重置游戲
function resetGame() {
score = 0;
updateScore();
createBoard();
showMessage('游戲重新開始!');
}

// 模擬破碎音效
function playBreakSound() {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();

oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);

oscillator.frequency.value = 200;
oscillator.type = 'square';
gainNode.gain.setValueAtTime(0.1, audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + 0.1);

oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.1);
}

// 模擬粉碎音效
function playShatterSound() {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();

for (let i = 0; i < 3; i++) {
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();

oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);

oscillator.frequency.value = 100 + Math.random() * 200;
oscillator.type = 'sawtooth';
gainNode.gain.setValueAtTime(0.1, audioContext.currentTime + i * 0.05);
gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + i * 0.05 + 0.1);

oscillator.start(audioContext.currentTime + i * 0.05);
oscillator.stop(audioContext.currentTime + i * 0.05 + 0.1);
}
}

// 初始化游戲
createBoard();
</script>
</body>
</html>

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

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

相關文章

996引擎-ItemTips特效框層級自定義

996引擎-ItemTips特效框層級自定義 需求場景 ItemTips 中相關方法 創建特效的位置 創建特效框 核心修改 調整視圖,自己加個背景,不用原來的 設置 tipsLayout_bg 的層級 結果預覽 參考資料 需求場景 策劃說我們的tips特效框,遮擋文字。如果按官方說的設為底層又跑到背景框后…

Java 注解與 APT(Annotation Processing Tool)

Java 注解與 APT&#xff08;Annotation Processing Tool&#xff09; 注解&#xff08;Annotation&#xff09;基礎 注解是 Java 語言的一種元數據形式&#xff0c;它可以在代碼中添加標記信息&#xff0c;用于描述代碼的額外信息&#xff0c;但不會直接影響代碼的執行邏輯。注…

Unity 檢測網絡-判斷當前(Android/Windows平臺)設備是否連接了指定WiFi

判斷設備是否連接了特定的網絡1.Unity 腳本2.Unity AndroidManifest.xml文件①改個設置②補充權限語句1.Unity 腳本 using UnityEngine; using System.Collections; using System.Diagnostics; using Debug UnityEngine.Debug; using UnityEngine.UI;#if UNITY_ANDROID &…

通過網絡強化增強混合IT環境的安全

網絡是企業運營的支柱&#xff0c;也是網絡犯罪分子和惡意威脅者的主要目標&#xff0c;他們會破壞IT運營的連續性。隨著混合云基礎設施、遠程辦公和物聯網&#xff08;IoT&#xff09;生態系統的出現&#xff0c;網絡邊界正在不斷擴大&#xff0c;新的漏洞不斷產生&#xff0c…

ACP(四):RAG工作流程及如何創建一個RAG應用

RAG的工作原理 你在考試的時候有可能會因為忘記某個概念或公式而失去分數&#xff0c;但考試如果是開卷形式&#xff0c;那么你只需要找到與考題最相關的知識點&#xff0c;并加上你的理解就可以進行回答了。 對于大模型來說也是如此&#xff0c;在訓練過程中由于沒有見過某個知…

宇視設備視頻平臺EasyCVR視頻設備軌跡回放平臺監控攝像頭故障根因剖析

監控攝像頭的類型繁多&#xff0c;市場上提供了廣泛的選擇。然而&#xff0c;在使用監控攝像頭的過程中&#xff0c;用戶可能會遇到云臺在很短的時間內出現運轉不靈或完全無法轉動的問題。這里&#xff0c;我們將對這一常見問題進行深入分析。一、具體的原因&#xff1a; 1、距…

【Uni-App+SSM 寵物項目實戰】Day15:購物車添加

大家好!今天是學習路線的第15天,我們正式進入訂單與購物車核心模塊。昨天完成了商家服務列表的分頁加載,今天聚焦“購物車添加”功能——這是連接“商品瀏覽”與“訂單提交”的關鍵環節,用戶可將寵物用品(如糧食、玩具)加入購物車,后續統一結算。 為什么學這個? 購物車…

Java 黑馬程序員學習筆記(進階篇6)

常用的 API1. 正則表達式(1) 題目&#xff1a;貪婪爬取和非貪婪爬取① 貪婪爬取&#xff1a;爬取數據的時候盡可能的多獲取數據 ② 非貪婪爬取&#xff1a;爬取數據的時候盡可能的少獲取數據 ③ Java中默認的是貪婪爬取 ④ 后面加上 ? 可以轉變為非貪婪爬取(2) 捕獲分組捕獲分…

計算機網絡---數據鏈路層上

文章目錄1. 數據鏈路層的功能2. 組幀2.1 字符填充法2.2 字節填充法2.3 零比特填充法2.4 違規編碼2.5 總結3. 差錯控制3.1 檢錯編碼3.1.1 奇偶校驗3.1.2 循環冗余校驗碼&#xff08;CRC&#xff09;3.1.3 總結3.2 糾錯編碼&#xff08;海明校驗碼&#xff09;3.3 總結4. 流量控制…

機器學習實戰項目中,回歸與分類模型中該如何科學定義目標變量Y?

前言 在機器學習項目里&#xff0c;目標變量 (Y) 的定義決定了你能解答什么問題&#xff0c;以及模型能給業務帶來什么價值。選擇不當不僅可能導致模型誤差大、偏差嚴重&#xff0c;還可能讓業務決策方向偏離。 本文分兩大場景&#xff1a; 供應鏈項目中的 銷量預測&#xff08…

【 C/C++ 算法】入門動態規劃-----一維動態規劃基礎(以練代學式)

每日激勵&#xff1a;“不設限和自我肯定的心態&#xff1a;I can do all things。 — Stephen Curry” 緒論?&#xff1a; 本章是動態規劃算法的基礎入門篇&#xff0c;我將通過三道簡單題 一道中等難度的一維動態規劃題來帶你對動態規劃有個初認識&#xff0c;并基本了解動…

深入對比Tomcat與Netty:HTTP請求從網卡到Controller的全鏈路追蹤

我們日常用Spring Boot寫的RestController&#xff0c;感覺上就是一個簡單的方法&#xff0c;但它背后其實有一套復雜的網絡服務在支撐。一個HTTP請求到底是怎么從用戶的瀏覽器&#xff0c;穿過層層網絡&#xff0c;最終抵達我們代碼里的Controller方法的&#xff1f;理解這個過…

GO學習記錄十——發包

記錄下不同平臺的發包操作和期間遇到的問題 1.命令&#xff1a; $env:GOOSlinux $env:GOARCHamd64 go build -o release/HTTPServices-linux第一行&#xff0c;配置平臺&#xff0c;linux、windows 第二行&#xff0c;配置部署服務器的處理器架構 第三行&#xff0c;輸出目標文…

貪心算法與動態規劃

1. 什么是貪心算法&#xff1f; 貪心算法是一種在每一步選擇中都采取在當前狀態下最好或最優&#xff08;即最有利&#xff09;的選擇&#xff0c;從而希望導致結果是全局最好或最優的算法。 核心思想&#xff1a;“每步都貪心地選擇眼前最好的&#xff0c;不去考慮整個未來的長…

學會“讀網頁”:生成式 AI 在足球賽事信息整理中的實戰

逐步教程&#xff08;Step-by-Step&#xff09; — 適合初學者與教學類文章 背景&#xff08;為什么要這樣做&#xff09; 對于足球迷、資訊編輯與數據分析師來說&#xff0c;最快、最準確把握一場比賽的核心信息至關重要&#xff1a;比分、關鍵事件&#xff08;進球、點球、紅…

BM3D 圖像降噪快速算法的 MATLAB 實現

BM3D 圖像降噪快速算法的 MATLAB 實現1. 快速 BM3D 算法流程&#xff08;概述&#xff09;步驟操作加速技巧① 分組塊匹配 堆疊FFT 互相關② 協同濾波3D 變換 硬閾值FFT 沿第三維③ 聚合加權平均稀疏矩陣累加 2. 核心函數&#xff08;單文件版&#xff09; 保存為 bm3d_fast.…

Go的schedt調度(runtime/proc.go)

1. 創建go的入口函數// Create a new g running fn. // Put it on the queue of gs waiting to run. // The compiler turns a go statement into a call to this. func newproc(fn *funcval) {gp : getg()pc : sys.GetCallerPC()systemstack(func() {newg : newproc1(fn, gp, …

Ubuntu 服務器配置轉發網絡訪問

配置文檔&#xff1a;Ubuntu 服務器轉發網絡訪問 一、網絡拓撲以以下網絡拓撲為示例Ubuntu 服務器&#xff08;兩個網卡&#xff09; eth1 10.66.71.222 &#xff08;接入內網&#xff09;eno1 192.168.2.100 &#xff08;直連相機&#xff09; 相機ip 192.168.2.1 Windows 客…

為什么企業需要高防IP

1. 抵御日益猖獗的DDoS攻擊 現代DDoS攻擊規模已突破Tbps級別 傳統防火墻無法應對大規模流量攻擊 高防IP采用分布式清洗中心&#xff0c;可輕松抵御300Gbps以上的攻擊流量 2. 保障業務連續性 網絡中斷1小時可能造成數百萬損失 高防IP確保服務99.99%可用性 智能切換機制實…

CSS基礎 - 選擇器備忘錄 --筆記5

目錄基礎選擇器組合器偽類選擇器屬性選擇器選擇器可以選中頁面上的特定元素并為其指定樣式。 CSS有多種選擇器。 基礎選擇器 標簽選擇器 – tagname&#xff1a;匹配目標元素的標簽名。優先級是0,0,1。如&#xff1a;p、h1、div類選擇器 – .class&#xff1a;匹配class屬性中…