angular打地鼠

說明:我計劃用angular做一款打地鼠的小游戲,

打地鼠游戲實現文檔

🎮 游戲邏輯

  • ?游戲場景
    采用 3x3 網格布局的 9 個地鼠洞
  • ?核心機制
    • 地鼠隨機從洞口彈出
    • 點擊有效目標獲得積分
    • 30 秒倒計時游戲模式
  • ?難度系統
    • 簡單模式:生成間隔 1.5s / 停留 1s
    • 普通模式:生成間隔 1s / 停留 0.8s
    • 困難模式:生成間隔 0.8s / 停留 0.6s

?? 主要功能

  • ?游戲控制
    • 開始/結束游戲按鈕
    • 游戲進行中禁止重復啟動
  • ?狀態顯示
    • 實時分數更新
    • 動態倒計時顯示
  • ?交互系統
    • 可視化難度選擇按鈕
    • 點擊命中即時反饋
  • ?動畫效果
    • 地鼠彈出/收回動畫
    • 按鈕激活狀態指示

🔧 實現細節

效果圖

在這里插入圖片描述

技術實現

step1:C:\Users\wangrusheng\WebstormProjects\untitled4\src\app\mouse\mouse.component.ts

import { Component } from '@angular/core';
import {NgForOf, NgIf, TitleCasePipe} from '@angular/common';@Component({selector: 'app-mouse',imports: [NgForOf,NgIf,TitleCasePipe],templateUrl: './mouse.component.html',styleUrl: './mouse.component.css'
})
export class MouseComponent {score = 0;timeLeft = 30;isPlaying = false;holes = Array(9).fill(false);private gameTimer: any;private moleTimer: any;// 新增類型安全難度列表readonly difficultyLevels = ['easy', 'medium', 'hard'] as const;difficulty: 'easy' | 'medium' | 'hard' = 'medium';// 根據難度調整參數get intervalTime() {return {easy: 1500,medium: 1000,hard: 800}[this.difficulty];}get activeTime() {return {easy: 1000,medium: 800,hard: 600}[this.difficulty];}startGame() {if (this.isPlaying) return;this.isPlaying = true;this.score = 0;this.timeLeft = 30;// 游戲倒計時this.gameTimer = setInterval(() => {this.timeLeft--;if (this.timeLeft <= 0) {this.endGame();}}, 1000);// 地鼠生成this.moleTimer = setInterval(() => {this.popUpMole();}, this.intervalTime);}endGame() {clearInterval(this.gameTimer);clearInterval(this.moleTimer);this.isPlaying = false;this.holes = this.holes.map(() => false);}popUpMole() {if (!this.isPlaying) return;const index = Math.floor(Math.random() * 9);this.holes[index] = true;setTimeout(() => {this.holes[index] = false;}, this.activeTime);}whackMole(index: number) {if (this.holes[index] && this.isPlaying) {this.score += 10;this.holes[index] = false;}}setDifficulty(level: 'easy' | 'medium' | 'hard') {this.difficulty = level;}
}

step2:
C:\Users\wangrusheng\WebstormProjects\untitled4\src\app\mouse\mouse.component.html

<!-- mouse.component.html -->
<div class="game-card"><div class="game-container"><div class="game-info"><div class="info-group"><div class="info-item"><span class="label">Score:</span><span class="value">{{ score }}</span></div><div class="info-item"><span class="label">Time:</span><span class="value">{{ timeLeft }}</span></div></div><div class="difficulty-group"><span class="label">Difficulty:</span><div class="button-group"><button*ngFor="let diff of difficultyLevels"[class.active]="difficulty === diff"(click)="setDifficulty(diff)"class="difficulty-btn">{{ diff | titlecase }}</button></div></div><buttonclass="start-btn"(click)="startGame()"[disabled]="isPlaying">{{ isPlaying ? 'Playing...' : 'Start Game' }}</button></div><div class="game-board"><div*ngFor="let hole of holes; let i = index"class="hole"[class.active]="hole"(click)="whackMole(i)"><div class="mole" *ngIf="hole"></div></div></div></div>
</div>

step3:
C:\Users\wangrusheng\WebstormProjects\untitled4\src\app\mouse\mouse.component.css

/* mouse.component.css */
.game-card {background: #ffffff;border-radius: 16px;box-shadow: 0 10px 30px rgba(0,0,0,0.1);padding: 24px;max-width: 800px;margin: 2rem auto;transition: transform 0.3s ease;
}.game-card:hover {transform: translateY(-2px);
}.game-container {text-align: center;
}.game-info {margin-bottom: 32px;display: flex;flex-direction: column;gap: 24px;
}.info-group {display: flex;justify-content: center;gap: 32px;margin-bottom: 16px;
}.info-item {background: #f8f9fa;padding: 12px 24px;border-radius: 8px;box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}.label {font-weight: 600;color: #6c757d;margin-right: 8px;
}.value {font-weight: 700;color: #495057;
}.difficulty-group {display: flex;flex-direction: column;gap: 12px;align-items: center;
}.button-group {display: flex;gap: 12px;
}button {border: none;border-radius: 8px;padding: 10px 20px;font-weight: 600;cursor: pointer;transition: all 0.2s ease;text-transform: uppercase;letter-spacing: 0.5px;
}.difficulty-btn {background: #e9ecef;color: #495057;min-width: 90px;
}.difficulty-btn.active {background: #4e66f8;color: white;box-shadow: 0 4px 6px rgba(78,102,248,0.2);
}.start-btn {background: #20c997;color: white;padding: 14px 32px;font-size: 1.1rem;margin-top: 16px;
}.start-btn:disabled {background: #ced4da;box-shadow: none;
}.start-btn:not(:disabled):hover {background: #1aa179;box-shadow: 0 4px 14px rgba(26,161,121,0.3);
}.game-board {display: grid;grid-template-columns: repeat(3, 1fr);gap: 20px;max-width: 600px;margin: 0 auto;padding: 20px;background: #f8f9fa;border-radius: 12px;
}.hole {width: 150px;height: 150px;background: #654321;border-radius: 50%;position: relative;overflow: hidden;cursor: pointer;transition: transform 0.2s, background 0.3s;
}.hole:hover {transform: scale(1.02);
}.hole.active {background: #8b4513;box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}.mole {position: absolute;width: 80%;height: 80%;background: #808080;border-radius: 50%;bottom: -30%;left: 10%;transition: bottom 0.3s;animation: pop-up 0.3s forwards;
}@keyframes pop-up {from { bottom: -30%; }to { bottom: 10%; }
}

end

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

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

相關文章

博客網站(springboot)整合deepseek實現在線調用

&#x1f389;&#x1f389;&#x1f389;&#x1f389;&#x1f389;&#x1f389; 歡迎訪問的個人博客&#xff1a;https://swzbk.site/&#xff0c;加好友&#xff0c;拉你入福利群 &#x1f389;&#x1f389;&#x1f389;&#x1f389;&#x1f389;&#x1f389; 1、de…

Kubernetes 單節點集群搭建

Kubernetes 單節點集群搭建教程 本人嘗試基于Ubuntu搭建一個單節點K8S集群&#xff0c;其中遇到各種問題&#xff0c;最大的問題就是網絡&#xff0c;各種鏡像源下載不下來&#xff0c;特此記錄&#xff01;注意&#xff1a;文中使用了幾個鏡像&#xff0c;將看來可能失效導致安…

【PTA題目解答】7-3 字符串的全排列(20分)next_permutation

1.題目 給定一個全由小寫字母構成的字符串&#xff0c;求它的全排列&#xff0c;按照字典序從小到大輸出。 輸入格式: 一行&#xff0c;一個字符串&#xff0c;長度不大于8。 輸出格式: 輸出所有全排列&#xff0c;每行一種排列形式&#xff0c;字典序從小到大。 輸入樣例…

專題三0~n-1中缺失的數字

1.題目 給一個數組&#xff0c;單調性是遞增的&#xff0c;需要找到缺失的數字&#xff0c;加上這個數字就變為等差數組了。 2.算法原理 這里用二分來解決&#xff0c;而二段性是根據下標區分&#xff0c;臨界值前的數字于下標相對應&#xff0c;臨界值后的于下標相差1&#x…

【圖像處理】ISP(Image Signal Processor) 圖像處理器的用途和工作原理?

ISP&#xff08;圖像信號處理器&#xff09;是數字影像設備的“視覺大腦”&#xff0c;負責將傳感器捕獲的原始電信號轉化為我們看到的高清圖像。以下從用途和工作原理兩方面通俗解析&#xff1a; 一、ISP的核心用途&#xff1a;讓照片“更像眼睛看到的” 提升畫質&#xff1a…

python學習筆記-mysql數據庫操作

現有一個需求&#xff0c;調用高德api獲取全國縣級以上行政區數據并保存為json文件&#xff0c;使用python獲取&#xff1a; import requests import json# 高德API Key api_key "your_api_key"# 調用行政區域查詢API def fetch_districts():url f"https://r…

Redisson 實現分布式鎖源碼淺析

大家好&#xff0c;我是此林。 今天來分享Redisson分布式鎖源碼。還是一樣&#xff0c;我們用 問題驅動 的方式展開講述。 1. redis 中如何使用 lua 腳本&#xff1f; Redis內置了lua解釋器&#xff0c;lua腳本有兩個好處&#xff1a; 1. 減少多次Redis命令的網絡傳輸開銷。…

【軟件】免費的PDF全文翻譯軟件,能保留公式圖表的樣式

轉載請注明出處&#xff1a;小鋒學長生活大爆炸[xfxuezhagn.cn] 如果本文幫助到了你&#xff0c;歡迎[點贊、收藏、關注]哦~ 很多PDF全文翻譯軟件都是收費的&#xff0c;而劃線翻譯看著又很累。這個開源的PDF全文翻譯軟件非常好用&#xff0c;并且能夠保留公式、圖表、目錄和注…

CentOS 7 系統上安裝 SQLite

1. 檢查系統更新 在安裝新軟件之前&#xff0c;建議先更新系統的軟件包列表&#xff0c;以確保使用的是最新的軟件源和補丁。打開終端&#xff0c;執行以下命令&#xff1a; sudo yum update -y -y 選項表示在更新過程中自動回答 “yes”&#xff0c;避免手動確認。 2. 安裝 …

Gin(后端)和 Vue3(前端)中實現 Server-Sent Events(SSE)推送

在 Gin&#xff08;后端&#xff09;和 Vue3&#xff08;前端&#xff09;中實現 Server-Sent Events&#xff08;SSE&#xff09;推送&#xff0c;主要分為以下幾個步驟&#xff1a; 后端&#xff08;Gin&#xff09;實現 SSE Gin 框架可以使用 c.SSEvent 方法來推送 SSE 事…

大模型微調中顯存占用和訓練時間的影響因素

BatchSize 顯存占用&#xff1a;與batch_size呈線性關系&#xff0c;可理解為 M t o t a l M f i x e d B a t c h S i z e ? M p e r ? s a m p l e M_{total}M_{fixed}BatchSize*M_{per-sample} Mtotal?Mfixed?BatchSize?Mper?sample?&#xff0c;其中 M f i x e d…

【排序算法對比】快速排序、歸并排序、堆排序

排序算法對比&#xff1a;快速排序、歸并排序、堆排序 1. 快速排序&#xff08;Quick Sort&#xff09; 原理 快速排序采用 分治法&#xff08;Divide and Conquer&#xff09;&#xff0c;通過選取基準值&#xff08;pivot&#xff09;&#xff0c;將數組劃分為 小于基準值…

PentestGPT 下載

PentestGPT 下載 PentestGPT 介紹 PentestGPT&#xff08;Penetration Testing GPT&#xff09;是一個基于大語言模型&#xff08;LLM&#xff09;的智能滲透測試助手。它結合了 ChatGPT&#xff08;或其他 GPT 模型&#xff09;與滲透測試工具&#xff0c;幫助安全研究人員自…

防火墻虛擬系統實驗

一實驗拓撲 二實驗過程 配置資源 創建虛擬系統 配置管理員 創建安全策略

代碼隨想錄算法訓練營第31天 | 56. 合并區間 738.單調遞增的數字 968.監控二叉樹

56. 合并區間 代碼隨想錄 56. 合并區間 - 力扣&#xff08;LeetCode&#xff09; class Solution {public int[][] merge(int[][] intervals) {Arrays.sort(intervals,(a,b)->{if(a[0] b[0])return a[1] - b[1];return a[0] - b[0];});List<int[]> result new Arra…

Go語言對于MySQL的基本操作

一.下載依賴 終端中輸入&#xff1a; go get -u github.com/go-sql-driver/mysql 導入包 import ("database/sql"_ "github.com/go-sql-driver/mysql" ) 二.案例 package main//go get-u github.com/go-sql-driver/mysql 獲取驅動 import ("databa…

Linux與深入HTTP序列化和反序列化

深入HTTP序列化和反序列化 本篇介紹 在上一節已經完成了客戶端和服務端基本的HTTP通信&#xff0c;但是前面的傳遞并沒有完全體現出HTTP的序列化和反序列化&#xff0c;為了更好得理解其工作流程&#xff0c;在本節會以更加具體的方式分析到HTTP序列化和反序列化 本節會在介紹…

基于Python+SQLite實現(Web)驗室設備管理系統

實驗室設備管理系統 應用背景 為方便實驗室進行設備管理&#xff0c;某大學擬開發實驗室設備管理系統 來管理所有實驗室里的各種設備。系統可實現管理員登錄&#xff0c;查看現有的所有設備&#xff0c; 增加設備等功能。 開發環境 Mac OSPyCharm IDEPython3Flask&#xff…

深拷貝and淺拷貝!

一、什么是拷貝&#xff1f;什么是深拷貝和淺拷貝&#xff1f; &#xff08;1&#xff09;拷貝&#xff1a;拷貝就是為了復用原對象的部分or全部數據&#xff0c;在原對象的基礎上通過復制的方式創建一個新的對象。 拷貝對象可以分為三種類型&#xff1a;直接賦值、淺拷貝和深拷…

高頻面試題(含筆試高頻算法整理)基本總結回顧43

干貨分享&#xff0c;感謝您的閱讀&#xff01; &#xff08;暫存篇---后續會刪除&#xff0c;完整版和持續更新見高頻面試題基本總結回顧&#xff08;含筆試高頻算法整理&#xff09;&#xff09; 備注&#xff1a;引用請標注出處&#xff0c;同時存在的問題請在相關博客留言…