需要素材的同学可以私信我
效果图:
上代码:
<!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>
感谢大家的阅读,如有不对的地方,可以向我提出,感谢大家!