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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
var startBtn = document.querySelector("#menu img");
var headEl = document.querySelector("#head");
var menuEl = document.querySelector("#menu");
var endEl = document.querySelector("#end");
var pipeUl = document.querySelector("#pipes");
var birdImg = document.querySelector("#bird");
var currentScoreEL = document.querySelector("#currentScore");
var bestScoreEl = document.querySelector("#bestScore");
var scoreEL = document.querySelector("#score");
var bulletMusic = document.querySelector("#bullet");
var gameMusic = document.querySelector("#gameMusic");
var gameOverMusic = document.querySelector("#gameOver");
var rootEl = document.querySelector("#root");
var num = 0;
var speed = 0;
var birdDownTimer;
var birdUpTimer;
function randomNum(min, max){ return Math.floor(Math.random() * (max - min + 1) + min); }
startBtn.onclick = function(){ event.stopPropagation();
gameMusic.loop = true; gameMusic.play(); headEl.style.display = "none"; menuEl.style.display = "none"; scoreEL.style.display = "block"; birdImg.style.display = "block"; setInterval(createPipe, 3000); birdDownTimer = setInterval(birdDown, 20); rootEl.onclick = clickRoot;
setInterval(function(){ var liArray = document.querySelectorAll("li"); for(var i = 0; i < liArray.length; i++){ crash(birdImg, liArray[i].firstElementChild); crash(birdImg, liArray[i].lastElementChild); } }, 10) }
function createPipe(){ var li = document.createElement("li"); li.className = "pipe"; pipeUl.appendChild(li); var passHeight = 130; var upHeight = randomNum(80, 200); var downHeight = li.clientHeight - passHeight - upHeight; var upDiv = document.createElement("div"); upDiv.className = "upPipe"; upDiv.style.height = upHeight + "px"; li.appendChild(upDiv); var downDiv = document.createElement("div"); downDiv.className = "downPipe"; downDiv.style.height = downHeight + "px"; li.appendChild(downDiv); console.log("上管道", upDiv.offsetLeft + "; 下管道" + downDiv.offsetLeft); console.log("管道父级li" + li.offsetLeft); var x = 343; var pipeMoveTimer = setInterval(function(){ x--; li.style.left = x + "px"; if(x <= -li.clientWidth){ clearInterval(pipeMoveTimer); li.remove(); } if(x == 0){ changeScore(); } }, 10); }
function changeScore(){ num++; scoreEL.innerHTML = ""; if(num < 10){ var img = document.createElement("img"); img.src = "img/" + num + ".jpg"; scoreEL.appendChild(img); }else{ var shiImg = document.createElement("img"); var shi = Math.floor(num / 10); shiImg.src = "img/" + shi + ".jpg"; scoreEL.appendChild(shiImg); var geImg = document.createElement("img"); var ge = num % 10; geImg.src = "img/" + ge + ".jpg"; scoreEL.appendChild(geImg); } }
function birdDown(){ birdImg.src = "img/down_bird.png"; speed += 0.5; if(speed >= 10){ speed = 10; }
birdImg.style.top = birdImg.offsetTop + speed + "px"; var maxTop = 423 - birdImg.clientHeight; if(birdImg.offsetTop >= maxTop){ gameOver(); } }
function gameOver(){ gameMusic.pause(); gameOverMusic.play(); var lastTimer = setInterval(function(){}, 10); for(var i = 1; i <= lastTimer; i++){ clearInterval(i); } endEl.style.display = "block"; endEl.style.zIndex = 10; currentScoreEL.innerHTML = num; rootEl.onclick = null; }
function clickRoot(){ bulletMusic.play(); clearInterval(birdDownTimer); clearInterval(birdUpTimer); speed = 10; birdUpTimer = setInterval(birdUp, 20); }
function birdUp(){ birdImg.src = "img/up_bird.png"; speed -= 0.5; if(speed < 0){ speed = 0; clearInterval(birdUpTimer); birdDownTimer = setInterval(birdDown, 20); } birdImg.style.top = birdImg.offsetTop - speed + "px"; if(birdImg.offsetTop <= 0){ gameOver(); } }
function crash(div1, div2){ var xLeft = div2.offsetParent.offsetLeft - div1.offsetWidth; var xRight = div2.offsetParent.offsetLeft + div2.offsetWidth; var yTop = div2.offsetTop - div1.offsetHeight; var yBottom = div2.offsetTop + div2.offsetHeight; if(div1.offsetLeft >= xLeft && div1.offsetLeft <= xRight && div1.offsetTop >= yTop && div1.offsetTop <= yBottom){ gameOver(); } }
|