js小遊戲——看你能堅持多久

來源:互聯網
上載者:User

標籤:des   style   blog   http   java   color   

點擊另存用到的圖片。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn"  ><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /><title>看你能堅持多久</title><style type="text/css">#top{color:white;height:20px;}#time{float:left;}#item{float:left;border:solid 1px gray;width:100px;padding:0;margin:0 20px;}#item div{background:yellow;width:0px;} input{margin:0 10px;float:left;}#desc{float:left}body{background:black}</style><script type="text/javascript">window.onload = init;function init(){time = 0;timeDiv = document.getElementById(‘time‘);input = document.getElementsByTagName(‘input‘)[0];body = document.getElementById(‘body‘);input.addEventListener(‘click‘,wenzi);div = document.getElementById(‘item‘).firstChild;width = screen.width;}function action(){var xx = document.createElement(‘img‘);xx.src = ‘./xingxing.gif‘;xx.style.width = random(10,100)+‘px‘;xx.style.position = ‘absolute‘;xx.style.left = random(0,width-100)+‘px‘; xx.style.top = random(20,600)+‘px‘;xx.onclick = del;body.appendChild(xx);time++;setTime();setWidth();isGameOver();}function setTime(){var timeText = document.createTextNode(‘時間:‘+Math.floor(time*0.5));timeDiv.replaceChild(timeText,timeDiv.firstChild);}function setWidth(){div.style.width = num()*5+‘px‘;var numText = document.createTextNode(num());div.firstChild.replaceChild(numText,div.firstChild.firstChild); }function isGameOver(){if(num()>=20){clearInterval(clock);for(var i=0;i<document.images.length;i++){(document.images)[i].onclick = ‘‘;}input.value="重新開始"alert(‘遊戲結束,您堅持了‘+Math.floor(time*0.5)+‘秒‘);}}function random(x,y){return Math.floor( Math.random() * (y-x+1) ) + x}function num(){return document.images.length;}function del(){body.removeChild(this);setWidth();}function wenzi(){if(input.value==‘開始‘){input.value = ‘暫停‘;begin();for(var i=0;i<document.images.length;i++){document.images[i].onclick = del;}}else if(input.value==‘暫停‘){input.value = ‘開始‘;clearInterval(clock);for(var i=0;i<document.images.length;i++){(document.images)[i].onclick = ‘‘;}}else if(input.value==‘重新開始‘){input.value = ‘暫停‘;while(body.hasChildNodes()) //當div下還存在子節點時 迴圈繼續{body.removeChild(body.firstChild);}time = 0;timeDiv.innerHTML=‘時間:0‘;begin();}}function begin(){clock = setInterval(‘action()‘,500);}</script></head><body><div id="top"><input type="button" value="開始"/><div id="time">時間:0</div><div id="item"><div><font color="red">0</font></div></div><div id="desc"><b>遊戲說明:</b>點擊消除星星,當星星數量達到20個時遊戲結束。看你能堅持多久?</div></div><div id="body"></div></body></html>

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.