{"id":198523,"student_id":1909,"content":"var DNA_LENGTH = 500; // 每個火箭的基因長度\nvar SIZE = 30; // 每一個世代的火箭個數\nvar rockets = []; // 存放所有的火箭\n\n// 初始化所有的火箭\nfor (var i = 0; i \u003c SIZE; i++) {\n var rocket = new Rocket();\n rockets.push(rocket);\n rocket.fire();\n rocket.gens = randomGens\n}\n\nforever(function () {\n if (key.up) rockets[0].vy -= 0.3;\n if (key.right) rockets[0].vx += 0.1;\n if (key.left) rockets[0].vx -= 0.1;\n});\n\n// 當所有的火箭都停止,執行nextGeneration 產生下一代火箭\nforever(function() {\n for (var i = 0; i \u003c rockets.length; i++) {\n if (rockets[i].active) return;\n }\n nextGeneration();\n});\n\n// 產生隨機的初始化基因\nfunction randomGens () {\n var gens = []\n for (var i = 0; i \u003c 30; i++) {\n gens.push(Math.floor(Math.random()*4))\n }\n return gens\n}\n\n// 產生下一個世代\nfunction nextGeneration () {\n for (var i = 0; i \u003c rockets.length; i++) {\n var rocket = rockets[1]\n rocket.score = Math.abs(rocket.vy)+Math.abs(rocket.x-platform.x)\n }\n rockets.sort(function(rocket1, rocket2) {\n return rocket1-rocket2\n })\n\n\n var parentA = rockets[0]\n var parentB = rockets[1]\n parentA.opacity = 1\n parentB.opacity = 1\n for (var i = 0; i \u003c SIZE; i++) {\n rockets[i].gens = crossover(parentA.gens, parentB.gens)\n rockets[i].fire()\n }\n\n}\n\n// 交配產生下一代基因\n// 每個基因有 2% 的機率突變,若不突變則各有 50% 的機率來自父親和母親\nfunction crossover (gensA, gensB) {\n var gensChild = []\n for (var i = 0; i \u003c DNA_LENGTH; i++) {\n if (Math.random() \u003e 0.02) {\n if (Math.random() \u003e 0.5) {\n gensChild.push(gensA[i])\n } else {\n gensChild.push(gensB[i])\n }\n }else{\n gensChild.push(Math.floor(Math.random()*4))\n }\n }\n\n\n\n\n\n\n return gensChild;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n}","created_at":"2020-10-24T16:26:59.162+08:00","updated_at":"2020-10-31T17:02:00.109+08:00","name":"火箭降落 AI 副本","language":"javascript","screenshot":{"url":"https://cdn1.koding.school/uploads/project/screenshot/198523/89edcce60b8890776f43b51e69f48ea4.jpg"},"parent_id":61788,"plugin":"var gravity = 0.08;\n\ncreateSprite('bg.png');\nvar platformA = createSprite('platform.png');\nvar platformB = createSprite('platform.png');\nplatformA.scale = 0.6;\nplatformB.scale = 0.6;\nplatformA.y = 430;\nplatformA.x = 90;\nplatformB.y = 430;\nplatformB.x = 550;\n\n// alias\nvar platform = platformB;\n\nvar fires = [];\n\nforever(function () {\n fires = fires.filter(function (obj) {\n if (obj.scale \u003c= 0) {\n obj.destroy();\n return false;\n }\n return true;\n });\n});\n\nfunction createFire (x, y, direction) {\n var fire = createSprite('fire.png');\n fire.moveTo(x, y);\n fire.direction = direction + Math.random()*30 - 15;\n fire.stepForward(30);\n fires.push(fire);\n fire.forever(function () {\n fire.stepForward(2);\n fire.scale -= 0.02;\n fire.opacity -= 0.02;\n })\n}\n\nfunction Rocket () {\n var rocket = createSprite('rocket.png');\n\n rocket.moveTo(platformA);\n rocket.y -= 50;\n rocket.gens = [];\n rocket.direction = 0;\n rocket.scale = 0.3;\n rocket.vx = 0;\n rocket.vy = 0;\n rocket.clock = 0;\n rocket.active = false;\n rocket.cache = {};\n rocket.layer = 1;\n\n rocket.fire = function () {\n rocket.moveTo(platformA);\n rocket.y -= 50;\n this.direction = 0;\n this.scale = 0.3;\n this.vx = 0;\n this.vy = 0;\n this.clock = 0;\n this.active = true;\n this.opacity = 1;\n }\n\n rocket.forever(function() {\n\n if (!rocket.active) return;\n\n rocket.direction = rocket.vx*10;\n rocket.isFire = (\n Math.abs(rocket.vx - rocket.cache.vx) \u003e 0.02 ||\n rocket.vy - gravity - rocket.cache.vy != 0\n )\n rocket.cache.vx = rocket.vx;\n rocket.cache.vy = rocket.vy;\n \n if (rocket.clock % 3 == 0 \u0026\u0026 rocket.isFire) {\n rocket.isFire = false;\n createFire(rocket.x, rocket.y, rocket.direction + 180);\n }\n\n var g = rocket.gens[rocket.clock];\n rocket.clock++;\n if (g == 0) rocket.vy -= 0.3;\n else if (g == 1) rocket.vx += 0.1;\n else if (g == 2) rocket.vx -= 0.1;\n\n if (Math.abs(rocket.vx) \u003c 0.01) {\n rocket.vx = 0;\n } else {\n rocket.vx += rocket.vx \u003e 0 ? -0.01 : 0.01;\n }\n\n rocket.vy += gravity;\n rocket.x += rocket.vx;\n rocket.y += rocket.vy;\n\n if (rocket.touched([platformA, platformB]) || rocket.y \u003e= 440) {\n rocket.active = false;\n var score = Math.abs(rocket.vy) + Math.abs(rocket.x - platformB.x);\n rocket.opacity = score \u003e 8 ? 0.1: 1;\n }\n })\n\n return rocket;\n}","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":48,"hashid":"6rps98yqz","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":3411574,"file_name":"bg.png","project_id":198523,"asset_id":114042,"created_at":"2020-10-24T16:26:59.167+08:00","updated_at":"2020-10-24T16:26:59.167+08:00"},{"id":3411575,"file_name":"platform.png","project_id":198523,"asset_id":114041,"created_at":"2020-10-24T16:26:59.168+08:00","updated_at":"2020-10-24T16:26:59.168+08:00"},{"id":3411576,"file_name":"rocket.png","project_id":198523,"asset_id":114040,"created_at":"2020-10-24T16:26:59.169+08:00","updated_at":"2020-10-24T16:26:59.169+08:00"},{"id":3411577,"file_name":"fire.png","project_id":198523,"asset_id":114039,"created_at":"2020-10-24T16:26:59.170+08:00","updated_at":"2020-10-24T16:26:59.170+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦