用html,js和layui写一个简单的点击打怪小游戏

简介: 用html,js和layui写一个简单的点击打怪小游戏

介绍:

   一个简单的打怪小游戏,点击开始游戏后,出现攻击按钮,击败怪物后可以选择继续下一关和结束游戏。

   继续下一个怪兽的血量会增加5点,攻击按钮会随机变色。

效果图:

html代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>打怪</title>
<link rel="stylesheet" href="../layui/css/layui.css" media="all">
<style>
/* 添加样式以中心布局两个游戏并增加一些间距 */
.game-container {
  text-align: center;
  margin: 5% auto;
}
.game-container > button {
  margin: 0 5px; /* 添加按钮间距 */
}
#result, #message {
  margin: 20px 0;
}
</style>
</head>
<body>
<!-- 在HTML中添加开始游戏按钮 -->
<div class="game-container">
  <h1>打怪小游戏</h1>
  <h2 id="monster">怪物 HP: <span id="monsterHp">10</span></h2>
  <button id="startGameButton" class="layui-btn layui-btn-primary">开始游戏</button>
  <button id="attackButton" class="layui-btn layui-btn-warm" style="display: none;">攻击怪物</button>
  <button id="nextLevelButton" class="layui-btn layui-btn-normal" style="display: none;">进入下一关</button>
  <button id="endGameButton" class="layui-btn layui-btn-danger" style="display: none;">结束游戏</button>
  <p id="message"></p>
</div>
  <script src="./gamejs/game.js"></script>
</body>
</html>

js代码:

document.addEventListener('DOMContentLoaded', () => {
    const monsterHpElement = document.getElementById('monsterHp');
    const messageElement = document.getElementById('message');
    const startGameButton = document.getElementById('startGameButton');
    const nextLevelButton = document.getElementById('nextLevelButton');
    const endGameButton = document.getElementById('endGameButton');
    let initialHp = 10;
    let monsterHp;
    let level = 1;
    const hpIncrease = 5;
    const attackButton = document.getElementById('attackButton');
    function startGame() {
        monsterHp = initialHp;
        level = 1;
        monsterHpElement.innerText = monsterHp;
        messageElement.innerText = '怪物出现了!';
        resetAttackButtonColor();
        attackButton.style.display = 'inline-block';
        startGameButton.style.display = 'none';
    }
    function updateMonsterStatus() {
        if (monsterHp > 0) {
            monsterHp--;
            monsterHpElement.innerText = monsterHp;
            messageElement.innerText = `怪物受到伤害!还剩 ${monsterHp} 生命值。`;
        } else {
            nextLevelButton.style.display = 'inline-block';
            endGameButton.style.display = 'inline-block';
            attackButton.style.display = 'none';
            messageElement.innerText = '恭喜你,打败了怪物!选择下一步操作。';
        }
    }
    function levelUp() {
        level++;
        monsterHp = initialHp + hpIncrease * (level - 1);
        monsterHpElement.innerText = monsterHp;
        messageElement.innerText = `第 ${level} 关开始!怪物生命值为 ${monsterHp}。`;
        attackButton.style.display = 'inline-block';
        nextLevelButton.style.display = 'none';
        endGameButton.style.display = 'none';
        changeAttackButtonColor();
    }
    function endGame() {
        messageElement.innerText = '游戏结束,感谢您的玩耍!点击下方按钮可以重新开始。';
        attackButton.style.display = 'none';
        nextLevelButton.style.display = 'none';
        endGameButton.style.display = 'none';
        startGameButton.style.display = 'inline-block'; // 显示开始游戏按钮
    }
    function resetAttackButtonColor() {
        attackButton.className = 'layui-btn layui-btn-warm'; // 这里设置默认的按钮颜色样式
    }
    function changeAttackButtonColor() {
        // 定义一组可能的颜色
        const colors = ['layui-btn-primary', 'layui-btn-normal', 'layui-btn-warm', 'layui-btn-danger'];
        // 随机选择一个颜色
        const randomColor = colors[Math.floor(Math.random() * colors.length)];
        attackButton.className = `layui-btn ${randomColor}`; // 更新按钮的 class 以改变颜色
    }
    startGameButton.addEventListener('click', startGame);
    nextLevelButton.addEventListener('click', levelUp);
    endGameButton.addEventListener('click', endGame);
    attackButton.addEventListener('click', updateMonsterStatus);
});
document.addEventListener02('DOMContentLoaded', () => {
    const monsterHpElement = document.getElementById('monsterHp');
    const messageElement = document.getElementById('message');
    const startGameButton = document.getElementById('startGameButton');
    const nextLevelButton = document.getElementById('nextLevelButton');
    const endGameButton = document.getElementById('endGameButton');
    let initialHp = 10;
    let monsterHp;
    let level = 1;
    const hpIncrease = 5;
    const attackButton = document.getElementById('attackButton');
    function startGame() {
        monsterHp = initialHp;
        level = 1;
        monsterHpElement.innerText = monsterHp;
        messageElement.innerText = '怪物出现了!';
        attackButton.style.display = 'inline-block';
        startGameButton.style.display = 'none';
    }
    function updateMonsterStatus() {
        if (monsterHp > 0) {
            monsterHp--;
            monsterHpElement.innerText = monsterHp;
            messageElement.innerText = `怪物受到伤害!还剩 ${monsterHp} 生命值。`;
        } else {
            nextLevelButton.style.display = 'inline-block';
            endGameButton.style.display = 'inline-block';
            attackButton.style.display = 'none';
            messageElement.innerText = '恭喜你,打败了怪物!选择下一步操作。';
        }
    }
    function levelUp() {
        level++;
        monsterHp = initialHp + hpIncrease * (level - 1);
        monsterHpElement.innerText = monsterHp;
        messageElement.innerText = `第 ${level} 关开始!怪物生命值为 ${monsterHp}。`;
        attackButton.style.display = 'inline-block';
        nextLevelButton.style.display = 'none';
        endGameButton.style.display = 'none';
    }
    function endGame() {
        messageElement.innerText = '游戏结束,感谢您的玩耍!点击下方按钮可以重新开始。';
        attackButton.style.display = 'none';
        nextLevelButton.style.display = 'none';
        endGameButton.style.display = 'none';
        startGameButton.style.display = 'inline-block'; // 显示开始游戏按钮
    }
    startGameButton.addEventListener('click', startGame);
    nextLevelButton.addEventListener('click', levelUp);
    endGameButton.addEventListener('click', endGame);
    attackButton.addEventListener('click', updateMonsterStatus);
});
相关文章
|
16天前
|
JavaScript 前端开发
js实现点击音频实现播放功能
js实现点击音频实现播放功能
|
1月前
|
JavaScript 前端开发
springboot+layui从控制器请求至页面时js失效的解决方法
springboot+layui从控制器请求至页面时js失效的解决方法
16 0
|
2天前
|
JavaScript 前端开发 BI
原生html—摆脱ps、excel 在线绘制财务表格加水印(html绘制表格js加水印)
原生html—摆脱ps、excel 在线绘制财务表格加水印(html绘制表格js加水印)
7 1
|
9天前
鼠标点击效果.html(网上收集6)
鼠标点击效果.html(网上收集6)
|
10天前
|
JavaScript 前端开发
js怎么删除html元素
js怎么删除html元素
23 10
|
19天前
|
JSON JavaScript 前端开发
js是什么、html、css
js是什么、html、css
|
19天前
|
JavaScript
js生成二维码点击可放大
js生成二维码点击可放大
|
30天前
|
JavaScript 前端开发 算法
游戏物理系统 - 介绍一下Box2D或其他物理引擎在JS小游戏中的使用。
Box2D, a popular 2D physics engine, simulates rigid body dynamics, collision detection, and constraints for JavaScript games via WebAssembly. It offers realistic physics, efficient collision handling, and customizable APIs.
17 4
|
1月前
|
JavaScript 计算机视觉
纯js实现人脸识别眨眨眼张张嘴案例——index.html
纯js实现人脸识别眨眨眼张张嘴案例——index.html
18 0
|
JavaScript 前端开发 关系型数据库
JavaScript小游戏,键盘英雄!
该游戏仅供探讨交流。 该游戏可随意传播,请保留声明与出处。 几年前的一个夜晚孤枕难眠,突然来了灵感。创作游戏的初衷是巩固js基础,有朋友问我,为什么取这个名字呢? 可以想象你是电影的主角,打字的速度已达到独孤求败的境界,狂风呼啸电闪雷鸣,键盘声响起,键盘在你指尖灵动肆意纷飞,眨眼的功夫一部20万字撰写完毕。
1099 0