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.mzph.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;同时存在的问题请在相关博客留言…