{"id":47172,"student_id":1440,"content":"//初始化\nvar godmode = false\nvar swith = false\nvar move = true\nvar end_x = 0\nvar end_y = 0\nvar list = []\nvar map = []\nvar x = 0\nvar y = 0\n\n//取得最大公因數\nvar a = 640\nvar b = 480\nwhile (a != b) {\n if (a \u003e b) {\n a -= b\n } else {\n b -= a\n }\n}\n\n//取得可以整除的數\nfor (var i = 0; i \u003c a+1; i++) {\n if (640%i == 0 \u0026\u0026 480%i == 0) {\n list.push(i)\n }\n}\nlist.sort(function(a, b) {\n return b-a\n})\n\n//設定難度\nvar difficulty = prompt(`難度要多少(1~${list.length})`)*1\n\nif (difficulty \u003c 1) {\n difficulty = 1\n}\nif (difficulty \u003e list.length) {\n difficulty = list.length\n}\n\n//設定格子大小\nvar lattice = list[difficulty-1]\n\n//創造空白地圖\nfor (var i = 0; i \u003c 640/lattice; i++) {\n map.push([])\n for (var j = 0; j \u003c 480/lattice; j++) {\n map[i].push({\n x: 0,\n y: 0,\n top: false,\n bottom: false,\n right: false,\n left: false,\n color: \"white\"\n })\n }\n}\n\n//去除不完整的方塊\nfor (var i = 0; i \u003c map.length; i++) {\n for (var j = 0; j \u003c map[i].length; j++) {\n if (i*lattice+lattice \u003e 640 || j*lattice+lattice \u003e 480) {\n map[i][j].color = \"black\"\n }\n }\n}\n\n//創造地圖\ncreate()\n\n//主程式\nforever(function() {\n if (swith) {\n draw_map()\n if (x == end_x \u0026\u0026 y == end_y) {\n pen.color = \"white\"\n pen.fillColor = \"white\"\n print(\"You End The Game\", (map[map.length-1][0].x+lattice)/2-50, (map[0][map[0].length-1].y+lattice)/2)\n pen.drawRect((map[map.length-1][0].x+lattice)/2-60, (map[0][map[0].length-1].y+lattice)/2-10, 160, 30)\n } else {\n if (move) {\n if (godmode) {\n map[end_x][end_y].color = \"white\"\n if (map[x][y].bottom \u0026\u0026 map[x][y+1].color == \"white\") {\n map[x][y+1].color = \"red\"\n y ++\n map[x][y-1].color = \"lightblue\"\n } else if (map[x][y].right \u0026\u0026 map[x+1][y].color == \"white\") {\n map[x+1][y].color = \"red\"\n x ++\n map[x-1][y].color = \"lightblue\"\n } else if (map[x][y].top \u0026\u0026 map[x][y-1].color == \"white\") {\n map[x][y-1].color = \"red\"\n y --\n map[x][y+1].color = \"lightblue\"\n } else if (map[x][y].left \u0026\u0026 map[x-1][y].color == \"white\") {\n map[x-1][y].color = \"red\"\n x --\n map[x+1][y].color = \"lightblue\"\n } else {\n map[x][y].color = \"yellow\"\n if (map[x][y].bottom \u0026\u0026 map[x][y+1].color == \"lightblue\") {\n map[x][y+1].color = \"red\"\n y ++\n map[x][y-1].color = \"yellow\"\n } else if (map[x][y].right \u0026\u0026 map[x+1][y].color == \"lightblue\") {\n map[x+1][y].color = \"red\"\n x ++\n map[x-1][y].color = \"yellow\"\n } else if (map[x][y].top \u0026\u0026 map[x][y-1].color == \"lightblue\") {\n map[x][y-1].color = \"red\"\n y --\n map[x][y+1].color = \"yellow\"\n } else if (map[x][y].left \u0026\u0026 map[x-1][y].color == \"lightblue\") {\n map[x-1][y].color = \"red\"\n x --\n map[x+1][y].color = \"yellow\"\n }\n }\n }\n if (key.up || key.w) {\n if (y \u003e 0) {\n if (map[x][y].top \u0026\u0026 map[x][y-1].color != \"black\") {\n map[x][y-1].color = \"red\"\n y--\n map[x][y+1].color = \"lightblue\"\n move = false\n }\n }\n }\n if (key.down || key.s) {\n if (y \u003c map[0].length) {\n if (map[x][y].bottom \u0026\u0026 map[x][y+1].color != \"black\") {\n map[x][y+1].color = \"red\"\n y++\n map[x][y-1].color = \"lightblue\"\n move = false\n }\n }\n }\n if (key.left || key.a) {\n if (x \u003e 0) {\n if (map[x][y].left \u0026\u0026 map[x-1][y].color != \"black\") {\n map[x-1][y].color = \"red\"\n x--\n map[x+1][y].color = \"lightblue\"\n move = false\n }\n }\n }\n if (key.right || key.d) {\n if (x \u003c map.length) {\n if (map[x][y].right \u0026\u0026 map[x+1][y].color != \"black\") {\n map[x+1][y].color = \"red\"\n x++\n map[x-1][y].color = \"lightblue\"\n move = false\n }\n }\n }\n } else {\n setTimeout(function() {\n move = true\n }, 100);\n }\n }\n } else {\n print(\"Loading \"+load()+\"%\", 640/2, 480/2)\n }\n})\n\n//取得隨機挖路的方向\nfunction getNeighbors (x, y) {\n var list = []\n if (!map[x][y].top \u0026\u0026 y \u003e 0 \u0026\u0026 map[x][y-1].color == \"white\") {\n list.push(\"top\")\n }\n if (!map[x][y].bottom \u0026\u0026 y \u003c map[0].length-1 \u0026\u0026 map[x][y+1].color == \"white\") {\n list.push(\"bottom\")\n }\n if (!map[x][y].left \u0026\u0026 x \u003e 0 \u0026\u0026 map[x-1][y].color == \"white\") {\n list.push(\"left\")\n }\n if (!map[x][y].right \u0026\u0026 x \u003c map.length-1 \u0026\u0026 map[x+1][y].color == \"white\") {\n list.push(\"right\")\n }\n return list[Math.floor(Math.random()*list.length)]\n}\n\n//創造地圖的移動指令\nfunction move_top() {\n map[x][y].top = true\n y--\n map[x][y].bottom = true\n}\n\nfunction move_bottom() {\n map[x][y].bottom = true\n y++\n map[x][y].top = true\n}\n\nfunction move_left() {\n map[x][y].left = true\n x--\n map[x][y].right = true\n}\n\nfunction move_right() {\n map[x][y].right = true\n x++\n map[x][y].left = true\n}\n\n//載入百分比計算\nfunction load() {\n complete = 0\n for (var i = 0; i \u003c map.length; i++) {\n for (var j = 0; j \u003c map[i].length; j++) {\n if (map[i][j].color == \"yellow\" || map[i][j].color == \"black\") {\n complete++\n }\n }\n }\n return Math.floor([(complete*100)/(map.length*map[0].length)]*100)/100\n}\n\n//創造地圖\nfunction create() {\n map[x][y].color = \"lightblue\"\n random = getNeighbors (x, y)\n if (random == \"right\") {\n move_right()\n }\n if (random == \"left\") {\n move_left()\n }\n if (random == \"top\") {\n move_top()\n }\n if (random == \"bottom\") {\n move_bottom()\n }\n if (random == undefined) {\n map[x][y].color = \"yellow\"\n if (map[x][y].right \u0026\u0026 map[x+1][y].color == \"lightblue\") {\n x++\n } else if (map[x][y].left \u0026\u0026 map[x-1][y].color == \"lightblue\") {\n x--\n } else if (map[x][y].top \u0026\u0026 map[x][y-1].color == \"lightblue\") {\n y--\n } else if (map[x][y].bottom \u0026\u0026 map[x][y+1].color == \"lightblue\") {\n y++\n }\n }\n\n if (load() == 100) {\n swith = true\n for (var i = 0; i \u003c map.length; i++) {\n for (var j = 0; j \u003c map[i].length; j++) {\n if (map[i][j].color != \"black\") {\n map[i][j].color = \"white\"\n }\n }\n }\n map[x][y].color = \"red\"\n while (end_x \u003c map.length/2 || end_y \u003c map[0].length/2) {\n if (640%lattice == 0 \u0026\u0026 480%lattice == 0) {\n end_x = Math.floor(Math.random()*map.length)\n end_y = Math.floor(Math.random()*map[0].length)\n } else {\n end_x = Math.floor(Math.random()*(map.length-1))\n end_y = Math.floor(Math.random()*(map[0].length-1))\n }\n\n }\n map[end_x][end_y].color = \"green\"\n } else {\n setTimeout(create)\n }\n}\n\n//畫地圖\nfunction draw_map() {\n //noprotect\n for (var i = 0; i \u003c map.length; i++) {\n for (var j = 0; j \u003c map[i].length; j++) {\n map[i][j].x = i*lattice\n map[i][j].y = j*lattice\n pen.color = map[i][j].color\n pen.fillColor = map[i][j].color\n if (map[i][j].color == \"red\") {\n pen.drawRect(x*lattice+0.5, y*lattice+0.5, lattice-2, lattice-2)\n } else {\n pen.drawRect(map[i][j].x, map[i][j].y, lattice, lattice)\n }\n pen.color = \"black\"\n pen.fillColor = \"black\"\n if (!map[i][j].top) {\n pen.drawRect(map[i][j].x, map[i][j].y, lattice, 0.1)\n }\n if (!map[i][j].bottom) {\n pen.drawRect(map[i][j].x, map[i][j].y+lattice, lattice, 0.1)\n }\n if (!map[i][j].left) {\n pen.drawRect(map[i][j].x, map[i][j].y, 0.1, lattice)\n }\n if (!map[i][j].right) {\n pen.drawRect(map[i][j].x+lattice, map[i][j].y, 0.1, lattice)\n }\n }\n }\n}","created_at":"2018-10-29T13:38:55.931+08:00","updated_at":"2019-11-02T15:22:39.522+08:00","name":"走迷宮(bata)","language":"javascript","screenshot":{"url":"https://cdn0.koding.school/uploads/project/screenshot/47172/793b56fc324e34324a02eb03c32e59fa.jpg"},"parent_id":2,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":102,"hashid":"882seqzd","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[]
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
繁中
简中
English
日本語
1:1:1
1:1
全寬
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦