console
function Index() {
this.score = 0;
this.lastScore = 0;
this.speed = 2;
this.color = ['#cc66cc','#99ff66','#66ffcc','#FF7F24'];
this.dom = {
title : $('.title'),
main : $('.main'),
author : $('.Author')
};
this.bindEvent();
this.timer1 = {};
this.timer2 = {};
}
Index.prototype.bindEvent = function () {
var self = this;
var topValue = -150;
self.dom.title.on('click',function () {
self.dom.title.css('display','none');
self.dom.author.hide();
self.creatBlock(0);
self.timer1 = setInterval(function () {
var main = self.dom.main;
topValue += self.speed;
if(main.children().length >= 6){
for(var i = 0; i < 4; i++){
if(main.children().eq(5).children().eq(i).attr('class') == 'target'){
alert('game over 你的得分为' + self.score);
clearInterval(self.timer1);
clearInterval(self.timer2);
return;
}
}
main.children().eq(5).remove();
}
if(parseInt(main.css('top')) > 0){
main.css('top','-150px');
topValue = -150;
self.creatBlock(1);
return;
}
main.css({
'top' : topValue + 'px',
})
},10)
self.timer2 = setInterval(function () {
var DisScore = self.score - self.lastScore;
if(DisScore == 10){
self.speed++;
self.lastScore = self.score;
DisScore = 0;
}
} ,200)
});
};
Index.prototype.creatBlock = function (IO) {
var self = this;
var main = self.dom.main;
var row = $('<div class = "row"></div>');
if(IO == 0){
main.append(row);
main.css('display','block');
}else if(IO == 1){
main.prepend(row);
}
for(var i = 0; i < 4; i++){
var column = $('<div class = "col"></div>');
row.append(column);
column.on('click',function () {
$this = $(this);
if($this.attr('class') == 'target'){
$this.css('backgroundColor','#eee').attr('class','col');
self.score++;
return;
}
alert('game over 你的得分为' + self.score);
clearInterval(self.timer1);
clearInterval(self.timer2);
})
}
var index = parseInt(Math.random() * 4);
$(row.children()[index]).css('backgroundColor',self.color[index]);
$(row.children()[index]).attr('class', 'target');
}
new Index();
<html>
<head>
<title>Donot</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="index.css">
</head>
<body>
<div class="wrapper">
<div class="title">Game Start</div>
<div class="main"></div>
<div class="Author">作者: 高闻灿</div>
</div>
</body>
<script src = "jquery.js" type="text/javascript"></script>
<script src = "demo.js"></script>
</html>
*{
margin: 0;
padding: 0;
list-style: none;
}
.wrapper{
width: 400px;
height: 600px;
border: 2px solid black;
margin: 200px auto;
position: relative;
overflow: hidden;
}
.title{
width: 100%;
height: 100px;
border-bottom:5px dashed #eee;
font-size: 50px;
line-height: 100px;
font-weight: bold;
font-style:arial;
text-align: center;
color: cyan;
cursor: pointer;
}
.main{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: -150px;
display: none;
}
.Author{
position:absolute;
width: 100%;
height: 50px;
top: 50%;
left: 0;
margin-top:-25px;
text-align: center;
font-size: 30px;
font-weight: bold;
line-height: 50px;
font-family: Arial;
}
.row{
width: 100%;
height: 149px;
border-bottom: 1px solid black;
}
.col, .target{
width: 99px;
height: 149px;
border-right: 1px solid black;
float: left;
}