{"id":298764,"student_id":10,"content":"var clock = 0;\nvar snake = game.snake; // 蛇\nvar arrows = []; // 紀錄格子數字的二維陣列\nvar numbers = []; // 紀錄箭頭方向的二維陣列\n\n// 初始化格子箭頭與格子數字的二維陣列\nfor (var x = 0; x \u003c 20; x++) {\n arrows[x] = []\n numbers[x] = []\n for (var y = 0; y \u003c 15; y++) {\n arrows[x][y] = undefined\n numbers[x][y] = undefined\n }\n}\n\ndrawArrows(arrows); //繪製箭頭\ndrawNumbers(numbers); //繪製數字\nforever(gameloop); //不斷執行遊戲迴圈\nhamilton(); //創造漢米頓圈\ncreateNumbers(); //初始化格子數字\n\nfunction gameloop () {\n clock += 1\n if (clock % 60 == 0) {\n AI()\n if (key.up) snake.direction = 0\n if (key.down) snake.direction = 180\n if (key.right) snake.direction = 90\n if (key.left) snake.direction = 270\n snake.move()\n }\n}\n\n// 產生漢米頓圈\nfunction hamilton () {\n \n}\n\n// 依據箭頭方向將數字填入格子\nfunction createNumbers () {\n \n}\n\n// 判斷該格子是否可以作為捷徑\nfunction isShortCut (headNum, tailNum, foodNum, nextNum) {\n \n}\n\nfunction AI () {\n var head = snake.getHead()\n var tail = snake.getTail()\n var food = game.getFood()\n \n if (arrows[head.x][head.y] != undefined) {\n snake.direction = arrows[head.x][head.y]\n }\n \n var headNum = numbers[head.x][head.y]\n var tailNum = numbers[tail.x][tail.y]\n var foodNum = numbers[food.x][food.y]\n \n \n}\n","created_at":"2021-09-24T12:19:40.157+08:00","updated_at":"2021-09-28T14:20:49.083+08:00","name":"貪吃蛇 AI(預設版)","language":"javascript","screenshot":{"url":"/uploads/project/screenshot/298764/9f5fc4eacadd2cce711552f2e7e60080.jpg"},"parent_id":2,"plugin":"Game.set({ width: 1200, height: 900 })\n\nsetBackdrop('black')\n\nconst SIZE = 60\nconst W = 1200 / SIZE\nconst H = 900 / SIZE\n\nconst food = {\n x: Math.floor(Math.random() * W),\n y: Math.floor(Math.random() * H),\n}\n\n\nfunction createSnakeGame (bodies) {\n \n let snake = {\n direction: 90,\n bodies,\n }\n \n snake.move = function () {\n let head = bodies.pop()\n head.x = bodies[0].x\n head.y = bodies[0].y\n if (snake.direction === 0) head.y -= 1\n if (snake.direction === 90) head.x += 1\n if (snake.direction === 180) head.y += 1\n if (snake.direction === 270) head.x -= 1\n if (bodies.some(pos =\u003e pos.x === head.x \u0026\u0026 pos.y === head.y)) gameover()\n if (head.x \u003c 0 || head.y \u003c 0 || head.x \u003e= W || head.y \u003e= H) gameover()\n if (head.x === food.x \u0026\u0026 head.y === food.y) {\n bodies.push({ x: head.x, y: head.y })\n randomFoodPos(food, bodies)\n }\n bodies.unshift(head)\n }\n \n snake.getHead = function () {\n return bodies[0]\n }\n \n snake.getTail = function () {\n return bodies[bodies.length - 1]\n }\n \n function getFood () {\n return food\n }\n \n forever(function(){\n for (let i = 0; i \u003c bodies.length - 1; i++) {\n pen.color = 'green'\n pen.fillColor = 'green'\n let a = bodies[i]\n let b = bodies[i + 1]\n if (a.x \u003e b.x) pen.drawRect(a.x * SIZE - 10, a.y * SIZE + 10, SIZE, SIZE - 20)\n if (a.x \u003c b.x) pen.drawRect(a.x * SIZE + 10, a.y * SIZE + 10, SIZE, SIZE - 20)\n if (a.y \u003e b.y) pen.drawRect(a.x * SIZE + 10, a.y * SIZE - 10, SIZE - 20, SIZE)\n if (a.y \u003c b.y) pen.drawRect(a.x * SIZE + 10, a.y * SIZE + 10, SIZE - 20, SIZE)\n pen.drawRect(b.x * SIZE + 10, b.y * SIZE + 10, SIZE - 20, SIZE - 20)\n }\n pen.fillColor = 'red'\n pen.drawRect(food.x * SIZE + 10, food.y * SIZE + 10, SIZE - 20, SIZE - 20)\n })\n \n return {\n snake,\n getFood,\n }\n \n}\n\nfunction randomFoodPos (food, bodies) {\n let pos = []\n for (var x = 0; x \u003c 20; x++) {\n for (var y = 0; y \u003c 15; y++) {\n if (bodies.some(pos =\u003e pos.x === x \u0026\u0026 pos.y === y) == false) {\n pos.push({ x, y }) \n }\n }\n }\n if (pos.length === 0) return gameover()\n pos = pos[Math.floor(Math.random() * pos.length)]\n food.x = pos.x\n food.y = pos.y\n}\n\n\nfunction gameover () {\n stop()\n drawText('gameover', 10, 10, 'red', 60)\n}\n\n\nlet arrowSprites = []\nfor (var x = 0; x \u003c 20; x++) {\n arrowSprites[x] = []\n for (var y = 0; y \u003c 15; y++) {\n let arrow = createSprite('arrow.png')\n arrow.x = (x + 0.5) * SIZE\n arrow.y = (y + 0.5) * SIZE\n arrow.opacity = 0.5\n arrow.hidden = true\n arrowSprites[x][y] = arrow\n }\n}\n\nfunction drawNumbers (numbers) {\n forever(() =\u003e {\n for (var x = 0; x \u003c 20; x++) {\n for (var y = 0; y \u003c 15; y++) {\n if (numbers[x][y] \u003e= 0) {\n drawText(numbers[x][y], x * SIZE, y * SIZE, 'white', 24)\n }\n }\n } \n })\n}\n\nfunction drawArrows (arrows) {\n forever(() =\u003e {\n for (var x = 0; x \u003c 20; x++) {\n for (var y = 0; y \u003c 15; y++) {\n if ([0, 90, 180, 270].includes(arrows[x][y])) {\n arrowSprites[x][y].hidden = false\n arrowSprites[x][y].direction = arrows[x][y]\n } else {\n arrowSprites[x][y].hidden = true\n }\n }\n }\n })\n}\n\nvar game = createSnakeGame([\n {x: 8, y: 5},\n {x: 7, y: 5},\n {x: 6, y: 5},\n {x: 5, y: 5},\n])\n\n\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":355,"hashid":"p93skveqg","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":6024950,"file_name":"arrow.png","project_id":298764,"asset_id":418379,"created_at":"2021-09-24T13:49:50.773+08:00","updated_at":"2021-09-24T13:49:50.773+08:00"}]
Orange Apple Learning Platform
My Projects
View Project Page
Export
Remix
Import
Delete
Download Android APP (APK)
Screenshot
繁中
简中
English
日本語
1:1:1
1:1
Full Width
Scan the QRCode below with your phone to install
Or you can
download APK
to this computer
Scan the QRCode below with your phone to install
Or you can
download APK
to this computer