{"id":10006395,"date":"2022-09-05T13:14:12","date_gmt":"2022-09-05T05:14:12","guid":{"rendered":"https:\/\/ityet.com\/?p=10006395"},"modified":"2022-09-05T13:14:12","modified_gmt":"2022-09-05T05:14:12","slug":"%e7%94%a8vue-js%e5%86%99%e4%b8%80%e4%b8%aa%e5%91%bd%e4%bb%a4%e8%a1%8c%e8%b4%aa%e5%90%83%e8%9b%87%e6%b8%b8%e6%88%8f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006395","title":{"rendered":"\u7528Vue.js\u5199\u4e00\u4e2a\u547d\u4ee4\u884c\u8d2a\u5403\u86c7\u6e38\u620f"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u5927\u5bb6\u597d,\u6211\u662f<a href=\"https:\/\/github.com\/webfansplz\">webfansplz<\/a>.\u672c\u6587\u8981\u5206\u4eab\u7684\u662f\u5982\u4f55\u4f7f\u7528Vue.js\u5b9e\u73b0\u4e00\u4e2a\u547d\u4ee4\u884c\u8d2a\u5403\u86c7\u6e38\u620f(<a href=\"https:\/\/github.com\/webfansplz\/temir-snake-game\">temir-snake-game<\/a>).\u5bf9\u4e8e\u8d2a\u5403\u86c7\u6e38\u620f\u60f3\u5fc5\u5927\u5bb6\u90fd\u4e0d\u964c\u751f\u4e86,\u4f7f\u7528Vue.js\u5b9e\u73b0\u4e00\u4e2aWeb\u7248\u7684\u8d2a\u5403\u86c7\u6e38\u620f\u4f3c\u4e4e\u6ca1\u4ec0\u4e48\u96be\u5ea6,\u90a3\u5982\u679c\u662f\u547d\u4ee4\u884c\u7248\u7684\u5462?\u662f\u4e0d\u662f\u4f60\u4f1a\u5bf9\u5b83\u7684\u5b9e\u73b0\u539f\u7406\u611f\u5174\u8da3\u5462?\u8ba9\u6211\u4eec\u5f00\u59cb\u5427!<\/p>\n<h2>\u5b89\u88c5<\/h2>\n<pre><code class=\"bash\">npm install temir-snake-game -g<\/code><\/pre>\n<h2>\u5f00\u59cb\u6e38\u620f<\/h2>\n<p>\u5728\u7ec8\u7aef\u7a97\u53e3\u8fd0\u884c<code>temir-sg<\/code>.<\/p>\n<p>\u5bf9\u4e8eWindows\u7cfb\u7edf,\u63a8\u8350\u4f7f\u7528<a href=\"https:\/\/github.com\/vercel\/hyper\">hyper<\/a>\u7ec8\u7aef\u8fdb\u884c\u4f53\u9a8c.<\/p>\n<h2>\u5c06Vue\u6e32\u67d3\u5230\u547d\u4ee4\u884c\u754c\u9762<\/h2>\n<p>\u4f7f\u7528Vue.js\u5b9e\u73b0\u547d\u4ee4\u884c\u8d2a\u5403\u86c7\u6e38\u620f,\u9996\u5148\u610f\u5473\u7740\u6211\u4eec\u8981\u5c06Vue.js\u6e32\u67d3\u5230\u547d\u4ee4\u884c\u754c\u9762,\u624d\u80fd\u5f00\u59cb\u5177\u4f53\u7684\u6e38\u620f\u5b9e\u73b0.\u6211\u4eec\u7ecf\u5e38\u7528Vue.js\u6765\u7f16\u5199Web\u5e94\u7528,\u4f46\u662fVue\u7684\u80fd\u529b\u5374\u4e0d\u4ec5\u4ec5\u5c40\u9650\u4e8e\u6b64,\u5b83\u7684\u821e\u53f0\u4e5f\u4e0d\u53ea\u6709\u6d4f\u89c8\u5668.Vue3\u62e5\u6709\u51fa\u8272\u7684\u8de8\u5e73\u53f0\u80fd\u529b,\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<a href=\"https:\/\/cn.vuejs.org\/api\/custom-renderer.html#create-renderer\">createRenderer API<\/a>\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6e32\u67d3\u5668,\u901a\u8fc7\u521b\u5efa\u5bbf\u4e3b\u73af\u5883\u4e2d\u5bf9\u5e94\u7684Node\u548cElement,\u5e76\u5bf9\u5143\u7d20\u8fdb\u884c\u589e\u5220\u6539\u67e5\u64cd\u4f5c.<\/p>\n<p>\u5f97\u76ca\u4e8eVue3\u51fa\u8272\u7684\u8de8\u5e73\u53f0\u80fd\u529b,\u6211\u5b9e\u73b0\u4e86<a href=\"https:\/\/github.com\/webfansplz\/temir\">Temir<\/a>,\u4e00\u4e2a\u7528Vue\u7ec4\u4ef6\u6765\u7f16\u5199\u547d\u4ee4\u884c\u754c\u9762\u5e94\u7528\u7684\u5de5\u5177.\u5f00\u53d1\u8005\u53ea\u9700\u8981\u4f7f\u7528Vue\u5c31\u53ef\u4ee5\u7f16\u5199\u547d\u4ee4\u884c\u5e94\u7528,\u4e0d\u9700\u8981\u4efb\u4f55\u989d\u5916\u7684\u5b66\u4e60\u6210\u672c.\u987a\u4fbf\u503c\u5f97\u4e00\u63d0\u7684\u662f,\u5b83\u8fd8\u652f\u6301HMR~<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042431220\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5173\u4e8e<a href=\"https:\/\/github.com\/webfansplz\/temir\">Temir<\/a>\u5c31\u4e0d\u5728\u8fd9\u91cc\u8fdb\u884c\u8be6\u7ec6\u7684\u4ecb\u7ecd\u4e86,\u6709\u5174\u8da3\u7684\u7ae5\u978b\u53ef\u4ee5\u4e0a<a href=\"https:\/\/github.com\/webfansplz\/temir\">Github<\/a>\u67e5\u770b\u4ecb\u7ecd\u6216\u8005\u770b<a href=\"https:\/\/segmentfault.com\/a\/1190000042288656\">\u4f7f\u7528Vue.js\u7f16\u5199\u547d\u4ee4\u884c\u754c\u9762<\/a>\u8fd9\u7bc7\u6587\u7ae0.<\/p>\n<h2>\u8d2a\u5403\u86c7\u6e38\u620f\u5b9e\u73b0<\/h2>\n<p>\u6709\u4e86<a href=\"https:\/\/github.com\/webfansplz\/temir\">Temir<\/a>,\u6211\u4eec\u5c31\u5177\u5907\u4e86\u4f7f\u7528Vue.js\u7f16\u5199\u547d\u4ee4\u884c\u6e38\u620f\u7684\u6761\u4ef6,\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u770b\u770b\u6e38\u620f\u7684\u5177\u4f53\u5b9e\u73b0:<\/p>\n<h3>\u5b9e\u73b0\u62c6\u89e3<\/h3>\n<p>\u9996\u5148\u6211\u4eec\u5bf9\u6e38\u620f\u5b9e\u73b0\u8fdb\u884c\u4e00\u4e0b\u7b80\u5355\u7684\u62c6\u89e3,\u4ece\u5143\u7d20+\u903b\u8f91\u7684\u7ef4\u5ea6\u6765\u770b,\u53ef\u4ee5\u7b80\u5355\u5206\u4e3a\u51e0\u90e8\u5206: <\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042431221\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h4>\u5143\u7d20\u521d\u59cb\u5316<\/h4>\n<h5>\u7ade\u6280\u53f0<\/h5>\n<p>\u86c7\u7684\u722c\u884c\u4e0e\u98df\u7269\u7684\u751f\u6210\u90fd\u9700\u8981\u4f9d\u8d56\u5750\u6807,\u6700\u7b80\u5355\u7684\u5750\u6807\u5176\u5b9e\u53ea\u9700\u8981\u4e00\u4e2a\u7d22\u5f15\u503c.\u56e0\u6b64\u7ade\u6280\u53f0\u7684\u7ec4\u6210\u4e5f\u5f88\u7b80\u5355,\u5c31\u662f\u7531\u5f88\u591a\u4e2a\u5c0f\u76d2\u5b50(\u8fd9\u91cc\u4ee5\u2b1b\u8868\u793a)\u7ec4\u6210,\u6bcf\u4e00\u4e2a\u76d2\u5b50\u5bf9\u5e94\u4e00\u4e2a\u5750\u6807(\u7d22\u5f15),\u6211\u4eec\u8981\u505a\u7684\u662f\u4e00\u4e2a28*28\u7684\u7ade\u6280\u53f0,\u56e0\u6b64\u5b83\u7684\u7d22\u5f15\u96c6\u5408\u5c31\u662f(0~783).<\/p>\n<pre><code class=\"js\">const basic = 28\nconst backgroundIcon = '\u2b1b'\nconst arena = ref&lt;string[]&gt;([])\n\nfunction initArena() {\n arena.value = Array.from({ length: basic * basic }, () =&gt; backgroundIcon)\n} <\/code><\/pre>\n<h5>\u86c7<\/h5>\n<p>\u524d\u9762\u6211\u4eec\u63d0\u5230\u4e86\u5750\u6807\u7684\u6982\u5ff5,\u86c7\u8eab\u7684\u7ec4\u6210\u5c31\u662f\u4e00\u4e32\u6709\u89c4\u5f8b\u7684\u5750\u6807.<\/p>\n<pre><code class=\"js\">const snakeIcon = '\ud83d\udfe7'\n\/\/ \u5750\u6807(\u7d22\u5f15)30,29 \u957f\u5ea6\u4e3a2\u7684\u86c7\u8eab\nconst snakeBody = ref([30, 29])<\/code><\/pre>\n<h5>\u98df\u7269<\/h5>\n<p>\u98df\u7269\u7684\u751f\u6210\u5176\u5b9e\u4e5f\u5c31\u662f\u968f\u673a\u4e00\u4e2a\u5750\u6807(\u7d22\u5f15),\u53ea\u4e0d\u8fc7\u8981\u6ce8\u610f\u7684\u662f,\u6211\u4eec\u9700\u8981\u907f\u5f00\u86c7\u8eab\u672c\u8eab\u7684\u5750\u6807.<\/p>\n<pre><code class=\"js\">const foodIcon = '\ud83c\udf57'\n\/\/ \u98df\u7269\u5750\u6807\nconst foodCoord = ref(77)\n\n\/\/ \u751f\u6210\u98df\u7269\nfunction generateFood() {\n  const food = Math.floor(Math.random() * basic * basic)\n  \/\/ \u4e0e\u86c7\u8eab\u51b2\u7a81,\u91cd\u65b0\u751f\u6210\n  if (snakeBody.value.includes(food)) {\n    generateFood()\n    return\n  }\n  foodCoord.value = food\n}<\/code><\/pre>\n<p>\u521d\u59cb\u5316\u540e\u7684\u5143\u7d20\u957f\u8fd9\u6837 \ud83d\udc47 :<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042431222\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h4>\u86c7\u7684\u722c\u884c<\/h4>\n<p>\u86c7\u7684\u722c\u884c\u903b\u8f91\u6709\u4e24\u4e2a\u57fa\u7840\u5143\u7d20,\u65b9\u5411 + \u6b65\u6570.\u524d\u9762\u6211\u4eec\u63d0\u5230\u4e86\u7ade\u6280\u53f0\u7684\u7ec4\u6210\u662f\u4e00\u4e2a28*28\u7684\u884c\u5217\u5f0f\u7ed3\u6784,\u90a3\u4e48\u5173\u4e8e\u65b9\u5411\u548c\u6b65\u6570\u7684\u6620\u5c04,\u5c31\u6bd4\u8f83\u6e05\u6670\u4e86:<\/p>\n<pre><code class=\"js\">const map = {\n left: -1,\n right: 1,\n top: -28,\n bottom: 28\n}<\/code><\/pre>\n<p>\u6709\u4e86\u4e24\u4e2a\u57fa\u672c\u5143\u7d20,\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u51fa\u6211\u4eec\u6bcf\u4e00\u6b21\u722c\u884c\u7684\u4e0b\u4e00\u4e2a\u5750\u6807.\u6211\u4eec\u53ea\u9700\u8981\u5728\u6bcf\u6b21\u722c\u884c\u7684\u65f6\u5019\u5f80\u86c7\u5934\u6dfb\u52a0\u5bf9\u5e94\u7684\u5750\u6807,\u5e76\u79fb\u9664\u86c7\u5c3e\u6240\u5728\u7684\u5750\u6807\u5c31\u53ef\u4ee5\u8fbe\u5230\u86c7\u722c\u884c\u7684\u6548\u679c.<\/p>\n<pre><code class=\"js\">\nfunction move() {\n  const h = snakeBody.value[0]\n  \/\/ \u8ba1\u7b97\u4e0b\u4e00\u6b21\u722c\u884c\u5750\u6807,\u5e76\u6dfb\u52a0\u81f3\u86c7\u5934\n  head.value = h + direction.value\n  snakeBody.value.unshift(head.value)\n\n  \/\/ \u5403\u5230\u98df\u7269,\u91cd\u65b0\u751f\u6210\n  if (head.value === foodCoord.value) {\n    generateFood()\n  }\n  \/\/ \u53ea\u6709\u5728\u672a\u5403\u5230\u98df\u7269\u7684\u65f6\u5019,\u624d\u9700\u8981\u79fb\u9664\u86c7\u5c3e\n  else { snakeBody.value.pop() }\n}<\/code><\/pre>\n<h4>\u8d8a\u754c\u903b\u8f91<\/h4>\n<p>\u8d2a\u5403\u86c7\u7684\u6e38\u620f\u7ed3\u675f\u89c4\u5219\u5224\u65ad\u5c31\u662f\u722c\u884c\u65f6\u86c7\u5934\u8d8a\u754c(\u8fd9\u91cc\u7684\u754c\u9650\u6307\u7684\u662f\u8d85\u51fa\u7ade\u6280\u53f0\u7684\u8303\u56f4)\u6216\u8005\u78b0\u5230\u86c7\u8eab.<\/p>\n<pre><code class=\"js\">function isOutOfRange(h: number) {\n    \/\/ 1. \u86c7\u5934\u78b0\u5230\u86c7\u8eab\n  return snakeBody.value.indexOf(h, 1) &gt; 0\n    \/\/ 2. \u86c7\u5934\u8d85\u51fa\u7ade\u6280\u53f0\u4e0a\u65b9\n    || h &lt; 0\n    \/\/ 3. \u86c7\u5934\u8d85\u51fa\u7ade\u6280\u53f0\u4e0b\u65b9\n    || h &gt; basic * basic - 1\n    \/\/ 4. \u86c7\u5934\u8d85\u51fa\u7ade\u6280\u53f0\u53f3\u65b9\n    || (direction.value === 1 &amp;&amp; h % basic === 0)\n    \/\/ 5. \u86c7\u5934\u8d85\u51fa\u7ade\u6280\u53f0\u5de6\u65b9\n    || (direction.value === -1 &amp;&amp; h % basic === basic - 1)\n}<\/code><\/pre>\n<h4>\u65b9\u5411\u63a7\u5236<\/h4>\n<p>\u8d2a\u5403\u86c7\u6e38\u620f\u6838\u5fc3\u7684\u64cd\u4f5c\u903b\u8f91\u5728\u4e8e\u64cd\u7eb5\u86c7\u7684\u65b9\u5411\u8fdb\u884c\u98df\u7269\u7684\u6355\u6349.\u6240\u4ee5\u6211\u4eec\u9700\u8981\u505a\u7684\u5c31\u662f\u6355\u6349\u7528\u6237\u65b9\u5411\u952e\u7684\u8f93\u5165\u8fdb\u884c\u65b9\u5411\u7684\u5207\u6362.<a href=\"https:\/\/github.com\/webfansplz\/temir\">Temir<\/a>\u63d0\u4f9b\u4e86useInput\u51fd\u6570\u76d1\u542c\u7528\u6237\u7684\u8f93\u5165.<\/p>\n<pre><code class=\"js\">import { useInput } from '@temir\/core'\nuseInput(onKeyBoard, { isActive: true })\n\nfunction onKeyBoard(_, keys) {\n  const { upArrow, downArrow, leftArrow, rightArrow } = keys\n  const d = {\n    [+leftArrow]: -1,\n    [+rightArrow]: 1,\n    [+upArrow]: -basic,\n    [+downArrow]: basic,\n  }[1] ?? direction.value\n  direction.value = (snakeBody.value[1] - snakeBody.value[0] === d) ? direction.value : d\n}<\/code><\/pre>\n<h4>UI\u7ed8\u5236<\/h4>\n<p>\u5173\u4e8eUI\u7684\u7ed8\u5236\u4e0e\u5448\u73b0<a href=\"https:\/\/github.com\/webfansplz\/temir\">Temir<\/a>\u63d0\u4f9b\u4e86\u4e00\u4e9bVue\u7ec4\u4ef6,\u6211\u4eec\u53ea\u9700\u8981\u50cf\u6784\u5efaFlexbox\u5e03\u5c40\u90a3\u6837\u6784\u5efa\u7ec8\u7aefUI:<\/p>\n<pre><code class=\"html\">&lt;script lang=&quot;ts&quot; setup&gt;\nimport { computed } from 'vue'\nimport { TBox, TText } from '@temir\/core'\nimport { useGame } from '.\/composables'\nimport Header from '.\/components\/Header.vue'\nimport Home from '.\/components\/Home.vue'\nimport Game from '.\/components\/Game.vue'\nimport GameOver from '.\/components\/GameOver.vue'\nimport Exit from '.\/components\/Exit.vue'\nconst { playStatus } = useGame()\nconst activeComponent = computed(() =&gt; {\n  return {\n    unplayed: Home,\n    playing: Game,\n    over: GameOver,\n    exit: Exit,\n  }[playStatus.value]\n})\n&lt;\/script&gt;\n\n&lt;template&gt;\n  &lt;TBox\n    :width=&quot;100&quot;\n    justify-content=&quot;center&quot;\n    align-items=&quot;center&quot;\n    flex-direction=&quot;column&quot;\n    border-style=&quot;double&quot;\n  &gt;\n    &lt;Header \/&gt;\n    &lt;component :is=&quot;activeComponent&quot; \/&gt;\n  &lt;\/TBox&gt;\n&lt;\/template&gt;<\/code><\/pre>\n<p>\u5230\u8fd9\u91cc,\u8d2a\u5403\u86c7\u7684\u5b9e\u73b0\u5c31\u7ed3\u675f\u4e86,\u5bf9\u5177\u4f53\u5b9e\u73b0\u611f\u5174\u8da3\u7684\u53ef\u4ee5<a href=\"https:\/\/github.com\/webfansplz\/temir-snake-game\">\u6233\u6e90\u7801<\/a>\u67e5\u770b.<\/p>\n<h2>\u6f14\u793a<\/h2>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2cak\" alt=\"temir-snake-game.gif\" title=\"temir-snake-game.gif\" loading=\"lazy\"><\/p>\n<h2>\u7ed3\u8bed<\/h2>\n<p>\u5982\u679c\u6211\u7684\u6587\u7ae0\u548c\u9879\u76ee\u5bf9\u4f60\u6709\u6240\u542f\u53d1\u548c\u5e2e\u52a9,\u8bf7\u7ed9\u4e00\u4e2a<a href=\"https:\/\/github.com\/webfansplz\/temir-snake-game\">star<\/a>\u652f\u6301\u4f5c\u8005 \u270c!<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042431217\">https:\/\/segmentfault.com\/a\/1190000042431217<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5927\u5bb6\u597d,\u6211\u662fwebfansplz.\u672c\u6587\u8981\u5206\u4eab\u7684\u662f\u5982\u4f55\u4f7f\u7528Vue.js\u5b9e\u73b0\u4e00\u4e2a\u547d\u4ee4\u884c\u8d2a\u5403\u86c7\u6e38\u620f(temir [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006395","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006395"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006395\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}