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
| <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; -webkit-user-select: none; } html, body{ width: 100%; height: 100%; overflow: hidden; } .jin{ position: absolute; top: -70px; width: 70px; height: 70px; } #pen{ width: 103px; background: url(img/pen.png); height: 48px; position: absolute; left: 0; bottom: 0; } #score{ width: 100px; height: 100px; color: gold; font-size: 80px; font-weight: bold; position: absolute; left: 40px; top: 40px; } </style> </head> <body> <div id="score">0</div> <div id="pen"></div> </body> </html> <script type="text/javascript"> var count = 0;
pen.onmousedown = function(){ var x = event.offsetX; document.onmousemove = function(){ pen.style.left = event.clientX - x + "px"; } } pen.onmouseup = function(){ document.onmousemove = null; } document.onmouseup = function(){ document.onmousemove = null; } function createJin(){ var jinbi = document.createElement("img"); jinbi.className = "jin"; jinbi.src = "img/jin.png"; document.body.appendChild(jinbi); jinbi.style.left = Math.floor(Math.random() * (document.body.clientWidth - jinbi.offsetWidth)) + "px"; setInterval(function(){ jinbi.style.top = jinbi.offsetTop + 2 + "px"; if(jinbi.offsetTop > document.body.clientHeight - jinbi.offsetHeight){ document.body.removeChild(jinbi); } if(crashJin(pen, jinbi)){ count++; score.innerHTML = count; document.body.removeChild(jinbi); } },10); } setInterval(createJin, 800); function crashJin(obj1, obj2){ var xLeft = obj1.offsetLeft - obj2.offsetWidth; var xRight = obj1.offsetLeft + obj1.offsetWidth; var yTop = obj1.offsetTop - obj2.offsetHeight; if(obj2.offsetLeft >= xLeft && obj2.offsetLeft <= xRight && obj2.offsetTop >= yTop){ return true; }else{ return false; } } </script>
|