JaveScript 之开关灯游戏
标签:
JavaScript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
var lightRow = 10; //行
var lightCol = 10; //列
var lightWidth = 50;
var lightHeight = 50;
var itemMargin = 3;
var lightBorder = 1;
var beforeBG = "black";
var afterBG = "yellow";
var shaDow1 = "5px 5px 5px rgb(50,50,50)";
var shaDow2 = "5px 5px greenyellow";
var body_ = document.getElementsByTagName("body");
var annui = document.createElement("button");
annui.innerHTML = "开始游戏";
annui.onclick = jj;
body_[0].appendChild(annui);
var mainDiv = document.createElement("div");
mainDiv.style.width = lightCol * lightWidth + lightBorder * lightCol * 2 + itemMargin * lightCol * 2 + "px";
mainDiv.style.margin = "0 auto";
body_[0].appendChild(mainDiv);
var lights = [];
for(var i = 0; i < lightRow * lightCol; i++) {
var lightItem = document.createElement("div");
lightItem.style.width = lightWidth + "px";
lightItem.style.height = lightHeight + "px";
lightItem.style.background = beforeBG;
lightItem.style.margin = itemMargin + "px";
lightItem.style.border = lightBorder + "px solid black";
lightItem.style.borderRadius = "0 20%";
lightItem.style.boxShadow = shaDow1;
lightItem.style.display = "inline-block";
lightItem.innerHTML = i;
lightItem.onclick = ff;
lightItem.onOroff = false;
lightItem.index = i;
lights.push(lightItem);
mainDiv.appendChild(lightItem);
}
var ny = 5;
var flag = 0;
function clickItem(a) {
onOroff(a);
if(a.index >= 10) {
onOroff(lights[a.index - 10]);
}
if(a.index <= 90) {
onOroff(lights[a.index + 10]);
}
if(a.index % 10 != 0) {
onOroff(lights[a.index - 1]);
}
if(a.index % 10 != 9) {
onOroff(lights[a.index + 1]);
}
}
function onOroff(item) {
if(item.onOroff == false) {
item.style.background = afterBG;
item.style.boxShadow = shaDow2;
item.onOroff = true;
flag++;
} else {
item.style.background = beforeBG;
item.style.boxShadow = shaDow1;
item.onOroff = false;
flag--;
}
}
function jj() {
for(var i = 0; i < ny; i++) {
var b = parseInt(Math.random() * 99);
clickItem(lights[b]);
console.log(b);
}
}
function ff() {
clickItem(this);
if(flag == 0) {
alert("恭喜过关");
ny += 5;
jj();
} else {
}
}
</script>
</html>
点击查看更多内容
5人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦