歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> HTML5 小游戲:是男人就撐過30秒

HTML5 小游戲:是男人就撐過30秒

日期:2017/3/1 10:35:48   编辑:Linux編程

話不多說HTML5 圖畫板寫的一個小游戲,原形為是男人就撐過30秒。

下載地址:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/1月/30日/HTML5 小游戲:是男人就撐過30秒/

演示地址:http://www.muu.cc/html5/nanren30/index.html

說明:

上下左右控制 ctrl鍵可加速

player是小球

star是怪物

請用支持canvas的浏覽器訪問 推薦用 firefox 或chrome

代碼

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>HTML5游戲:是男人就撐過30秒 || www.88181.com</title>
  6. <script language="javascript" type="text/javascript">
  7. //定義鍵盤值
  8. var KEY = { D: 68, W: 87, A: 65, S:83, RIGHT:39, UP:38, LEFT:37, DOWN:40, QUICK:17};
  9. //定義輸入對象
  10. var input = {
  11. right : false,
  12. up : false,
  13. left : false,
  14. down : false,
  15. quick : false
  16. };
  17. //小球對象
  18. var player = {
  19. speed : 1,
  20. qspeed : 2,
  21. left : 0,
  22. top : 0,
  23. xleft : 0,
  24. dleft : 0,
  25. xtop : 0,
  26. dtop : 0,
  27. r : 5
  28. }
  29. player.init = function(){
  30. thisthis.xleft = this.r;
  31. thisthis.xtop = this.r;
  32. this.dleft = $("myCanvas").width-this.r;
  33. this.dtop = $("myCanvas").height-this.r;
  34. this.left = $("myCanvas").width/2;
  35. this.top = $("myCanvas").height/2;
  36. }
  37. player.getSpeed = function(){
  38. return (input.quick?this.qspeed:this.speed);
  39. }
  40. player.update = function(){
  41. if (input.right) player.left+=player.getSpeed();
  42. if (input.left) playerplayer.left-=player.getSpeed();
  43. if (input.down) player.top+=player.getSpeed();
  44. if (input.up) playerplayer.top-=player.getSpeed();
  45. if (player.left>player.dleft) playerplayer.left=player.dleft;
  46. if (player.left<player.xleft) playerplayer.left=player.xleft;
  47. if (player.top>player.dtop) playerplayer.top=player.dtop;
  48. if (player.top<player.xtop) playerplayer.top=player.xtop;
  49. var c=$("myCanvas");
  50. var ccxt=c.getContext("2d");
  51. cxt.fillStyle="#FF0000";
  52. cxt.beginPath();
  53. cxt.arc(player.left,player.top,player.r,0,Math.PI * 2,true);
  54. cxt.closePath();
  55. cxt.fill();
  56. }
  57. //星星
  58. var star = function(){
  59. this.x = 0;
  60. this.y = 0;
  61. this.r = 5;
  62. this.c = "#00FF00";
  63. this.ax = 0;
  64. this.ay = 0;
  65. this.a = 0;
  66. this.rAngle = 0;
  67. this.speed = 0;
  68. this.isAddX = true;
  69. this.isAddY = true;
  70. this.init = function(){
  71. var lon = ($("myCanvas").width+$("myCanvas").height)*2;
  72. var rlon = Math.floor(Math.random()*(lon+1));
  73. this.rAngle = Math.floor(Math.random()*91);
  74. if(rlon<$("myCanvas").width){//上
  75. this.x = rlon;
  76. this.y = 0;
  77. }else if(rlon<$("myCanvas").width+$("myCanvas").height){//右
  78. this.x = $("myCanvas").width;
  79. this.y = rlon-$("myCanvas").width;
  80. }else if(rlon<$("myCanvas").width*2+$("myCanvas").height){//下
  81. this.x = $("myCanvas").width - (rlon-$("myCanvas").width-$("myCanvas").height);
  82. this.y = $("myCanvas").height;
  83. }else{//左
  84. this.x = 0;
  85. this.y = $("myCanvas").height-(rlon-$("myCanvas").width*2-$("myCanvas").height);
  86. }
  87. if(rlon<$("myCanvas").width/2 || rlon>$("myCanvas").width*2+$("myCanvas").height+$("myCanvas").height/2){//左上
  88. this.isAddX = true;
  89. this.isAddY = true;
  90. }else if(rlon<$("myCanvas").width+$("myCanvas").height/2){//右上
  91. this.isAddX = false;
  92. this.isAddY = true;
  93. }else if(rlon<$("myCanvas").width+$("myCanvas").height+$("myCanvas").width/2){//右下
  94. this.isAddX = false;
  95. this.isAddY = false;
  96. }else{//左下
  97. this.isAddX = true;
  98. this.sAddY = false;
  99. }
  100. this.ax = Math.sin(Math.PI/180*this.rAngle)*star.speed;
  101. thisthis.ax = this.isAddX?this.ax:0-this.ax;
  102. this.ay = Math.cos(Math.PI/180*this.rAngle)*star.speed;
  103. thisthis.ay = this.isAddY?this.ay:0-this.ay;
  104. }
  105. this.update = function(){//更新
  106. thisthis.x=this.x+this.ax;
  107. thisthis.y=this.y+this.ay;
  108. if((this.isAddX && this.x>$("myCanvas").width) || (!this.isAddX && this.x<0) || (this.isAddY && this.y>$("myCanvas").height) || (!this.isAddY && this.y<0)){
  109. this.init();
  110. return;
  111. }
  112. //$("message").innerHTML = $("message").innerHTML+"<br> x="+this.x+";y="+this.y;
  113. //$("message").innerHTML = $("message").innerHTML+"<br>cxt.arc("+Math.round(this.x)+","+Math.round(this.y)+","+this.r+",0,Math.PI * 2,true)";
  114. var c=$("myCanvas");
  115. var ccxt=c.getContext("2d");
  116. cxt.fillStyle=this.c;
  117. cxt.beginPath();
  118. cxt.arc(this.x,this.y,this.r,0,Math.PI * 2,true);
  119. cxt.closePath();
  120. cxt.fill();
  121. }
  122. this.iscollide = function(){//判斷是否被撞到
  123. var x = Math.abs(player.left-this.x);
  124. var y = Math.abs(player.top-this.y);
  125. var R = this.r+player.r;
  126. if(R*R < x*x+y*y){
  127. return true;
  128. }
  129. return false;
  130. }
  131. }
  132. star.speed = 1;
  133. var press = function(event){
  134. var code = event.keyCode || window.event;
  135. switch(code) {
  136. case KEY.RIGHT:
  137. case KEY.D: input.right = true; break;
  138. case KEY.UP:
  139. case KEY.W: input.up = true; break;
  140. case KEY.LEFT:
  141. case KEY.A: input.left = true; break;
  142. case KEY.DOWN:
  143. case KEY.S: input.down = true; break;
  144. case KEY.QUICK: input.quick = true; break;
  145. }
  146. }
  147. var release = function(event) {
  148. var code = event.keyCode || window.event;
  149. switch(code) {
  150. case KEY.RIGHT:
  151. case KEY.D: input.right = false; break;
  152. case KEY.UP:
  153. case KEY.W: input.up = false; break;
  154. case KEY.LEFT:
  155. case KEY.A: input.left = false; break;
  156. case KEY.DOWN:
  157. case KEY.S: input.down = false; break;
  158. case KEY.QUICK: input.quick = false; break;
  159. }
  160. }
  161. var stars = new Array();
  162. var myInter;
  163. var begin;
  164. var time = 0;
  165. //加載事件
  166. var load = function(){
  167. player.init();
  168. for(i=0;i<20;i++){
  169. var s = new star();
  170. s.init();
  171. stars[i] = s;
  172. }
  173. begin = new Date();
  174. myInter = setInterval(function(){update();},20);
  175. }
  176. var $ = function(id){
  177. return document.getElementById(id);
  178. }
  179. //更新方法
  180. var update = function(){
  181. var c=$("myCanvas");
  182. cc.width = c.width; // Clears the canvas
  183. player.update();
  184. for(i=0;i<stars.length;i++){
  185. stars[i].update();
  186. }
  187. updatetime();
  188. isDead();
  189. }
  190. //更新時間
  191. var updatetime = function(){
  192. var end = new Date();
  193. var time = Math.round((end-begin)/1000);
  194. star.speed = Math.round(time/10);
  195. $("time").innerHTML = time;
  196. }
  197. //判斷是否死了
  198. var isDead = function(){
  199. for(i=0;i<stars.length;i++){
  200. var flag = stars[i].iscollide();
  201. if(flag==false){
  202. clearInterval(myInter);
  203. alert("失敗了");
  204. return;
  205. }
  206. }
  207. }
  208. </script>
  209. </head>
  210. <body onLoad="load();" onkeydown="press(event);" onkeyup="release(event);" >
  211. <canvas id="myCanvas" width="400" height="400" style=" border:2px solid #F00; left:30%; position:absolute; ">
  212. <h1>換個好浏覽器吧,ie太垃圾了</h1>
  213. </canvas>
  214. <div id="message">
  215. 兼容 wasd 和 ↑←↓→<br>
  216. ctrl 鍵加速<br>
  217. <span id="time"></span>
  218. </div>
  219. </body>
  220. </html>
Copyright © Linux教程網 All Rights Reserved