<!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>Document</title>
<style>
#cont{
width: 400px;
height: 420px;
position: relative;
/* border: 1px solid red; */
overflow: hidden;
}
#main{
position: relative;
top: -100px;
}
.row{
width: 400px;
height: 100px;
}
.row div{
width: 98px;
height: 98px;
border: 1px solid gray;
}
.black{
background: black;
}
</style>
</head>
<body>
<div id="cont">
<div id="main">
</div>
</div>
<hr>
<h3 id="f">0</h3>
</body>
<script>
var main =document.getElementById('main');
//div 工厂
function cdiv(className){
var div = document.createElement('div');
if(className){
div.setAttribute('class',className);
}
return div;
}
function crow(){
var row = cdiv('row');//;创建行 div
var index=Math.floor(Math.random()*4);
//创建小div
for(var i=0;i<4;i++){
if (i==index) {
//创建黑块
row.appendChild(cdiv('black'));
}else{
//创建普通小块
row.appendChild(cdiv());
}
}
if (main.firstChild) {
main.insertBefore(row,firstChild);
}else{
main.appendChild(row);
}
}
//初始化函数
function init(){
//循环调用crow创建游戏界面
for(var i= 0;i<4;i++){
crow();
}
var sta = true;
var sp =2;
main.onclick=function (ev){
var dj =ev.target;
//如果点击白块,停止计时器
if(sta ==false){
clearInterval(clock);
sta=false;
alert('洗洗睡吧');
}else if(dj.className==''){
clearInterval(clock);
sta= false;
alert('洗洗睡吧');
}else{
dj.className='';
dj.parentNode.pass=true;
//得分
var f = document.getElementById('f');
var fs= parseInt(f.innerHTML)+1;
f.innerHTML=fs;
//根据分值实现加速
if (fs%5==0) {
sp+=2;
}
}
}
//设置定时器,反复调用move函数
clock = setInterval('move()',50);
}
function move(){
var t=parseInt(getComputedStyle(main).top);
top +=sp;
main.style.top= top+'px';
if(top>=0){
crow();
main.style.top='-100px';
//判断用户没有点击停止运行
if(main.lastChild.pass== undefined){
clearInterval(clock);
sta=false;
alert('洗洗睡吧');
}
if(main.children.length>5){
main.removeChild(main.lastChild);
}
}
}
init();
</script>
</html>