1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>贪吃蛇</title>
    8. <style>
    9. *{
    10. padding: 0;
    11. margin: 0;
    12. box-sizing: border-box;
    13. }
    14. #main{
    15. margin: 100px;
    16. }
    17. .btn{
    18. width: 100px;
    19. height: 40px;
    20. /* margin: 10px; */
    21. }
    22. .canvas{
    23. position: relative;
    24. top: 40px;
    25. border: 1px solid rgb(107, 221, 178);
    26. background-color: rgb(218, 236, 245);
    27. /* background-color: rgb(241, 159, 112); */
    28. }
    29. </style>
    30. </head>
    31. <body>
    32. <div id="main">
    33. <input class="btn" type="button" value="开始游戏" id="begin"/>
    34. <input class="btn" type="button" value="暂停游戏" id="pause"/>
    35. <script>
    36. var timer; //变量可以提升
    37. var showCanvas = true; //是否显示格子
    38. /*设置地图的构造方法:
    39. atom为原子初始化的宽高(应该一致)
    40. xnum为地图x轴应该有多少个原子(横向)
    41. ynum为地图y轴应该有多少个原子(纵向)
    42. */
    43. function Map(atom,xnum,ynum){
    44. this.atom = atom;
    45. this.xnum = xnum;
    46. this.ynum = ynum;
    47. this.canvas = null; //初始化画布
    48. this.create = function(){
    49. this.canvas = document.createElement("div");
    50. this.canvas.className = "canvas";
    51. this.canvas.style.width = this.atom * this.xnum + "px"; //画布的宽
    52. this.canvas.style.height = this.atom * this.ynum + "px"; //画布的高
    53. main.appendChild(this.canvas);
    54. //判断是否需要显示格子
    55. if (showCanvas){
    56. for (var i = 0;i<xnum;i++){
    57. for (var j = 0;j<ynum;j++){
    58. //定义格子的样式
    59. var lattice = document.createElement("div");
    60. lattice.style.border = "1px solid rgb(107, 221, 178)";
    61. lattice.style.width = this.atom + "px";
    62. lattice.style.height = this.atom + "px";
    63. //添加格子到地图中
    64. this.canvas.appendChild(lattice);
    65. lattice.style.position = "absolute";
    66. //利用绝对定位
    67. lattice.style.left = i * this.atom + "px";
    68. lattice.style.top = j * this.atom + "px";
    69. }
    70. }
    71. }
    72. }
    73. }
    74. //创建食物的构造方法
    75. function Food(map){
    76. this.width = map.atom;
    77. this.height = map.atom;
    78. this.bgcolor = "rgb(" + Math.floor(Math.random()*200) + "," + Math.floor(Math.random()*200) + ","+ Math.floor(Math.random()*200) + ")";
    79. //基于x轴和y轴的位置
    80. this.x = Math.floor(Math.random()*map.xnum);
    81. this.y = Math.floor(Math.random()*map.ynum);
    82. this.flag = document.createElement("div");
    83. //设置食物的样式
    84. this.flag.style.width = this.width + "px";
    85. this.flag.style.borderRadius = "5px";
    86. this.flag.style.height = this.height + "px";
    87. this.flag.style.backgroundColor = this.bgcolor;
    88. //定义食物的位置
    89. this.flag.style.position = "absolute";
    90. this.flag.style.left = this.x * this.width + "px";
    91. this.flag.style.top = this.y * this.height + "px";
    92. map.canvas.appendChild(this.flag);
    93. }
    94. //创建蛇的构造方法
    95. function Sanke(map){
    96. //设置宽、高
    97. this.width = map.atom;
    98. this.height = map.atom;
    99. //默认走的方向
    100. this.direction = "right";
    101. //设置蛇的主体
    102. this.body = [
    103. { x:2, y:0 }, //蛇头
    104. { x:1, y:0 }, //蛇体
    105. { x:0, y:0 }, //蛇尾
    106. ];
    107. //显示蛇 var i in this.body
    108. this.display = function(){
    109. for (var i = 0;i < this.body.length; i++){
    110. if (this.body[i].x != null){
    111. var create_s = document.createElement("div");
    112. //将节点保存到一个状态变量当中,以便之后删除
    113. this.body[i].flag = create_s;
    114. //设置蛇的样式
    115. create_s.style.width = this.width + "px";
    116. create_s.style.height = this.height + "px";
    117. /* create_s.style.border = "2px solid rgb(241, 159, 112)" */
    118. create_s.style.backgroundColor = "rgb(107, 109, 221)";
    119. create_s.style.borderRadius = "5px";
    120. //设置蛇的位置
    121. create_s.style.position = "absolute";
    122. create_s.style.left = this.body[i].x * this.width + "px";
    123. create_s.style.top = this.body[i].y * this.height + "px";
    124. //添加到地图中
    125. map.canvas.appendChild(create_s);
    126. }
    127. }
    128. }
    129. //控制蛇的运动
    130. this.run = function(){
    131. /*
    132. { x:2, y:0 }, //蛇头
    133. { x:1, y:0 }, //蛇体
    134. { x:0, y:0 }, //蛇尾
    135. 让x轴的每个位置+1
    136. */
    137. for (var i = this.body.length-1; i>0; i--) {
    138. this.body[i].x = this.body[i-1].x;
    139. this.body[i].y = this.body[i-1].y;
    140. }
    141. //根据方向处理蛇头
    142. switch (this.direction) {
    143. case "left":this.body[0].x--; break;
    144. case "right":this.body[0].x++; break;
    145. case "up": this.body[0].y--; break;
    146. case "down": this.body[0].y++; break;
    147. }
    148. //删除旧的元素
    149. for (var i in this.body){
    150. if (this.body[i].flag != null){ //当吃到食物,flag等于null,且不能删除
    151. map.canvas.removeChild(this.body[i].flag);
    152. }
    153. }
    154. //显示新的元素
    155. this.display();
    156. }
    157. }
    158. //实例化地图类
    159. var map = new Map(20,40,20);
    160. map.create(); //创建画布
    161. //实例化食物类,并创建食物
    162. var food = new Food(map);
    163. //实例化蛇类,创建蛇
    164. var sanke = new Sanke(map);
    165. //显示蛇
    166. sanke.display();
    167. //加入键盘事件,控制蛇的方向
    168. document.onkeydown = function(event){
    169. event = event || window.event;
    170. //上:38 右:39 下:40 左:37
    171. switch (event.keyCode){
    172. case 38:
    173. if (sanke.direction != "down"){
    174. sanke.direction = "up";
    175. }
    176. break;
    177. case 39:
    178. if (sanke.direction != "left"){
    179. sanke.direction = "right";
    180. }
    181. break;
    182. case 40:
    183. if (sanke.direction != "up"){
    184. sanke.direction = "down";
    185. }
    186. break;
    187. case 37:
    188. if (sanke.direction != "right"){
    189. sanke.direction = "left";
    190. }
    191. break;
    192. }
    193. }
    194. //给开始按钮绑定函数
    195. document.querySelector("#begin").onclick = function(){
    196. clearInterval(timer);
    197. timer = setInterval(function(){
    198. sanke.run();
    199. },300);
    200. }
    201. document.querySelector("#pause").onclick = function(){
    202. clearInterval(timer);
    203. }
    204. </script>
    205. </div>
    206. </body>
    207. </html>