7.29关灯游戏,用script实现
2024-10-21 11:26:57
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>关灯js版</title>
</head>
<body>
<script>
let rowCount = 10;
let colCount = 10;
let onColor = 'yellow';
let offColor = 'black';
let lightCount = 0;
let level = 1;
let lightWidth = 50;
let lightHeight = lightWidth;
let lightList = [];
//游戏初始化
function init(){
let wap = document.createElement('div');
wap.style.width = lightWidth * colCount + 'px';
wap.style.height = lightWidth * rowCount +'px';
wap.style.border = '1px solid gray';
wap.style.margin = 'auto';
wap.classList.add('clear');
let clearStyle = document.createElement('style');
clearStyle.innerText = '.clear:after {content :""; display: block; clear: both;}'
for(i = 0 ; i < 100 ; i++){
let light = document.createElement('div');
light.style.width = lightWidth + 'px';
light.style.height = lightHeight +'px';
light.style.border ='1px solid lightgray';
light.style.boxSizing = 'border-box';
light.style.backgroundColor = offColor;
light.style.float = 'left';
light.id = i;
light.onclick = myClick;
//push 从数组后面添加元素
lightList.push(light);
wap.appendChild(light);
}
document.body.appendChild(clearStyle);
document.body.appendChild(wap);
}
//游戏逻辑
function myClick(){
findLights(this);
}
function findLights(light){
turnLight(light);
//获取灯的下标
let index = parseInt(light.id);
let row = parseInt(index / colCount);
let upIndex = index - colCount;
if(upIndex >= 0){
turnLight(lightList[upIndex]);
}
let downIndex = index + colCount;
if(downIndex < lightList.length){
turnLight(lightList[downIndex]);
}
let leftIndex = index - 1;
let leftRow = parseInt(leftIndex / colCount);
if(leftIndex >= 0 && leftRow == row){
turnLight(lightList[leftIndex]);
}
let rightIndex = index + 1;
let rightRow = parseInt(rightIndex / colCount);
if(rightRow == row){
turnLight(lightList[rightIndex]);
}
//获胜判断
if(lightCount == 0){
alert('u win');
run();
}
}
function turnLight(light){
if(light.style.backgroundColor == onColor){
light.style.backgroundColor = offColor;
lightCount--;
}else{
light.style.backgroundColor = onColor;
lightCount++;
}
}
function next(level){
for(let i = 0 ; i < level; i++){
let randomIndex = parseInt(Math.random() * lightList.length);
findLights(lightList[randomIndex]);
}
}
function run(){
next(level++);
}
init();
run();
</script>
</body>
</html>
最新文章
- react入门参考资料--step by step
- C# 记录错误日志
- 【转】8G内存下MySQL的优化详细方案
- 菜单栏被flex页面遮挡解决办法
- 最小的k个数
- linux 流量监控
- Android上传文件之FTP
- TCP/IP笔记 三.运输层(4)——TCP链接管理与TCP状态机
- 多层TreeWidget可选实现
- c#调用语音功能
- int有符号和无符号类型内存 -- C
- MongoDB学习笔记(三)
- codeforces 792A-D
- 关于button去掉自带阴影效果的方法
- 杂记-python
- 使用CMake生成解决方案后构建INSTALL报错
- MySQL数据库事务各隔离级别加锁情况--read committed &;&; MVCC
- git push fatal: The remote end hung up unexpectedly
- 6_linux用户及权限(1)
- Keepalive+nginx实现高可用负载均衡方案
热门文章
- 第九周总结-MySQL、前端
- Gateway服务网关 (入门到使用)
- spring cloud12-spring cloud sleuth
- 使用 shell 脚本拼接 srt 字幕文件 (srtcat)
- gitlabApi如何获取项目文件夹的commitId
- axios实现跨域的问题 vue实现跨域
- STL中的智能指针(Smart Pointer)及其源码剖析: std::auto_ptr
- 题解 [ZJOI2010]排列计数
- pytorch学习笔记三之神经网络
- LeetCode-2049 统计最高分的结点数