{"id":134364,"student_id":10,"content":"\n\nforever(function(){\n\n for (var x = 0; x \u003c 32; x++) {\n for (var y = 0; y \u003c 24; y++) {\n var c = maze[x][y]\n if (c !== food) c.color = 'white';\n }\n }\n \n var arr = [];\n arr.push({x: x, y: y});\n \n while(arr.length \u003e 0) {\n var pos = arr.shift();\n var x = pos.x;\n var y = pos.y;\n var c = maze[x][y];\n \n if (maze[pos.x][pos.y] == food) break;\n \n if (y \u003e 1 \u0026\u0026 maze[x][y - 1].color == 'white' \u0026\u0026 c.up) {\n maze[x][y - 1].color = 'lightblue';\n maze[x][y - 1].from = c;\n arr.push({x: x, y: y - 1});\n }\n \n if (y \u003c 23 \u0026\u0026 maze[x][y + 1].color == 'white' \u0026\u0026 c.down) {\n maze[x][y + 1].color = 'lightblue';\n maze[x][y + 1].from = c;\n arr.push({x: x, y: y + 1});\n }\n \n if (x \u003e 1 \u0026\u0026 maze[x - 1][y].color == 'white' \u0026\u0026 c.left) {\n maze[x - 1][y].color = 'lightblue';\n maze[x - 1][y].from = c;\n arr.push({x: x - 1, y});\n }\n \n if (x \u003c 31 \u0026\u0026 maze[x + 1][y].color == 'white' \u0026\u0026 c.right) {\n maze[x + 1][y].color = 'lightblue';\n maze[x + 1][y].from = c;\n arr.push({x: x + 1, y});\n }\n \n }\n \n var tmp = food;\n while(tmp.from) {\n tmp = tmp.from;\n tmp.color = 'yellow';\n if (tmp == body[0]) break;\n }\n \n if (maze[x][y - 1].color == 'yellow') direction = 'up';\n if (maze[x][y + 1].color == 'yellow') direction = 'down';\n if (maze[x + 1][y].color == 'yellow') direction = 'left';\n if (maze[x - 1][y].color == 'yellow') direction = 'right';\n\n})","created_at":"2020-03-20T10:49:09.819+08:00","updated_at":"2020-03-20T12:05:11.340+08:00","name":"貪食蛇AI v2","language":"javascript","screenshot":{"url":"https://cdn3.koding.school/uploads/project/screenshot/134364/89edcce60b8890776f43b51e69f48ea4.jpg"},"parent_id":134320,"plugin":"// // noprotect\n// setBackdrop(\"black\");\n\n// var grid = {}\n// var width = 32;\n// var height = 24;\n\n// for (var x = -1; x \u003c= width; x++) {\n// grid[x] = {};\n// for (var y = -1; y \u003c= height; y++) {\n// grid[x][y] = {\n// color: 'white',\n// steps: 0,\n// from: undefined,\n// x,\n// y,\n// }\n// }\n// }\n\n// for (var x = 0; x \u003c width; x++) {\n// grid[x][-1] = grid[x][height - 1]\n// grid[x][height] = grid[x][0]\n// }\n\n// for (var y = 0; y \u003c height; y++) {\n// grid[-1][y] = grid[width - 1][y]\n// grid[width][y] = grid[0][y]\n// }\n\n// for (var x = 0; x \u003c width; x++) {\n// for (var y = 0; y \u003c height; y++) {\n// grid[x][y].up = grid[x][y - 1];\n// grid[x][y].down = grid[x][y + 1];\n// grid[x][y].left = grid[x - 1][y];\n// grid[x][y].right = grid[x + 1][y];\n// }\n// }\n\n\n\n// var body = []\n// var direction = 'right';\n// var clock = 0;\n// var food = grid[5][5];\n\n// body.push(grid[9][0]);\n// body.push(grid[8][0]);\n// body.push(grid[7][0]);\n// body.push(grid[6][0]);\n// body.push(grid[5][0]);\n// body.push(grid[4][0]);\n// body.push(grid[3][0]);\n// body.push(grid[2][0]);\n// body.push(grid[1][0]);\n// body.push(grid[0][0]);\n\n// forever(function() {\n\n// for (var x = 0; x \u003c width; x++) {\n// for (var y = 0; y \u003c height; y++) {\n// var c = grid[x][y];\n// pen.fillColor = c.color;\n// pen.drawRect(c.x*20, c.y*20, 20, 20);\n// }\n// }\n\n\n// // setBackdrop(\"black\");\n// body.forEach(function (b) {\n// // pen.fillColor = 'black';\n// // pen.drawRect(b.x*20, b.y*20, 20, 20);\n// b.color = 'black';\n// });\n\n// pen.fillColor = 'red';\n// pen.drawRect(food.x*20, food.y*20, 20, 20);\n\n// if (key.up) direction = 'up';\n// if (key.down) direction = 'down';\n// if (key.left) direction = 'left';\n// if (key.right) direction = 'right';\n\n// clock++;\n// if (clock%2 == 0) {\n// body.pop();\n// if (direction == 'up') body.unshift(body[0].up);\n// if (direction == 'down') body.unshift(body[0].down);\n// if (direction == 'left') body.unshift(body[0].left);\n// if (direction == 'right') body.unshift(body[0].right);\n\n// if (body[0] === food) {\n// body.unshift(food);\n// var randX = Math.floor(Math.random()*32);\n// var randY = Math.floor(Math.random()*24);\n// food = grid[randX][randY];\n// }\n// }\n\n\n// })\n\n\nvar maze = {};\nvar width = 20;\nvar row = 640/width;\nvar col = 480/width;\n\nvar x = 8;\nvar y = 5;\nvar direction = 'right';\nvar body = [];\n\n(function () {\n for (var x = 0; x \u003c row; x++) {\n maze[x] = {};\n for (var y = 0; y \u003c col; y++) {\n maze[x][y] = {\n color: '#ffffff',\n up: true,\n right: true,\n down: true,\n left: true\n };\n }\n }\n\n forever(function() {\n for (var cx = 0; cx \u003c row; cx++) {\n for (var cy = 0; cy \u003c col; cy++) {\n\n var cell = maze[cx][cy];\n pen.size = 0.001;\n pen.fillColor = cell.color;\n pen.drawRect(cx*width, cy*width, width, width);\n\n pen.size = 1;\n var x = cx*width;\n var y = cy*width;\n if (!cell.up) pen.drawLine(x, y, x + width, y);\n if (!cell.right) pen.drawLine(x + width, y, x + width, y + width);\n if (!cell.down) pen.drawLine(x, y + width, x + width, y + width);\n if (!cell.left) pen.drawLine(x, y + width, x, y);\n }\n }\n });\n\n body = [maze[5][5], maze[6][5], maze[7][5], maze[8][5]];\n body.forEach(function (b) {\n b.up = false;\n b.down = false;\n b.left = false;\n b.right = false;\n });\n\n})();\n\nvar food = maze[20][20];\nfood.color = 'red';\n\n\nvar clock = 0;\nforever(function () {\n \n \n \n clock++;\n if (clock % 10 !== 0) return;\n\n if (direction == 'up') y--;\n if (direction == 'down') y++;\n if (direction == 'left') x--;\n if (direction == 'right') x++;\n \n if (x \u003e= 32) x -= 32;\n if (y \u003e= 24) y -= 24;\n if (x \u003c 0) x += 32;\n if (y \u003c 0) y += 24;\n \n maze[x][y].up = false;\n maze[x][y].down = false;\n maze[x][y].left = false;\n maze[x][y].right = false;\n\n body.push(maze[x][y]);\n \n if (food == maze[x][y]) {\n body.push(maze[x][y]);\n food.color = 'white';\n \n var randX = Math.floor(Math.random()*32);\n var randY = Math.floor(Math.random()*24);\n food = maze[randX][randY]\n food.color = 'red';\n }\n \n var tail = body.shift();\n tail.up = true;\n tail.down = true;\n tail.right = true;\n tail.left = true;\n})","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":251,"hashid":"npmsvyvr","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":2309164,"file_name":"earth.png","project_id":134364,"asset_id":182916,"created_at":"2020-03-20T10:49:09.824+08:00","updated_at":"2020-03-20T10:49:09.824+08:00"},{"id":2309165,"file_name":"space_bg.jpg","project_id":134364,"asset_id":182917,"created_at":"2020-03-20T10:49:09.825+08:00","updated_at":"2020-03-20T10:49:09.825+08:00"},{"id":2309166,"file_name":"koding.png","project_id":134364,"asset_id":182918,"created_at":"2020-03-20T10:49:09.826+08:00","updated_at":"2020-03-20T10:49:09.826+08:00"},{"id":2309167,"file_name":"Gymnopedie_No_1.mp3","project_id":134364,"asset_id":182919,"created_at":"2020-03-20T10:49:09.827+08:00","updated_at":"2020-03-20T10:49:09.827+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦