[数字华容道] Html+css+js 实现小游戏

效果图

代码预览

在线预览地址

代码示例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>数字华容道</title>
<style>
h1 {
text-align: center;
} .box {
border: 1px solid #cfcfcf;
margin: 0 auto;
width: 322px;
padding: 20px;
border-radius: 20px;
} .fun {
display: flex;
justify-content: space-between;
} td {
width: 100px;
height: 100px;
text-align: center;
background-color: #f1c385;
user-select: none;
} .current {
background-color: #fff !important;
transition: all .3s;
} #error {
color: red;
}
</style>
</head>
<body>
<div class="box">
<h1>数字华容道</h1>
<p><strong>规则:</strong>移动方块依次出现1、2、3、4、5、6、7、8就可通关!不能对角线移动,不能跳格子移动。只能相邻上下或左右</p>
<hr />
<div class="fun">
<div><span>计次:</span><span id="num">0</span></div>
<div><span>提示:</span><span id="error"></span></div>
<div><span>功能:</span><button id="reset">重开</button></div>
</div>
<hr />
<table border="1">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td class="current"></td>
</tr>
</table>
</div>
<script>
const step = document.getElementById('num');
const error = document.getElementById('error');
const seed = [1, 2, 3, 4, 5, 6, 7, 8];
// 随机数组
const shuffle = (array) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
// 检查结果
const check = () => {
let flag = true;
const tds = document.querySelectorAll('td');
for (let i = 0; i < tds.length - 1; i++) {
let td = tds[i];
if (i + 1 !== parseInt(td.innerText)) {
flag = false;
}
}
if (flag) {
error.innerText = '恭喜你通关啦!';
}
}
// 更新 td 数据
const init = () => {
const data = shuffle(seed);
const tds = document.querySelectorAll('td');
for (let i = 0; i < tds.length - 1; i++) {
let td = tds[i];
td.innerText = data[i];
td.className = ''
}
error.innerText = '';
step.innerText = 0;
tds[tds.length - 1].className = 'current'
tds[tds.length - 1].innerText = ''
}
init()
document.getElementById('reset').addEventListener('click',()=>{
init();
});
// 监听点击事件,移动方块处理
document.querySelector('table').addEventListener('click', (event) => {
const target = event.target;
const current = document.querySelector('.current'); const {
x: cx,
y: cy
} = current.getBoundingClientRect();
const {
x: tx,
y: ty
} = target.getBoundingClientRect();
const w = Math.abs(cx - tx);
const h = Math.abs(cy - ty);
if ((cx === tx || ty === cy) && (w < 200 && h < 200)) {
if (target.nodeName === 'TD' && target !== current) {
const innerText = target.innerText;
target.classList = 'current';
target.innerText = '';
// 当前空白块
current.innerText = innerText
current.classList.remove('current');
// 更新步骤
let num = step.innerText || 0;
num++;
step.innerText = num;
error.innerText = '';
check();
}
} else {
error.innerText = '不能这样哦';
}
})
</script>
</body>
</html>

[数字华容道] Html+css+js 实现小游戏的更多相关文章

  1. 使用html+css+js实现弹球游戏

    使用html+css+js实现弹球游戏 效果图: 代码如下,复制即可使用: <!doctype html> <head> <style type="text/c ...

  2. pixi.js 微信小游戏 入手

    pixi是什么?一款h5游戏引擎 优点:简单简洁性能第一 缺点:大多数用的国产三大引擎,pixi资料少,工具少, 为什么学,装逼 用pixi开发小游戏行吗? 行.但要简单处理下 下载官网上的 weap ...

  3. JS写小游戏(一):游戏框架

    前言 前一阵发现一个不错的网站,都是一些用html5+css+js写的小游戏,于是打算学习一番,写下这个系列博客主要是为了加深理解,当然也有一些个人感悟,如果英文好可以直接Click Here. 概述 ...

  4. 例子:js超级玛丽小游戏

    韩顺平_轻松搞定网页设计(html+css+javascript)_第34讲_js超级玛丽小游戏_学习笔记_源代码图解_PPT文档整理 采用面向对象思想设计超级马里奥游戏人物(示意图) 怎么用通过按键 ...

  5. three.js 微信小游戏

    最近在 https://classroom.udacity.com/courses/cs291 学习了一些 3D 引擎和 three.js 的知识 把 three.js 弄到微信小游戏里,先随便跑一跑 ...

  6. js消除小游戏(极简版)

    js小游戏极简版 (1) 基础布局 <div class = "box"> <p></p> <div class="div&qu ...

  7. JS扫雷小游戏

    HTML代码 <title> 扫雷 </title> <!-- ondragstart:防拖拽生成新页面 oncontextmenu:屏蔽右键菜单--> <b ...

  8. 用原生js写小游戏--Flappy Bird

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 【JS入门小游戏】01-骰子游戏

    01-骰子游戏 游戏出自Udemy的JS课程中提到的一个游戏,课程主要是对JS部分进行详细的从0开始的讲解,本篇文章是对整个游戏的分析,包括HTMK,CSS和JS,也主要对JS进行刨析. 游戏链接:h ...

  10. 使用JS制作小游戏贪吃蛇

    先看效果图: 过程如下: 1.首先创建一张画布地图<div class="map"> </div>: 2.创建食物的自调用函数 (function (){ ...

随机推荐

  1. elasticsearch映射创建查询 和Spring Data ElasticSearch入门

    Elasticsearch核心概念 Elasticsearch是面向文档(document oriented)的,这意味着它可以存储整个对象或文档(document).然而它不仅 仅是存储,还会索引( ...

  2. openGauss每日一练第四天

    openGauss 每日一练第四天 本文出处:https://www.modb.pro/db/193083 学习地址 https://www.modb.pro/course/133 学习目标 学习 o ...

  3. 选择排序的基本实现【数据结构与算法—TypeScript 实现】

    笔记整理自 coderwhy 『TypeScript 高阶数据结构与算法』课程 概念 本质:两两元素相比较,先扫描一遍未排序数列,把未排序的数列中的最小(大)元素,放到数列的已排序的末尾 特性 选择排 ...

  4. 为你推荐一款高效的IO组件——okio

    原文:https://mp.weixin.qq.com/s/XnNhSq8ESoslb2DQEzMbCQ,点击链接查看更多技术内容.   前不久,三方组件库上新了一批JS/eTS组件,其中就包括oki ...

  5. Node.js 与前端开发实战

    0x1 Node.js 的应用场景 前端工程化 打包工具:webpack.vite.esbuild.parce 代码压缩:uglifyjs 语法转换:babeljs,typescript 难以替代 W ...

  6. centos8 \CentOS 9 Stream \Oracle Linux8\Oracle Linux 9 rpm 安装mysql8.0.28 mysql8.0.34

    centos8 rpm 安装mysql8.0.28 检查 检测系统是否自带安装 MySQL 命令如下: rpm -qa | grep mysql 如果如下存在已安装的包,就需要卸载 mysql80-c ...

  7. Launching Teamviewer remotely through SSH

    Launching Teamviewer remotely through SSH When you need to manage your Server remotely, but you can ...

  8. 重新整理数据结构与算法(c#)——算法套佛洛伊德算法[三十二]

    前言 佛洛伊德算法和迪杰斯特拉算法非常像,但是它求的是任何一个点到其他点之间的距离. 假设有一张图: 转换为矩阵为: 他们的前驱为: 可能上面表述前驱不清楚,举个例子. 看下图: 这第二种图表示,从A ...

  9. MySQL 分析查询与来源机器

    当前分析针对版本:MariaDB 10.5 线上出现报错:can't create more than max_prepared_stmt_count statements.造成这个错误的直接原因就是 ...

  10. CDN应用进阶 | 正确使用CDN 让你更好规避安全风险

    为了帮助用户更好地了解和使用CDN产品,CDN应用实践进阶系统课程开课了.12月17日,阿里云CDN产品专家彭飞在线分享了<正确使用CDN,让你更好规避安全风险>议题,内容主要包括以下几个 ...