1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| var zhuti = document.getElementsByClassName('zhuti')[0];
var jifen = document.getElementsByClassName('jifen')[0];
var start = document.getElementsByClassName('start')[0];
var zhezhaoceng = document.getElementsByClassName('zhezhaoceng')[0]; function addbox(classname) {
var index = Math.floor(Math.random() * 4) var bigdiv = document.createElement('div'); bigdiv.className = classname; for (var i = 0; i < 4; i++) { var smallbox = document.createElement('div') bigdiv.appendChild(smallbox) } if (zhuti.children.length == 0) { zhuti.appendChild(bigdiv) } else { zhuti.insertBefore(bigdiv, zhuti.children[0]) } bigdiv.children[index].style.backgroundColor = 'black'; bigdiv.children[index].className = 'random_box'; } function move(obj) { var num = 0; var sudu = 5; obj.timeID = setInterval(function () { var nowtop = parseInt(getComputedStyle(obj)['top']) + sudu; obj.style.top = nowtop + 'px'; if (parseInt(getComputedStyle(obj)['top']) >= 0){ addbox('row') obj.style.top = -150 + 'px' } if (obj.children.length == 6) { for (var i = 0; i < 4; i++) { if (obj.children[obj.children.length - 1].children[i].className == 'random_box') { jifen.innerHTML = '当前最高得分:' + num; start.style.display = 'block' start.innerHTML = '小朋友, 游戏结束'; start.style.display = 'block'; start.style.backgroundColor = 'green'; start.style.height = 60 + 'px'; start.style.fontSize = 30 + 'px'; zhezhaoceng.style.display = 'block'; clearInterval(obj.timeID) } } obj.removeChild(obj.children[obj.children.length - 1]) } obj.onmousedown = function (event) { if (event.target.className == 'random_box') { event.target.style.backgroundColor = 'red'; event.target.className = ''; num++; jifen.innerHTML = '当前得分' + num; } else { clearInterval(obj.timeID) start.style.display = 'block'; start.style.backgroundColor = 'green'; start.style.height = 60 + 'px'; start.style.fontSize = 30 + 'px'; start.innerHTML = '游戏结束!再来一局'; jifen.innerHTML = '最终得分' + num; zhezhaoceng.style.display = 'block'; } if (num % 5 == 0) { sudu++; } } }, 20) } start.onclick = function () { if (zhuti.children.length != 0) { zhuti.innerHTML = '' } zhezhaoceng.style.display = 'none'; this.style.display = 'none'; jifen.innerHTML = '当前得分:0' move(zhuti) }
|