script> script src="https://ajax.googleapis.com/ajax/libs/jquer">
当前位置:   article > 正文

D3.js版贪吃蛇(Eating Snake)

D3.js版贪吃蛇(Eating Snake)

简介

三方君用 d3.js 实现贪吃蛇小游戏设计

点这边玩玩看

Source Code

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Eating Snake</title>
  5. <script src="https://d3js.org/d3.v3.min.js"></script>
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. <style>
  10. button {
  11. padding: 6px 12px;
  12. font-size: 14px;
  13. background-color: #fff;
  14. border: 1px solid #ccc;
  15. border-radius: 4px;
  16. margin: 5px;
  17. }
  18. .start {
  19. color: white;
  20. background-color: red;
  21. border: 1px solid transparent;
  22. }
  23. .new {
  24. color: white;
  25. background-color: #5cb85c;
  26. border: 1px solid transparent;
  27. }
  28. #Grids{
  29. border: 1.5px solid #ccc;
  30. max-width: 250px;
  31. max-height: 250px;
  32. }
  33. </style>
  34. <p>Eating Snake</p>
  35. <div id="Control"></div>
  36. <div id="Grids"></div>
  37. <script>
  38. $(document).ready(function(){
  39. var rectW = 25, rectL = 25, cc = 10, delayTime = 100, timerID = 0, stopped = true,
  40. nextDir, grids, body, food, head,
  41. color = ['white','black','green'],
  42. directionVectors = {
  43. 65: [-1,0], 37: [-1,0], // 'A' or Left Arrow
  44. 87: [0,-1], 38: [0,-1], // 'W' or Up Arrow
  45. 68: [1,0], 39: [1,0], // 'D' or Right Arrow
  46. 83: [0,1], 40: [0,1] // 'S' or Down Arrow
  47. };
  48. var xs = d3.scale.linear().domain([0,rectL]).range([0,rectL * cc]),
  49. ys = d3.scale.linear().domain([0,rectW]).range([0,rectW * cc]);
  50. var rects = d3.select("#Grids")
  51. .append("svg:svg")
  52. .attr("stroke", "rgba(204, 204,204,0.2)")
  53. .attr("shape-rendering", "optimizeSpeed")
  54. .attr("width",rectL*cc)
  55. .attr("height", rectW*cc);
  56. var ctrl = d3.select("#Control")
  57. ctrl.append('button')
  58. .attr('class', 'new')
  59. .text('New')
  60. .on('click', function(){ return setNewGrid() });
  61. ctrl.append('button')
  62. .attr('class', 'start')
  63. .text('Start')
  64. .on('click', startStop);
  65. var initialize = function(){
  66. grids = [];
  67. d3.range(rectL).forEach(function(x){
  68. grids[x] = new Array();
  69. d3.range(rectW).forEach(function(y){
  70. grids[x][y] = 0;
  71. });
  72. });
  73. body = [[0,0],[0,1],[0,2],[0,3]];
  74. head = [0,3];
  75. nextDir = [0,1];
  76. makeFood();
  77. makeBody();
  78. rects.selectAll("rect")
  79. .data(function(){ return setGrid()})
  80. .enter()
  81. .append("rect")
  82. .attr("x", function(d){ return xs(d.x)})
  83. .attr("y", function(d){ return ys(d.y)})
  84. .attr("width",cc)
  85. .attr("height",cc)
  86. .attr("fill", function(d){ return color[d.state];});
  87. };
  88. $(document).keydown(function(event){
  89. var vector = directionVectors[event.which];
  90. if (!vector) return;
  91. // (right <--> left or up <--> down)
  92. if ((vector[0] !== 0 && vector[0] + nextDir[0] === 0) ||
  93. (vector[1] !== 0 && vector[1] + nextDir[1] === 0)) {
  94. return;
  95. }
  96. nextDir = vector;
  97. event.preventDefault()
  98. });
  99. function collide(a, b){
  100. var k = 0
  101. b.forEach( function(x){
  102. if(a[0]==x[0] && a[1]==x[1]){
  103. k = 1;
  104. }
  105. });
  106. return k;
  107. } ;
  108. function makeFood(){
  109. var x, y, k = 1;
  110. while(k){
  111. x = Math.floor(Math.random()*rectL);
  112. y = Math.floor(Math.random()*rectW);
  113. if(!collide([x,y],body))
  114. k = 0;
  115. }
  116. food = [x, y];
  117. grids[x][y] = 2;
  118. }
  119. function makeBody(){
  120. for(var i in body){
  121. var x = body[i][0],
  122. y = body[i][1];
  123. grids[x][y] = 1;
  124. }
  125. }
  126. var setGrid = function (){
  127. var bits = [];
  128. for(var i= 0; i< rectL; i++){
  129. for(var j= 0; j< rectW; j++){
  130. bits.push({ "x": i, "y": j, "state": grids[i][j] });
  131. }
  132. }
  133. return bits;
  134. };
  135. var setNewGrid = function (){
  136. initialize();
  137. displayGrid();
  138. nextDir = [0,1];
  139. }
  140. var move = function(){
  141. function getNext(i,j){
  142. var x = i[0] + j[0];
  143. var y = i[1] + j[1];
  144. return [x, y];
  145. }
  146. var nextPos = getNext(head, nextDir);
  147. if(nextPos[0] < 0 || nextPos[1] < 0 || nextPos[0] >= rectL || nextPos[1] >= rectW ){
  148. startStop();
  149. return;
  150. }
  151. if(collide(nextPos, body)){
  152. startStop();
  153. return;
  154. }
  155. head[0] = nextPos[0];
  156. head[1] = nextPos[1];
  157. body.push([head[0], head[1]]);
  158. if(nextPos[0] == food[0] && nextPos[1] == food[1]){
  159. makeFood();
  160. }
  161. else{
  162. grids[body[0][0]][body[0][1]] = 0;
  163. body.shift();
  164. }
  165. makeBody()
  166. displayGrid();
  167. };
  168. var displayGrid = function(){
  169. rects.selectAll("rect")
  170. .data(function(){ return setGrid()})
  171. .transition()
  172. .attr("fill", function(d) { return color[d.state]; })
  173. .duration(0);
  174. }
  175. function startStop() {
  176. stopped = !stopped;
  177. d3.select('button.start')
  178. .text(function () { return stopped ? 'Start' : 'Stop';});
  179. }
  180. var run = function(){
  181. clearInterval(timerID);
  182. timerID = setInterval( function(){ if (!stopped) move();},delayTime);
  183. }
  184. initialize();
  185. run();
  186. });
  187. </script>
  188. </body>
  189. </html>

 

 

D3JS相关系列教程

  1. D3.js 实战 - 上色落伍了,来点图样吧!
  2. D3.js 实战 - 地表最快地球仪应用   
  3. 让我的视觉化动起来!D3.js 动画函数入门
  4. D3.js 视觉化教学 -全球即时地震视觉化
  5. 总整理!你不可错过的 d3.js 数据转换技巧
  6. D3.js版贪吃蛇(Eating Snake)
  7. 统计图表制作 – 使用 d3-generator
  8. 用 JavaScript 做数据视觉化 - D3.JS
  9. 高手才知道!七个你所不知道的 D3.js 秘技
  10. D3JS 实战 - 是长条也是圆饼的变形图表

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

闽ICP备14008679号