{"id":196467,"student_id":1909,"content":"// 設為 true 則顯示蘋果\ngame.displayApple = true;\n\n// 手動點擊畫面使鳥起飛\nwhen('click', bird.jump);\n\n// 可以在主控台中印出 Q 觀察看看\n// 鳥的大腦,記錄在不同的狀態下要做什麼反應(跳/不跳)\nvar Q = new Brain; \n\nvar S1; // 上一次的狀態\nvar A; // 上一次的行為\nvar S2; // 這次的狀態\nvar alpha = 0.3; // 學習速率\nvar beta = 0.8; // 獎勵延遲\n\nforever(function () {\n var apple = game.getApple()\n S2 = getStatus(bird,apple)\n print(S2,100,10,'white',30)\n \n if (apple.touched(bird)) {\n apple.hidden = true\n reward(S1,A,S2,50)\n \n }\n if (bird.touched(ground)||bird.touched(pipes)) {\n reward(S1,A,S2,-1000)\n game.restart()\n }else{\n reward(S1,A,S2,1)\n }\n \n if (Q[S2][0]\u003cQ[S2][1]) {\n A = 1\n bird.jump()\n } else {\n A = 0\n }\n S1 = S2\n});\n\n// 根據鳥與蘋果的相對位置來作為「狀態」\nfunction getStatus (bird, apple) {\n var x = apple.X - bird.X\n var y = apple.Y - bird.Y\n return x+\":\"+y\n}\n\n// 獎勵懲罰來更新 Q 大腦\nfunction reward (S1, A, S2, R) {\n var mr = Math.max(Q[S2][0], Q[S2][1])\n Q[S1][A] = Q[S1][A]*(1-alpha)+alpha*(R+beta*mr)\n}\n","created_at":"2020-10-17T16:48:03.722+08:00","updated_at":"2020-10-17T17:43:26.894+08:00","name":"flappy bird Q-learning 副本","language":"javascript","screenshot":{"url":"https://cdn0.koding.school/uploads/project/screenshot/196467/593e893c97f76ab2c8fc16dd2264ab23.jpg"},"parent_id":65053,"plugin":"setBackdrop('bg.png');\n\n// Brain\nfunction Brain () {\n var w = 16;\n var h = 32;\n var obj = {};\n obj['undefined'] = [0, 0];\n for (var x = -1; x \u003c w + 1; x++) {\n for (var y=-h; y \u003c h; y++) {\n obj[x + ':' + y] = [0, 0];\n }\n }\n return obj;\n}\n\nvar game = (() =\u003e {\n\n let points = 0; // 分數\n let speed = 3; // 速度\n let space = 720/3; // 720/3 為三組水管之間的間隔\n let targetApple; // 鳥前面最近的蘋果\n let gravity = 0.25; // 引力\n let gameStatus = 'playing'; // 遊戲狀態 playing: 進行中 stop: 死亡\n \n let apple1 = createSprite('apple.png');\n let apple2 = createSprite('apple.png');\n let apple3 = createSprite('apple.png');\n let apples = [apple1, apple2, apple3];\n\n let pipe1 = createSprite('pipe.png');\n let pipe2 = createSprite('pipe.png');\n let pipe3 = createSprite('pipe.png');\n let pipe4 = createSprite('pipe.png');\n let pipe5 = createSprite('pipe.png');\n let pipe6 = createSprite('pipe.png');\n pipe2.direction = -90;\n pipe4.direction = -90;\n pipe6.direction = -90;\n\n let ground1 = createSprite('ground.png');\n let ground2 = createSprite('ground.png');\n ground1.y = 450;\n ground2.y = 450;\n\n let bird = createSprite('bird.png');\n bird.x = 200;\n bird.vy = 0;\n bird.scale = 0.8;\n \n bird.jump = function () {\n bird.vy = -4.5;\n }\n\n // bird.on('touch', [pipe1, pipe2, pipe3, pipe4, pipe5, pipe6, ground1, ground2], () =\u003e gameStatus = 'stop');\n\n forever(()=\u003e{\n\n // Sprite.x -\u003e Sprite.x\n [bird, apple1, apple2, apple3].forEach(function(s) {\n s.X = Math.floor(s.x/15);\n s.Y = Math.floor(s.y/15);\n });\n\n game.gameStatus = gameStatus; // suck\n\n pipe1.moveTo(apple1);\n pipe1.y -= 270;\n pipe1.x -= 25;\n pipe2.moveTo(apple1);\n pipe2.y += 270;\n pipe2.x -= 25;\n\n pipe3.moveTo(apple2);\n pipe3.y -= 270;\n pipe3.x -= 25;\n pipe4.moveTo(apple2);\n pipe4.y += 270;\n pipe4.x -= 25;\n \n pipe5.moveTo(apple3);\n pipe5.y -= 270;\n pipe5.x -= 25;\n pipe6.moveTo(apple3);\n pipe6.y += 270;\n pipe6.x -= 20;\n\n if (gameStatus === 'playing') {\n\n bird.vy += gravity;\n bird.y += bird.vy;\n \n ground1.x -= speed;\n ground2.x -= speed;\n if (ground1.x \u003c -320) ground1.x += 1280;\n if (ground2.x \u003c -320) ground2.x += 1280;\n\n apples.forEach((apple) =\u003e {\n apple.x -= speed;\n apple.opacity = .3;\n \n if (apple.x \u003c -30) {\n apple.x += space*3;\n apple.y = Math.random() * 150 + 110;\n apple.hidden = false;\n };\n });\n }\n \n // 取的眼前最近的蘋果\n targetApple = apples\n .filter((a) =\u003e { return a.x \u003e bird.x })\n .sort((a1, a2) =\u003e { return a1.x - a2.x })[0];\n \n targetApple.opacity = 1;\n \n if (targetApple.x \u003e bird.x \u0026\u0026 targetApple.x - bird.x \u003c speed*2) {\n points++;\n }\n \n if (game.displayApple == false) {\n apple1.opacity = 0;\n apple2.opacity = 0;\n apple3.opacity = 0;\n }\n\n print(points, 10, 10, 'white', 45);\n });\n\n function restart() {\n bird.y = 240;\n bird.vy = 0;\n apple1.x = space*1 + 200;\n apple1.y = Math.random() * 150 + 110\n apple2.x = space*2 + 200;\n apple2.y = Math.random() * 150 + 110\n apple3.x = space*3 + 200;\n apple3.y = Math.random() * 150 + 110\n points = 0;\n apple1.hidden = false;\n apple2.hidden = false;\n apple3.hidden = false;\n ground1.x = 320;\n ground2.x = 320 + 640;\n gameStatus = 'playing';\n }\n\n function getApple () {\n return targetApple;\n }\n\n return {\n restart: restart,\n bird: bird,\n getApple: getApple,\n displayApple: false,\n pipes: [pipe1, pipe2, pipe3, pipe4, pipe5, pipe6],\n ground: [ground1, ground2],\n }\n})();\n\nvar bird = game.bird;\nvar ground = game.ground;\nvar pipes = game.pipes;\ngame.restart();\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":59,"hashid":"rdvs6pd8r","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":3370271,"file_name":"bg.png","project_id":196467,"asset_id":117221,"created_at":"2020-10-17T16:48:03.734+08:00","updated_at":"2020-10-17T16:48:03.734+08:00"},{"id":3370272,"file_name":"bird.png","project_id":196467,"asset_id":117222,"created_at":"2020-10-17T16:48:03.735+08:00","updated_at":"2020-10-17T16:48:03.735+08:00"},{"id":3370273,"file_name":"ground.png","project_id":196467,"asset_id":117223,"created_at":"2020-10-17T16:48:03.736+08:00","updated_at":"2020-10-17T16:48:03.736+08:00"},{"id":3370274,"file_name":"pipe.png","project_id":196467,"asset_id":117224,"created_at":"2020-10-17T16:48:03.737+08:00","updated_at":"2020-10-17T16:48:03.737+08:00"},{"id":3370275,"file_name":"apple.png","project_id":196467,"asset_id":117225,"created_at":"2020-10-17T16:48:03.749+08:00","updated_at":"2020-10-17T16:48:03.749+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦