1+ window . onload = function ( ) {
2+ //获取内容区里面得#main和#go,以及获取计数区。
3+ var main = document . getElementById ( 'main' ) ;
4+ var go = document . getElementById ( 'go' ) ;
5+ var count = document . getElementById ( 'count' ) ;
6+ //设置四种颜色
7+ cols = [ '#1AAB8A' , '#E15650' , '#121B39' , '#80A84E' ] ;
8+ //动态创建div
9+ function CDiv ( classname ) {
10+ //创建div节点,为一行
11+ var Div = document . createElement ( 'div' )
12+ //生成随机数,Math.floor()是四舍五入的作用,产生的数永远不会大于4,产生的随机数表示那个有颜色的那一个
13+ index = Math . floor ( Math . random ( ) * 4 )
14+ //设置class
15+ Div . className = classname
16+
17+ //在一行里面动态添加四个div,一行里面的四块
18+ for ( var i = 0 ; i < 4 ; i ++ ) {
19+ var iDiv = document . createElement ( 'div' )
20+ Div . appendChild ( iDiv )
21+ }
22+
23+ //判断#main里面是否有元素
24+ if ( main . children . length == 0 ) {
25+ main . appendChild ( Div ) ;
26+ } else {
27+ //如果有元素,则在该元素之前插入
28+ main . insertBefore ( Div , main . children [ 0 ] ) ;
29+ }
30+ //随机的设置四个div块的背景颜色
31+ Div . children [ index ] . style . backgroundColor = cols [ index ] ;
32+ Div . children [ index ] . className = "i" ;
33+ }
34+ function move ( obj ) {
35+ //默认速度与计分
36+ var speed = 5 , num = 0 ;
37+ //定义一个定时器
38+ obj . timer = setInterval ( function ( ) {
39+ //速度
40+ var step = parseInt ( getComputedStyle ( obj , null ) [ 'top' ] ) + speed ;
41+ obj . style . top = step + 'px'
42+ if ( parseInt ( getComputedStyle ( obj , null ) [ 'top' ] ) >= 0 ) {
43+ CDiv ( 'row' ) ;
44+ obj . style . top = - 150 + 'px' ;
45+ }
46+ if ( obj . children . length == 6 ) {
47+ for ( var i = 0 ; i < 4 ; i ++ ) {
48+ if ( obj . children [ obj . children . length - 1 ] . children [ i ] . className == 'i' ) {
49+ //游戏结束
50+ obj . style . top = '-150px' ;
51+ count . innerHTML = '游戏结束,最高得分: ' + num ;
52+ //关闭定时器
53+ clearInterval ( obj . timer ) ;
54+ //显示开始游戏
55+ go . children [ 0 ] . innerHTML = '游戏结束' ;
56+ go . style . display = "block" ;
57+ }
58+ }
59+ obj . removeChild ( obj . children [ obj . children . length - 1 ] ) ;
60+ }
61+ //点击与计分
62+ obj . onmousedown = function ( event ) {
63+ //点击的不是白盒子
64+ // 兼容IE
65+ event = event || window . event ;
66+ if ( ( event . target ? event . target : event . srcElement ) . className == 'i' ) {
67+ //点击后的盒子颜色
68+ ( event . target ? event . target : event . srcElement ) . style . backgroundColor = "#bbb" ;
69+ //清除盒子标记
70+ ( event . target ? event . target : event . srcElement ) . className = '' ;
71+ //计分
72+ num ++ ;
73+ //显示得分
74+ count . innerHTML = '当前得分: ' + num ;
75+ }
76+ else {
77+ //游戏结束
78+ obj . style . top = 0 ;
79+ count . innerHTML = '游戏结束,最高得分: ' + num ;
80+ //关闭定时器
81+ clearInterval ( obj . timer ) ;
82+ //显示开始游戏
83+ go . children [ 0 ] . innerHTML = '游戏结束' ;
84+ go . style . display = "block" ;
85+ }
86+ //盒子加速
87+ if ( num % 10 == 0 ) {
88+ speed ++ ;
89+ }
90+ }
91+ //松开触发停止
92+ obj . onmouseup = function ( event ) {
93+ }
94+ } , 20 )
95+ }
96+ go . children [ 0 ] . onclick = function ( ) {
97+ if ( main . children . length ) {
98+ //暴力清除main里面所有盒子
99+ main . innerHTML = '' ;
100+ }
101+ //清空计分
102+ count . innerHTML = '游戏开始' ;
103+ //隐藏开始盒子
104+ this . parentNode . style . display = "none" ;
105+ move ( main ) ;
106+ }
107+ }
0 commit comments