当前位置:   article > 正文

植物大战僵尸实训4(暂时完结)_html植物大战僵尸中僵尸行走的代码

html植物大战僵尸中僵尸行走的代码

植物大战僵尸实训4

   这是我的第四部分打僵尸的代码,僵尸吃和僵尸走到植物后还有一些问题,这部分代码和前面3部分和不到一起,要和一起,前三部分要用面向对象,此代码仅供参考。以下是实现效果(为展示效果,我把僵尸血量改成5):

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>植物大战僵尸Javascript版 -- 植物战僵尸</title>
  6. <style type="text/css">
  7. *{margin:0px;padding:0px;font-family: 宋体; font-size: 12px}
  8. .WindowFrame{position:absolute;width:900px;height:600px;overflow: hidden;border:3px outset/*边框突出*/ ;}
  9. #tGround {position:absolute;width:1400px;height:600px;visibility:visible;z-index:0;background:url('images/interface/background1unsodded.jpg') -115px 0px no-repeat;}
  10. #sod{position: absolute; height: 117px; width: 770px; top: 280px; left: 132px; z-index: 1; background-image: url( ./images/interface/sod1row.png); background-position: 0px 0px; background-repeat: no-repeat;}
  11. #dPlants,#dZombies{position:absolute;left:0;top:0;z-index:2}
  12. #dPlants div{position: absolute; top: 294px; z-index: 9999;}
  13. #dZombies div{position: absolute; top: 226px; z-index:22;}
  14. #dPlants img{position: absolute;}
  15. </style>
  16. <script type="text/javascript">
  17. window.onload = function(){
  18. var dPlants=document.getElementById("dPlants");
  19. var dAll=document.getElementById("dAll");
  20. var dZombies=document.getElementById("dZombies");
  21. function Plant(){
  22. this.zw=this.init();//初始化,this的是指这个Plant这个对象,this不能用在嵌套里,采用赋给别的量来实现
  23. this.blood=4;//植物4滴血
  24. }
  25. Plant.prototype.init=function(){//植物的初始化
  26. var zw=document.createElement("div");
  27. zw.innerHTML='<img src="images/interface/plantshadow32.png" style="left:-12px;top:49px;">' +
  28. '<img src="images/plant/Peashooter.gif">';
  29. return zw;
  30. }
  31. Plant.prototype.doPlant=function(top,left){
  32. this.zw.style.left=(left||200)+"px";
  33. this.zw.style.top=(top||294)+"px";
  34. dPlants.appendChild(this.zw);
  35. }
  36. Plant.prototype.attackMode=function(){//定义攻击方式
  37. //如何实现发生豌豆子弹
  38. var pd=document.createElement("img");
  39. pd.src='images/plant/PB00.gif';
  40. pd.style.position="absolute";
  41. pd.style.zIndex='888';
  42. pd.style.left=(this.zw.offsetLeft+30)+"px";
  43. pd.style.top=(this.zw.offsetTop+3)+"px";
  44. dZombies.appendChild(pd);
  45. return pd;
  46. }
  47. Plant.prototype.attack=function(zombie){//攻击
  48. var This=this;
  49. this.attackTimer=setInterval(function(){
  50. var pd=This.attackMode();
  51. var pdTimer=setInterval(function(){
  52. pd.style.left=(pd.offsetLeft+11)+"px";
  53. if(pd.offsetLeft>=dAll.offsetWidth){
  54. clearInterval(pdTimer);
  55. pdTimer=null;
  56. dZombies.removeChild(pd);
  57. }else if(pd.offsetLeft>=zombie.js.offsetLeft+40){
  58. clearInterval(pdTimer);
  59. pdTimer=null;
  60. pd.src="images/plant/PeaBulletHit.gif";//替换图片,实现爆炸
  61. setTimeout(function(){
  62. dZombies.removeChild(pd);
  63. },300);
  64. zombie.blood--;
  65. if(This.zw.left>=zombie.js.offsetLeft+40&&This.blood!=0&&zombie.blood!=0){
  66. This.blood--;
  67. if(This.blood==0){
  68. This.die();
  69. }else{
  70. if(zombie.blood>2){
  71. zombie.eat();
  72. }else if(zombie.blood==2){
  73. zombie.lossHead();
  74. }else if(zombie.blood==1){
  75. zombie.noHeadEat();
  76. }else if(zombie.blood==0){
  77. zombie.die();
  78. This.stop();
  79. }
  80. }
  81. }else{
  82. if(zombie.blood>2){
  83. zombie.walk();
  84. }else if(zombie.blood==2){
  85. zombie.lossHead();
  86. zombie.lossHeadWalk();
  87. }else if(zombie.blood==1){
  88. zombie.lossHeadWalk();
  89. }else if(zombie.blood==0){
  90. zombie.die();
  91. This.stop();
  92. }
  93. }
  94. }
  95. },80);
  96. },2000);
  97. }
  98. Plant.prototype.stop=function(){//停止攻击
  99. clearInterval(this.attackTimer);
  100. this.attackTimer=null;
  101. }
  102. Plant.prototype.die=function(){ //死亡
  103. this.stop();
  104. dPlants.removeChild(this.zw);
  105. }
  106. function Zombie(){ //定义僵尸
  107. this.js=this.init();
  108. this.blood=10;
  109. }
  110. Zombie.prototype.init=function(){ //僵尸初始化
  111. var js=document.createElement("div");
  112. js.innerHTML='<img src="images/interface/plantshadow32.png" style="position:absolute;left:72px;top:122px;">' +
  113. '<img src="images/Zombies/Zombie.gif">';//僵尸的定位
  114. js.style.left=700+"px";
  115. dZombies.appendChild(js);
  116. return js;
  117. }
  118. Zombie.prototype.walk=function(){//僵尸行走
  119. if(this.walkTimer==null) {
  120. var This=this;
  121. var imgs=this.js.getElementsByTagName("img");
  122. imgs[1].src='images/Zombies/Zombie.gif';
  123. this.walkTimer = setInterval(function () {
  124. This.js.style.left = (This.js.offsetLeft - 1) + "px";
  125. }, 80);
  126. }
  127. }
  128. Zombie.prototype.lossHeadWalk=function(){//无头僵尸行走
  129. //alert(12);
  130. var This=this;
  131. //原因:用于将僵尸吃改成走
  132. var imgs=this.js.getElementsByTagName("img");
  133. imgs[1].src='images/Zombies/ZombieLostHead.gif';
  134. if(this.walkTimer==null) {
  135. //僵尸每1秒行走1px
  136. this.walkTimer = setInterval(function () {
  137. This.js.style.left = (This.js.offsetLeft - 1) + "px";
  138. }, 80);
  139. }
  140. }
  141. Zombie.prototype.lossHead=function(){//僵尸掉头
  142. var head=document.createElement("img");
  143. head.src='images/Zombies/ZombieHead.gif';
  144. head.style.position="absolute";
  145. head.style.left=(this.js.offsetLeft+38)+"px";
  146. head.style.top=this.js.offsetTop+"px";
  147. head.style.zIndex="23";
  148. dZombies.appendChild(head);
  149. setTimeout(function(){
  150. dZombies.removeChild(head);
  151. },800);
  152. }
  153. Zombie.prototype.eat=function(){//僵尸吃
  154. //僵尸停止行走
  155. clearInterval(this.walkTimer);
  156. this.walkTimer=null;
  157. var imgs=this.js.getElementsByTagName("img");
  158. imgs[1].src='images/Zombies/ZombieAttack.gif';
  159. }
  160. Zombie.prototype.noHeadEat=function(){//无头僵尸吃
  161. clearInterval(this.walkTimer);//僵尸停止行走
  162. this.walkTimer=null;
  163. var imgs=this.js.getElementsByTagName("img");
  164. imgs[1].src='images/Zombies/ZombieLostHeadAttack.gif';
  165. }
  166. Zombie.prototype.die=function(){//僵尸死亡
  167. //僵尸停止行走
  168. clearInterval(this.walkTimer);
  169. this.walkTimer=null;
  170. var imgs=this.js.getElementsByTagName("img");
  171. imgs[1].src='images/Zombies/ZombieDie.gif';
  172. this.clean();
  173. }
  174. Zombie.prototype.clean=function(){//僵尸消失
  175. var This=this;
  176. setTimeout(function(){
  177. dZombies.removeChild(This.js);
  178. },2000);
  179. }
  180. var zombie=new Zombie();
  181. zombie.walk();
  182. var plant=new Plant();
  183. plant.doPlant();
  184. plant.attack(zombie);
  185. }
  186. </script>
  187. </head>
  188. <body id="dBody" bgcolor="#008080">
  189. <!--主界面EDAll-->
  190. <div class="WindowFrame" id="dAll">
  191. <div id="tGround"><!--背景图片--></div>
  192. <div id="sod"><!--草地背景--></div>
  193. <div id="dPlants"></div> <!--植物容器-->
  194. <div id="dZombies"></div> <!--僵尸容器-->
  195. </div>
  196. </body>
  197. </html>

 

 

 

 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/760534
推荐阅读
相关标签
  

闽ICP备14008679号