JS之打地鼠案例

2024-01-25 05:12
文章标签 js 案例 地鼠

本文主要是介绍JS之打地鼠案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需要素材的同学可以私信我
效果图:

请添加图片描述

上代码:

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title></title><style>* {margin: 0;padding: 0;}.box {position: relative;width: 320px;height: 480px;background: url("img/game_bg.jpg") no-repeat;margin: 100px auto;}.score {position: absolute;color: white;font-size: 20px;top: 2.2%;left: 18%;}.time {position: absolute;background: url("img/progress.png");top: 66px;left: 62px;width: 180px;height: 16px;}.stop1 {width: 50px;height: 50px;background: url("img/stop.png") no-repeat center;position: absolute;top: 100px;left: 10px;}.start,.reset {width: 100px;height: 100px;background-color: pink;opacity: 0.4;border-radius: 50px;text-align: center;position: absolute;top: 130px;left: 0;bottom: 0;right: 0;margin: auto;font-size: 20px;cursor: pointer;}.gameOver {position: absolute;top: 0;text-align: center;color: white;font-size: 20px;width: 100%;height: 480px;line-height: 480px;background-color: black;opacity: 0.3;}.gameOver span {color: red;font-size: 25px;}.gameOver,.reset,.stop1 {display: none;}.box img {position: absolute;}</style>
</head><body><!-- 大盒子 --><div class="box"><!-- 得分 --><div class="score">0</div><!-- 时间流逝条 --><div class="time"></div><!-- 暂停按钮 --><div class="stop1"></div><!-- 开始 --><div class="start"><span style="z-index: 1;position: relative;left: 0px;top: 35px;color: red;">点击开始</span></div><!-- 游戏结束 --><div class="gameOver">游戏结束最终得分:<span>0</span></div><!-- 重新开始 --><div class="reset"><span style="z-index: 1;position: relative;left: 0px;top: 35px;color: red;">重新开始</span></div><!-- <img src="img/h5.png" alt=""> --></div>
</body>
<script>// 页面初始化window.onload = function () {// 获取大盒子var box = document.querySelector(".box")// 获取分数var score = document.querySelector(".score")// 获取进度条var timeBox = document.querySelector(".time")// 获取暂停按钮var stopBtn = document.querySelector(".stop1")// 获取开始按钮var startBtn = document.querySelector(".start")// 获取重新开始按钮var resetBox = document.querySelector(".reset")// 获取gameovervar gameOverbox = document.querySelector(".gameOver")// 得分var s = 0// 计时器var timer// 游戏状态,t代表开始,f代表暂停var state = true// 定义9个地洞的坐标值wolf_position = [{// 最上面的洞top: "115px",left: "95px"},{// 第二排第一个top: "160px",left: "16px"},{// 第二排第二个top: "143px",left: "185px"},{// 第二列第二个top: "194px",left: "101px"},{// 第三排第一个top: "220px",left: "14px"},{// 第四排第一个top: "293px",left: "28px"},{// 第三排第三个top: "212px",left: "197px"},{// 第二列第三个top: "274px",left: "117px"},{// 第四排第三个top: "296px",left: "205px"}]// 进度条初始宽度var timeWidth = timeBox.offsetWidth// console.log(timeWidth);// 点击开始按钮的时候startBtn.onclick = function () {// 隐藏开始按钮startBtn.style.display = "none"// 暂停按钮显示stopBtn.style.display = "block"// 进度条开始计时progressStart()// 游戏开始出现狼showWolf()// 游戏开始出现addWolf()}// 进度条计时function progressStart() {timer = setInterval(function () {timeBox.style.width = timeWidth + "px"timeWidth--if (timeWidth <= 0) {// 小于180时结束游戏clearInterval(timer)// alert("游戏结束")// 调用游戏结束gameOver()}}, 100)}// 游戏结束function gameOver() {// 重新开始按钮出现resetBox.style.display = "block"// 游戏结束标语出现gameOverbox.style.display = "block"// 游戏结束狼停止出现clearInterval(wolfTimer)gameOverbox.innerHTML = "游戏结束最终得分:" + sresetBtn()}// 暂停游戏stopBtn.onclick = function () {if (state) {// 清除定时器clearInterval(timer)// 停止时暂停生产狼clearInterval(wolfTimer)// 换成开始按钮this.style.backgroundImage = "url(img/start.png)"// 变成falsestate = false} else {// 启用定时器,调用progressStart()// 开始时显示狼showWolf()this.style.backgroundImage = "url(img/stop.png)"state = true}}// 灰太狼// 判断是否重复var nub = -1// 灰太狼轮播var wolfLuntimer// 狼下降var downWolftimervar wolfDowntimer// 狼的定时器var wolfTimerfunction addWolf() {// 创建节点var wolf = document.createElement("img")// 随机数0-8var index = Math.floor(Math.random() * 9)// 如果上一个重复重新赋值while (index == nub) {index = Math.floor(Math.random() * 9)}nub = indexconsole.log(index);// 坑位console.log(wolf_position[index]);// 赋值wolf.style.top = wolf_position[index].topwolf.style.left = wolf_position[index].left// 添加到box后面box.appendChild(wolf)// 随机出来的是小灰灰还是灰太狼var n = Math.floor(Math.random() * 10)c = ""if (n >= 3) {c = "h"} else {c = "x"}// 定义狼的下标轮播效果var Wolfindex = 0// 狼轮播// addWolf(c)wolfLuntimer = setInterval(function () {// 轮播// addWolf(c)wolf.src = "img/" + c + Wolfindex + ".png"Wolfindex++if (Wolfindex > 5) {clearInterval(wolfLuntimer)}}, 50)// 定义下标为5var downIndex = 5// 让狼下降,要延迟下降wolfDowntimer = setTimeout(function () {// 延时器里执行定时器downWolftimer = setInterval(function () {wolf.src = "img/" + c + downIndex + ".png"downIndex--if (downIndex == -1) {// downIndex = 5// clearInterval(downWolftimer)// clearTimeout(wolfDowntimer)// 移除元素box.removeChild(wolf)}}, 50)}, 1000)// 传入参数wolfScore(wolf)}// 批量显示function showWolf() {wolfTimer = setInterval(function () {addWolf()}, 1300)}// 不能连续击打var strike = 0// 打狼得分function wolfScore(wolf) {wolf.onclick = function () {if (strike == 0) {strike = 1console.log(1);// 打击前关闭下降动画clearTimeout(wolfDowntimer)clearInterval(downWolftimer)// 判断是小灰灰还是灰太狼if (c == "h") {s += 10} else {s -= 10}score.innerHTML = s// 如果小于0 不扣不变为负数if (score.innerHTML < 0) {score.innerHTML = 0}var koindex = 5// 被打中的动画wolf_ko = setInterval(function () {wolf.src = "img/" + c + koindex + ".png"koindex++if (koindex > 9) {clearInterval(wolf_ko)box.removeChild(wolf)strike = 0}}, 50)}}}// 重新开始function resetBtn() {// 隐藏当前按钮resetBox.onclick = function () {// 隐藏当前按钮this.style.display = "none"gameOverbox.style.display = "none"// 进度条填满timeWidth = 180timeBox.style.width = timeWidth + "px"// 调用进度条progressStart()// 重新赋值得分s = 0score.innerHTML = sshowWolf()wolfScore()}}}
</script>
</html>

感谢大家的阅读,如有不对的地方,可以向我提出,感谢大家!

这篇关于JS之打地鼠案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/642189

相关文章

PyTorch核心方法之state_dict()、parameters()参数打印与应用案例

《PyTorch核心方法之state_dict()、parameters()参数打印与应用案例》PyTorch是一个流行的开源深度学习框架,提供了灵活且高效的方式来训练和部署神经网络,这篇文章主要介绍... 目录前言模型案例A. state_dict()方法验证B. parameters()C. 模型结构冻

mysql_mcp_server部署及应用实践案例

《mysql_mcp_server部署及应用实践案例》文章介绍了在CentOS7.5环境下部署MySQL_mcp_server的步骤,包括服务安装、配置和启动,还提供了一个基于Dify工作流的应用案例... 目录mysql_mcp_server部署及应用案例1. 服务安装1.1. 下载源码1.2. 创建独立

mybatis-plus分表实现案例(附示例代码)

《mybatis-plus分表实现案例(附示例代码)》MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生,:本文主要介绍my... 目录文档说明数据库水平分表思路1. 为什么要水平分表2. 核心设计要点3.基于数据库水平分表注意事项示例

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.

Redis 命令详解与实战案例

《Redis命令详解与实战案例》本文详细介绍了Redis的基础知识、核心数据结构与命令、高级功能与命令、最佳实践与性能优化,以及实战应用场景,通过实战案例,展示了如何使用Redis构建高性能应用系统... 目录Redis 命令详解与实战案例一、Redis 基础介绍二、Redis 核心数据结构与命令1. 字符

通过DBeaver连接GaussDB数据库的实战案例

《通过DBeaver连接GaussDB数据库的实战案例》DBeaver是一个通用的数据库客户端,可以通过配置不同驱动连接各种不同的数据库,:本文主要介绍通过DBeaver连接GaussDB数据库的... 目录​一、前置条件​二、连接步骤​三、常见问题与解决方案​1. 驱动未找到​2. 连接超时​3. 权限不

Java中的随机数生成案例从范围字符串到动态区间应用

《Java中的随机数生成案例从范围字符串到动态区间应用》本文介绍了在Java中生成随机数的多种方法,并通过两个案例解析如何根据业务需求生成特定范围的随机数,本文通过两个实际案例详细介绍如何在java中... 目录Java中的随机数生成:从范围字符串到动态区间应用引言目录1. Java中的随机数生成基础基本随

SpringMVC配置、映射与参数处理​入门案例详解

《SpringMVC配置、映射与参数处理​入门案例详解》文章介绍了SpringMVC框架的基本概念和使用方法,包括如何配置和编写Controller、设置请求映射规则、使用RestFul风格、获取请求... 目录1.SpringMVC概述2.入门案例①导入相关依赖②配置web.XML③配置SpringMVC