在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

简陋的贪吃蛇

2016-12-20 13:14| 发布者: zhangjf| 查看: 424| 评论: 0

摘要: 这是一个使用html,javascript实现的简陋的贪吃蛇程序 !DOCTYPE htmlhtmlheadmeta charset=UTF-8title贪吃蛇/titlemeta name=viewport content=width=device-width, initial-s ...
这是一个使用html,javascript实现的简陋的贪吃蛇程序
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>贪吃蛇</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <style type="text/css">
  8. html,body{
  9. margin:0;
  10. padding: 0;
  11. width:100%;
  12. height:100%;
  13. background-color: #3f3f3f;
  14. }
  15. canvas{
  16. position:relative;
  17. left:50%;
  18. top:50%;
  19. margin-left:-300px;
  20. margin-top:-300px;
  21. border:1px solid #000;
  22. background-color: #f0f0f0;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <canvas id="gameCanvas" width="600" height="600"></canvas>
  28. <script type="text/javascript" src="./js/main.js"></script>
  29. </body>
  30. </html>
复制代码
  1. (function(context){
  2. //game config
  3. var config={
  4. width:600,
  5. height:600,
  6. unit:10
  7. };
  8. //game snake
  9. var snake={
  10. body:[],
  11. v:1,
  12. direction:"left",
  13. init:function(){
  14. this.body.push({x:config.width/config.unit/2,y:config.height/config.unit/2});
  15. },
  16. draw:function(){
  17. context.save();
  18. for(var i=0;i<this.body.length;i++){
  19. context.fillRect(this.body[i].x*config.unit,this.body[i].y*config.unit,config.unit,config.unit);
  20. }
  21. context.restore();
  22. },
  23. move:function(direction){
  24. if(this.direction=="left"&&direction=="right") return;
  25. if(this.direction=="up"&&direction=="down") return;
  26. if(this.direction=="right"&&direction=="left") return;
  27. if(this.direction=="down"&&direction=="up") return;
  28. this.direction=direction||this.direction;
  29. var nextX=this.body[0].x;
  30. var nextY=this.body[0].y;
  31. if("left"==this.direction){
  32. nextX-=this.v;
  33. }else if("up"==this.direction){
  34. nextY-=this.v;
  35. }else if("right"==this.direction){
  36. nextX+=this.v;
  37. }else if("down"==this.direction){
  38. nextY+=this.v;
  39. }
  40. this.body.pop(),
  41. this.body.unshift({x:nextX,y:nextY});
  42. },
  43. eatFood:function(food){
  44. this.body.push({x:food.x,y:food.y});
  45. environment.destroyFood(food);
  46. }
  47. };
  48. var environment={
  49. currEntifyId:0,
  50. entity:[],
  51. init:function(){
  52. },
  53. nextEntifyId:function(){
  54. return this.currEntifyId++;
  55. },
  56. createFood:function(){
  57. this.entity.push({id:this.nextEntifyId(),x:parseInt(Math.random()*(config.width/config.unit)), y:parseInt(Math.random()*(config.height/config.unit)), type:"food"});
  58. },
  59. destroyFood:function(food){
  60. var newEntify=[];
  61. for(var i=0;i<this.entity.length;i++){
  62. if(this.entity[i].id==food.id) continue;
  63. newEntify.push(this.entity[i]);
  64. }
  65. this.entity=newEntify;
  66. },
  67. draw:function(){
  68. context.save();
  69. var hasFood=false;
  70. for(var i=0;i<this.entity.length;i++){
  71. if(this.entity[i].type=="food"){
  72. hasFood=true;
  73. context.fillRect(this.entity[i].x*config.unit,this.entity[i].y*config.unit,config.unit,config.unit);
  74. }
  75. }
  76. context.restore();
  77. if(!hasFood) this.createFood();
  78. }
  79. };
  80. //game event manager
  81. var gameEvent={
  82. onSnakeEatFood:function(snake,food){
  83. snake.eatFood(food);
  84. },
  85. onSnakeAgainstWall:function(){
  86. alert("game over!!!");
  87. },
  88. onSnakeEatSelf:function(){
  89. alert("game over!!!");
  90. }
  91. };
  92. //keybord event
  93. document.onkeydown=function(event){
  94. var key=event.keyCode;
  95. if(key==37){ //left
  96. snake.move("left");
  97. }else if(key==38){ //up
  98. snake.move("up");
  99. }else if(key==39){ //right
  100. snake.move("right");
  101. }else if(key==40){ //down
  102. snake.move("down");
  103. }
  104. };
  105. //init game
  106. environment.init();
  107. snake.init();
  108. //main game loop
  109. var currentFrame=0;
  110. (function gameLoop(){
  111. if(currentFrame%10==0){
  112. //clear canvas
  113. context.clearRect(0,0,config.width,config.height);
  114. //check event
  115. if(snake.body[0].x<=0||snake.body[0].y<=0||
  116. snake.body[0].x>=config.width/config.unit||snake.body[0].y>=config.height/config.unit){
  117. gameEvent.onSnakeAgainstWall();
  118. }
  119. for(var i=1;i<snake.body.length;i++){
  120. var dX2=Math.pow(snake.body[0].x-snake.body[i].x,2);
  121. var dY2=Math.pow(snake.body[0].y-snake.body[i].y,2);
  122. if((dX2+dY2)==0){
  123. gameEvent.onSnakeEatSelf();
  124. }
  125. }
  126. for(var i=0;i<environment.entity.length;i++){
  127. var entifyX=environment.entity[i].x;
  128. var entifyY=environment.entity[i].y;
  129. var entifyType=environment.entity[i].type;
  130. var dX2=Math.pow((snake.body[0].x-entifyX),2);
  131. var dY2=Math.pow((snake.body[0].y-entifyY),2);
  132. if((dX2+dY2)==0){
  133. gameEvent.onSnakeEatFood(snake,environment.entity[i]);
  134. }
  135. }
  136. //draw stage
  137. environment.draw();
  138. snake.move();
  139. snake.draw();
  140. }
  141. //next frame
  142. currentFrame++;
  143. requestAnimationFrame(gameLoop);
  144. })();
  145. })(document.getElementById("gameCanvas").getContext("2d"));
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 05:31

Copyright 2015-2025 djqfx

返回顶部