{"id":31549,"student_id":10,"content":"// 設為 true 則顯示蘋果\ngame.displayApple = true;\n\n// 手動點擊畫面使鳥起飛\nwhen('click', bird.jump);\n\n// 可以在主控台中印出 Q 觀察看看\nvar Q = new Brain; // 鳥的大腦,記錄在不同的狀態下要做什麼反應(跳/不跳)\n\nvar S1; // 上一次的狀態\nvar A; // 上一次的行為\nvar S2; // 這次的狀態\nvar alpha = 0.3; // 學習速率\nvar beta = 0.8; // 獎勵延遲\n\nforever(function () {\n \n var apple = game.getApple(); // 取得眼前最近的蘋果\n S1 = getStatus(bird, apple);\n print(S1, 100, 10, 'white', 30);\n \n if (Q[S1][0] \u003c Q[S1][1]) {\n A = 1;\n bird.jump();\n } else {\n A = 0;\n }\n S2 = getStatus(bird, apple);\n\n if (bird.touched(ground) || bird.touched(pipes)) {\n game.restart();\n reward(S1, A, S2, -1000); //如果鳥死亡懲罰 -1000 分\n }\n\n if (bird.touched(apple)) {\n apple.hidden = true;\n reward(S1, A, S2, 50); //如果鳥吃到蘋果獎勵 +50 分\n } else {\n reward(S1, A, S2, 1); //如果鳥活著獎勵 +1 分\n }\n\n \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":"2018-06-12T15:28:08.668+08:00","updated_at":"2024-01-15T23:17:46.940+08:00","name":"flappy bird Q-learning","language":"javascript","screenshot":{"url":"https://cdn9.koding.school/uploads/project/screenshot/31549/b16eba536c8ec8f92b5a4290f41f0e93.jpg"},"parent_id":31540,"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();","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":372,"hashid":"6rpsqnk3","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":589981,"file_name":"apple.png","project_id":31549,"asset_id":63917,"created_at":"2018-06-12T15:28:08.675+08:00","updated_at":"2018-06-12T15:28:08.675+08:00"},{"id":589982,"file_name":"pipe.png","project_id":31549,"asset_id":57152,"created_at":"2018-06-12T15:28:08.685+08:00","updated_at":"2018-06-12T15:28:08.685+08:00"},{"id":589983,"file_name":"ground.png","project_id":31549,"asset_id":57151,"created_at":"2018-06-12T15:28:08.687+08:00","updated_at":"2018-06-12T15:28:08.687+08:00"},{"id":589984,"file_name":"bird.png","project_id":31549,"asset_id":57150,"created_at":"2018-06-12T15:28:08.689+08:00","updated_at":"2018-06-12T15:28:08.689+08:00"},{"id":589985,"file_name":"bg.png","project_id":31549,"asset_id":57149,"created_at":"2018-06-12T15:28:08.690+08:00","updated_at":"2018-06-12T15:28:08.690+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦