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

相关文章

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

六个案例搞懂mysql间隙锁

《六个案例搞懂mysql间隙锁》MySQL中的间隙是指索引中两个索引键之间的空间,间隙锁用于防止范围查询期间的幻读,本文主要介绍了六个案例搞懂mysql间隙锁,具有一定的参考价值,感兴趣的可以了解一下... 目录概念解释间隙锁详解间隙锁触发条件间隙锁加锁规则案例演示案例一:唯一索引等值锁定存在的数据案例二:

MySQL 表的内外连接案例详解

《MySQL表的内外连接案例详解》本文给大家介绍MySQL表的内外连接,结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录表的内外连接(重点)内连接外连接表的内外连接(重点)内连接内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选,我

使用Python获取JS加载的数据的多种实现方法

《使用Python获取JS加载的数据的多种实现方法》在当今的互联网时代,网页数据的动态加载已经成为一种常见的技术手段,许多现代网站通过JavaScript(JS)动态加载内容,这使得传统的静态网页爬取... 目录引言一、动态 网页与js加载数据的原理二、python爬取JS加载数据的方法(一)分析网络请求1