• 做一个贪吃蛇小游戏happy一下


    直接Vue上代码

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. ctx: null,
    6. interval: null,
    7. snakeData: [],
    8. count: 0,//步数
    9. pointList: [{ x: 36, y: 18 }],//默认点
    10. direction: "d", // 默认右,w:上,s:下,a:左,d:右
    11. controll: null,//监听
    12. controll2: null,//监听
    13. speedCount: 0, // 渲染倍数
    14. speed: 8 // 渲染倍数分子
    15. };
    16. },
    17. mounted() {
    18. // 贪吃蛇,canvas
    19. const canvas = document.getElementById("canvas");
    20. this.ctx = canvas.getContext("2d"); // 获取绘制上下文
    21. const list = [];
    22. for (let i = 0; i < 2; i++) {
    23. list.push({
    24. x: 0 + i * 6,
    25. y: 0,
    26. type: "right"
    27. });
    28. }
    29. this.snakeData = list;
    30. console.log("snakeData", this.snakeData);
    31. this.setView(list);
    32. this.controll = new AbortController();
    33. this.controll2 = new AbortController();
    34. window.addEventListener(
    35. "keydown",
    36. val => {
    37. if (
    38. val.key === "w" ||
    39. val.key === "a" ||
    40. val.key === "s" ||
    41. val.key === "d"
    42. ) {
    43. // 方向不能相反
    44. switch (val.key) {
    45. case "w":
    46. if (this.direction !== "s") {
    47. this.direction = "w";
    48. this.speed = 4;
    49. }
    50. break;
    51. case "a":
    52. if (this.direction !== "d") {
    53. this.direction = "a";
    54. this.speed = 4;
    55. }
    56. break;
    57. case "s":
    58. if (this.direction !== "w") {
    59. this.direction = "s";
    60. this.speed = 4;
    61. }
    62. break;
    63. case "d":
    64. if (this.direction !== "a") {
    65. this.direction = "d";
    66. this.speed = 4;
    67. }
    68. break;
    69. default:
    70. console.log("方向不合法或无效按键");
    71. break;
    72. }
    73. }
    74. },
    75. { signal: this.controll.signal }
    76. );
    77. window.addEventListener(
    78. "keyup",
    79. val => {
    80. if (
    81. val.key === "w" ||
    82. val.key === "a" ||
    83. val.key === "s" ||
    84. val.key === "d"
    85. ) {
    86. this.speed = 8;
    87. }
    88. },
    89. { signal: this.controll2.signal }
    90. );
    91. this.count = 0;
    92. this.intervalFun();
    93. },
    94. beforeDestroy() {
    95. this.count = 0;
    96. this.interval && cancelAnimationFrame(this.interval);
    97. this.controll.abort();
    98. this.controll2.abort();
    99. },
    100. methods: {
    101. intervalFun() {
    102. this.count++;
    103. this.speedCount++;
    104. // 默认向右移动
    105. if (this.count > 5000) {
    106. // clearInterval(this.interval);
    107. cancelAnimationFrame(this.interval);
    108. } else {
    109. if (this.speedCount % this.speed === 0) {
    110. // 速度缩减10倍,执行10次才渲染1次
    111. const w = canvas.clientWidth;
    112. const h = canvas.clientHeight;
    113. this.ctx.clearRect(0, 0, w, h);
    114. this.goForword(this.snakeData);
    115. }
    116. }
    117. this.interval = requestAnimationFrame(this.intervalFun);
    118. },
    119. goForword(list) {
    120. // console.log("list1", list);
    121. const option = { ...list[list.length - 1] };
    122. if (this.direction === "d") {
    123. option.x = Number(option.x) + 6;
    124. } else if (this.direction === "a") {
    125. option.x = Number(option.x) - 6;
    126. } else if (this.direction === "w") {
    127. option.y = Number(option.y) - 6;
    128. } else if (this.direction === "s") {
    129. option.y = Number(option.y) + 6;
    130. }
    131. list.push(option);
    132. let flag = false;
    133. // console.log("option", option);
    134. this.pointList = this.pointList.filter(item => {
    135. if (option.x === item.x && option.y === item.y) {
    136. flag = true;
    137. }
    138. return item.x !== option.x || item.y !== option.y;
    139. });
    140. if (!flag) {
    141. list.shift();
    142. } else {
    143. // 重新生成点
    144. const x = Math.floor(Math.random() * Math.round(400 / 6)) * 6;
    145. const y = Math.floor(Math.random() * Math.round(400 / 6)) * 6;
    146. this.pointList.push({
    147. x,
    148. y
    149. });
    150. }
    151. this.snakeData = list;
    152. // console.log("snakeData", this.snakeData);
    153. this.setView(this.snakeData); // 画蛇
    154. this.setView(this.pointList); // 画点
    155. },
    156. setView(list) {
    157. // this.ctx.clearRact(0, 0, 400, 400);
    158. this.ctx.beginPath();
    159. for (let i = 0; i < list.length; i++) {
    160. this.ctx.rect(list[i].x, list[i].y, 6, 6); // 绘制矩形
    161. }
    162. this.ctx.fill(); // 描边一个矩形轮廓
    163. }
    164. }
    165. }
    166. script>
    167. <style lang="scss" scoped>
    168. #canvas {
    169. border: 1px solid #dee2ed;
    170. }
    171. style>

    直接拿着用即可

  • 相关阅读:
    阿里P8现身说法,解密“架构”原理与实战笔记:从分布式到微服务
    FIR 中级应用 - AM 调幅波调制解调(FIR + FIFO)
    P8739 [蓝桥杯 2020 国 C] 重复字符串
    udp多播
    Python算法练习 10.11
    kubesphere3.4.1不同等级告警发送至不同邮箱
    企业集中监控体系思路及架构
    吴峰光杀进 Linux 内核
    Qt httpclient
    云计算场景下,如何快速定位出虚拟机reboot/shutdown引发的故障
  • 原文地址:https://blog.csdn.net/2201_75705263/article/details/133353429