找回密码

碧海潮声大学生网

查看: 870|回复: 3
打印 上一主题 下一主题

〖分享〗

[复制链接]
跳转到指定楼层
1#
发表于 2005-12-19 02:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一段JS的贪吃蛇游戏~~
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  4. <title> 贪吃蛇 </title>
  5. <style>
  6. #mapbox {
  7.   font-family: 宋体; font-size: 17px; line-height:100%;
  8.   height:360; width:430;
  9. }
  10. </style>
  11. <script language="JavaScript">
  12.     /////////////////////
  13.    //  贪吃蛇      //
  14.   //  OO+TextBox   //
  15.   //  程序作者:海浪  //
  16. //    2005-8-15  //
  17. /////////////////////
  18. var map;
  19. var gott;
  20. function play()
  21. {
  22.   clearInterval(gott);
  23.   map = new mapClass(25,20);
  24.   document.onkeydown = keydown;
  25.   gott = setInterval("map.snake.go()",150);
  26. }
  27. function keydown()
  28. {
  29.   switch (event.keyCode)
  30.   {
  31.   case 38: map.snake.refx(0); break;
  32.   case 39: map.snake.refx(1); break;
  33.   case 40: map.snake.refx(2); break;
  34.   case 37: map.snake.refx(3); break;
  35.   }
  36. }
  37. ////////////////////////////////////////////
  38. function mapClass(x,y)
  39. {
  40.   this.x = x;
  41.   this.y = y;
  42.   this.sq = "■";
  43.   this.sk = " ";
  44.   this.marr = [];
  45.   
  46.   for(var iy=0; iy<this.y; iy++)
  47.   {
  48.    this.marr[iy] = [];
  49.    for(var ix=0; ix<this.x; ix++)
  50.     this.marr[iy][ix] = (ix==0||iy==0||ix==this.x-1||iy==this.y-1)?this.sq:this.sk;
  51.   }
  52.   this.snake = new snakeClass(this);
  53.   this.bean = new beanClass(this);
  54.   this.bean.newbean();
  55.   this.bean.addn();
  56. }
  57. function mapClass.prototype.write()
  58. {
  59.   var str = "";
  60.   for(var ii=0; ii<this.y; ii++)
  61.    str += this.marr[ii].join("")+"\n";
  62.   mapbox.innerText = str;
  63. }
  64. function mapClass.prototype.rexy(x,y,s)
  65. {
  66.   var str = s || this.sk;
  67.   this.marr[y][x] = str;
  68. }
  69. function mapClass.prototype.jcxy(x,y,s)
  70. {
  71.   var str = s || this.sk;
  72.   return this.marr[y][x] == str;
  73. }
  74. ////////////////////////////////////////////
  75. function snakeClass(po)
  76. {
  77.   this.parent = po;
  78.   this.mt = "●";
  79.   this.boarr = [];
  80.   this.bolength = 4;
  81.   this.fang = 1;
  82.   this.fangtt = 1;
  83.   this.boarr[0] = this.addbody(3,5);
  84.   this.parent.rexy(this.boarr[0].x,this.boarr[0].y,this.mt);
  85. }
  86. function snakeClass.prototype.go()
  87. {
  88.   this.fang = this.fangtt;
  89.   var x = this.boarr[0].x + [0,1,0,-1][this.fang];
  90.   var y = this.boarr[0].y + [-1,0,1,0][this.fang];
  91.   var chi = this.parent.jcxy(x,y,this.parent.bean.mt);
  92.   if(!this.parent.jcxy(x,y) && !chi)
  93.   {
  94.    clearInterval(gott);
  95.    alert("Game Over!");
  96.    return;
  97.   }
  98.   if(chi)
  99.   {
  100.    this.bolength++;
  101.    this.parent.bean.addn();
  102.    this.parent.bean.newbean();
  103.   }
  104.   this.parent.rexy(x,y,this.mt);
  105.   this.boarr = [].concat(this.addbody(x,y),this.boarr);
  106.   if(this.boarr.length>this.bolength)
  107.   {
  108.    var tb = this.boarr.pop();
  109.    this.parent.rexy(tb.x,tb.y);
  110.   }
  111.   this.parent.write();
  112. }
  113. function snakeClass.prototype.addbody(x,y)
  114. {
  115.   return { x:x, y:y };
  116. }
  117. function snakeClass.prototype.refx(n)
  118. {
  119.   if(Math.abs(this.fang-n)!=2)
  120.    this.fangtt=n;
  121. }
  122. ////////////////////////////////////////////
  123. function beanClass(po)
  124. {
  125.   this.parent = po;
  126.   this.mt = "◎";
  127.   this.sne = -100;
  128. }
  129. function beanClass.prototype.addn()
  130. {
  131.   this.sne+=100;
  132.   fan.innerText = this.sne;
  133. }
  134. function beanClass.prototype.newbean()
  135. {
  136.   var x = Math.floor(Math.random()*(this.parent.x-2))+1;
  137.   var y = Math.floor(Math.random()*(this.parent.y-2))+1;
  138.   if(this.parent.jcxy(x,y))
  139.    this.parent.rexy(x,y,this.mt);
  140.   else
  141.    this.newbean();
  142. }
  143. </script>
  144. </head>
  145. <body>
  146. <center>
  147. <h2>贪吃蛇</h2><hr>
  148. <div id="mapbox"></div>
  149. <input type="button" value="开始" onclick="play()"> 得分:<span id="fan"></span>
  150. <hr>
  151. 程序作者:海浪 2005-8-15
  152. </center>
  153. </body>
  154. </html>
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩
2#
 楼主| 发表于 2005-12-19 02:36 | 只看该作者
真是强
3#
发表于 2005-12-19 12:10 | 只看该作者
还不错,如果把符号用图片替换,在对速度调整改进改进就是个比较成熟的游戏了 [s:1]
4#
发表于 2005-12-19 13:54 | 只看该作者
确实不错
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋| 碧海潮声大学生网  

Copyright © 2001-2013 Comsenz Inc.   All Rights Reserved.

Powered by Discuz! X3.2( 浙ICP备11026473号 )

快速回复 返回顶部 返回列表