{"id":48667,"student_id":10,"content":"var player = Player('yellow.png', 1, 1, 'down', 'player');\n\nforever(function(){\n if (key.up) player.next = 'up';\n if (key.down) player.next = 'down';\n if (key.right) player.next = 'right';\n if (key.left) player.next = 'left';\n});\n\n// var blinky = Player('red.png', 26, 1, 'down');\n// var pinky = Player('pink.png', 10, 1, 'down');\n// var inky = Player('blue.png', 10, 1, 'down');\n\n// blinky.ai = function () {\n// blinky.next = searchPath(blinky.target, player.target);\n// }\n\n\n\n// pinky.ai = function () {\n// let nextTarget = player.target[player.next];\n// for (let i=0; i\u003c4; i++) {\n// if (nextTarget[player.next]) {\n// nextTarget = nextTarget[player.next];\n// }\n// }\n// pinky.next = searchPath(blinky.target, nextTarget);\n// }\n\n// inky.ai = function () {\n// let tx = player.x - blinky.x + player.x;\n// let ty = player.y - blinky.y + player.y;\n// let best;\n// let bestDistance = 10000000;\n// for (var x=0; x\u003c28; x++) {\n// for (var y=0; y\u003c31; y++) {\n// if (grid[x][y].role === 'food') {\n// d = grid[x][y].distanceTo(player);\n// if (d \u003c bestDistance) {\n// best = grid[x][y];\n// bestDistance = d;\n// }\n// }\n// } \n// }\n// inky.next = searchPath(inky.target, best);\n// }\n\n\n// function searchPath (from, to) {\n// var tmp = from.direction;\n// from.toward(to);\n// var d = from.direction;\n// from.direction = tmp;\n \n// if (d \u003e= 45 \u0026\u0026 d \u003c 135) return 'right';\n// if (d \u003e= 135 \u0026\u0026 d \u003c 225) return 'down';\n// if (d \u003e= 225 \u0026\u0026 d \u003c 315) return 'left';\n// return 'up';\n \n// }\n\n// function closest (arr, target) {\n \n// var best;\n// var bestDiff;\n \n// for (var i=0; i\u003carr.length; i++) {\n// var a = target;\n// var b = arr[i];\n \n// var diff = Math.floor(a - b);\n// if (diff \u003e 180) diff = 360 - diff;\n \n// if (best == undefined || diff \u003c bestDiff) {\n// best = arr[i];\n// bestDiff = diff;\n// }\n// }\n// return best;\n// }\n\n// function bestPath (from, to) {\n// let arr = [from];\n// let history = [from];\n \n// let count=0;\n// while (arr.length \u003e 0) {\n// count++;\n// let b = arr.shift();\n \n// if (b === to) break;\n \n// if (b.up \u0026\u0026 history.indexOf(b.up) === -1) {\n// arr.push(b.up);\n// b.up.from = b;\n// history.push(b.up);\n// }\n// if (b.right \u0026\u0026 history.indexOf(b.right) === -1) {\n// arr.push(b.right);\n// b.right.from = b;\n// history.push(b.right);\n// }\n// if (b.left \u0026\u0026 history.indexOf(b.left) === -1) {\n// arr.push(b.left);\n// b.left.from = b;\n// history.push(b.left);\n// }\n// if (b.down \u0026\u0026 history.indexOf(b.down) === -1) {\n// arr.push(b.down);\n// b.down.from = b;\n// history.push(b.down);\n// }\n// }\n// console.log(count);\n \n// var end = to;\n// var last;\n// while(end.from) {\n// last = end;\n// end = end.from;\n// }\n// if (last.x \u003e end.x) return 'right';\n// if (last.x \u003c end.x) return 'left';\n// if (last.y \u003e end.y) return 'down';\n// if (last.y \u003c end.y) return 'up';\n// }\n","created_at":"2018-11-12T14:59:03.598+08:00","updated_at":"2019-11-07T16:52:30.980+08:00","name":"pacman","language":"javascript","screenshot":{"url":"https://cdn3.koding.school/uploads/project/screenshot/48667/76f38e21c50acbcda559768dfdbf1d39.jpg"},"parent_id":2,"plugin":"setBackdrop(\"black\");\n\nlet map = [\n 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',\n 'x............xx............x',\n 'x.xxxx.xxxxx.xx.xxxxx.xxxx.x',\n 'x.xxxx.xxxxx.xx.xxxxx.xxxx.x',\n 'x.xxxx.xxxxx.xx.xxxxx.xxxx.x',\n 'x..........................x',\n 'x.xxxx.xx.xxxxxxxx.xx.xxxx.x',\n 'x.xxxx.xx.xxxxxxxx.xx.xxxx.x',\n 'x......xx....xx....xx......x',\n 'xxxxxx.xxxxx.xx.xxxxx.xxxxxx',\n 'xxxxxx.xxxxx.xx.xxxxx.xxxxxx',\n 'xxxxxx.xx xx.xxxxxx',\n 'xxxxxx.xx xxx xxx xx.xxxxxx',\n 'xxxxxx.xx x x xx.xxxxxx',\n ' x x ',\n 'xxxxxx.xx x x xx.xxxxxx',\n 'xxxxxx.xx xxxxxxxx xx.xxxxxx',\n 'xxxxxx.xx xx.xxxxxx',\n 'xxxxxx.xx xxxxxxxx xx.xxxxxx',\n 'xxxxxx.xx xxxxxxxx xx.xxxxxx',\n 'x............xx............x',\n 'x.xxxx.xxxxx.xx.xxxxx.xxxx.x',\n 'x.xxxx.xxxxx.xx.xxxxx.xxxx.x',\n 'x...xx.......xx.......xx...x',\n 'xxx.xx.xx.xxxxxxxx.xx.xx.xxx',\n 'xxx.xx.xx.xxxxxxxx.xx.xx.xxx',\n 'x......xx....xx....xx......x',\n 'x.xxxxxxxxxx.xx.xxxxxxxxxx.x',\n 'x.xxxxxxxxxx.xx.xxxxxxxxxx.x',\n 'x..........................x',\n 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',\n]\n\n\nvar grid = {}\nfor (let y=-1; y \u003c 31 + 1; y++) {\n grid[y] = {}\n}\n\nfor (var y=0; y \u003c 31; y++) {\n for (var x=0; x \u003c 28; x++) {\n var s;\n if (map[y][x] === 'x') {\n s = createSprite('block.png');\n s.role = 'block';\n }\n if (map[y][x] === '.') {\n s = createSprite('yellow.png');\n s.role = 'food';\n s.scale = 0.1;\n }\n if (map[y][x] === ' ') {\n s = createSprite('yellow.png');\n s.role = 'food';\n s.hidden = true;\n }\n s.X = x;\n s.Y = y;\n s.x = x*15 + 130;\n s.y = y*15 + 15;\n grid[x][y] = s;\n }\n}\n\n// connect\nfor (let x = 0; x \u003c 28; x++) {\n for (let y = 0; y \u003c 31; y++) {\n \n if (grid[x][y].role === 'block') continue;\n \n let count = 0;\n if (grid[x][y + 1] \u0026\u0026 grid[x][y + 1].role !== 'block') {\n grid[x][y].down = grid[x][y + 1];\n count++;\n }\n if (grid[x][y - 1] \u0026\u0026 grid[x][y - 1].role !== 'block') {\n grid[x][y].up = grid[x][y - 1];\n count++;\n }\n if (grid[x + 1][y] \u0026\u0026 grid[x + 1][y].role !== 'block') { \n grid[x][y].right = grid[x + 1][y];\n count++;\n }\n if (grid[x - 1][y] \u0026\u0026 grid[x - 1][y].role !== 'block') {\n grid[x][y].left = grid[x - 1][y];\n count++;\n }\n grid[x][y].count = count; // 岔路個數\n }\n}\n\n\nfunction Player (img, x, y, direction, role) {\n let player = createSprite(img);\n player.scale = 0.4;\n player.target = grid[x][y];\n player.moveTo(grid[x][y]);\n player.currentDirection = direction;\n player.next = undefined;\n player.role = role; // ghost, player\n \n player.forever(function(){\n \n if (player.distanceTo(player.target) \u003e 1) {\n player.toward(player.target);\n player.stepForward(1);\n } else {\n player.moveTo(player.target);\n \n if (player.target.X === 0 \u0026\u0026 player.target.Y === 14) {\n player.moveTo(grid[27][14]);\n player.target = grid[27][14];\n }\n else if (player.target.X === 27 \u0026\u0026 player.target.Y === 14) {\n player.moveTo(grid[0][14]);\n player.target = grid[0][14];\n }\n \n if (player.role == 'player' \u0026\u0026 player.target.role === 'food') {\n if (!player.target.hidden) {\n player.target.hidden = true;\n }\n }\n \n // 遇到岔路時進行決策\n if (player.target.count \u003e= 3 \u0026\u0026 player.ai) player.ai();\n \n // 如果可以轉彎\n if (player.next \u0026\u0026 player.target[player.next]) {\n player.target = player.target[player.next];\n player.currentDirection = player.next;\n player.next = undefined;\n }\n // 如果不能轉彎就繼續往前移動\n else if (player.target[player.currentDirection]) {\n player.target = player.target[player.currentDirection];\n }\n else {\n // 遇到轉角就自動轉彎\n if (player.target.count === 2) {\n if (player.currentDirection != 'right' \u0026\u0026 player.target.left) {\n player.target = player.target.left;\n player.currentDirection = 'left';\n }\n if (player.currentDirection != 'left' \u0026\u0026 player.target.right) {\n player.target = player.target.right;\n player.currentDirection = 'right';\n }\n if (player.currentDirection != 'down' \u0026\u0026 player.target.up) {\n player.target = player.target.up;\n player.currentDirection = 'up';\n }\n if (player.currentDirection != 'up' \u0026\u0026 player.target.down) {\n player.target = player.target.down;\n player.currentDirection = 'down';\n }\n }\n }\n }\n });\n \n return player;\n}\n\n\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":309,"hashid":"wdkspp9g","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":872914,"file_name":"yellow.png","project_id":48667,"asset_id":95983,"created_at":"2018-11-12T16:04:00.140+08:00","updated_at":"2018-11-12T16:04:00.140+08:00"},{"id":872915,"file_name":"orange.png","project_id":48667,"asset_id":95984,"created_at":"2018-11-12T16:04:00.143+08:00","updated_at":"2018-11-12T16:04:00.143+08:00"},{"id":872916,"file_name":"pink.png","project_id":48667,"asset_id":95985,"created_at":"2018-11-12T16:04:00.357+08:00","updated_at":"2018-11-12T16:04:00.357+08:00"},{"id":872917,"file_name":"red.png","project_id":48667,"asset_id":95986,"created_at":"2018-11-12T16:04:00.362+08:00","updated_at":"2018-11-12T16:04:00.362+08:00"},{"id":872918,"file_name":"blue.png","project_id":48667,"asset_id":95987,"created_at":"2018-11-12T16:04:00.579+08:00","updated_at":"2018-11-12T16:04:00.579+08:00"},{"id":872934,"file_name":"block.png","project_id":48667,"asset_id":95989,"created_at":"2018-11-12T16:21:03.342+08:00","updated_at":"2018-11-12T16:21:03.342+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦