{"id":68694,"student_id":1976,"content":"// 設為 true 則顯示蘋果\ngame.displayApple = true;\n\n// 手動點擊畫面使鳥起飛\nwhen('click', bird.jump);\n\n// 可以在主控台中印出 Q 觀察看看\n// 鳥的大腦,記錄在不同的狀態下要做什麼反應(跳/不跳)\nvar Q = new Brain; \nconsole.log(Q)\nvar S1; // 上一次的狀態\nvar A; // 上一次的行為\nvar S2; // 這次的狀態\nvar alpha = 0.3; // 學習速率\nvar beta = 0.8; // 獎勵延遲\n\nforever(function () {\n \n var apple = game.getApple()\n S2 = getStatus(bird, apple) // S1=\u003e 上一個狀態, S2: 現在的狀態\n print(S1,100,10,'white',30) \n \n if(bird.touched(pipes) || bird.touched(ground)) {\n game.restart()\n reward(S1, A, S2, -1000)\n }else if(bird.touched(apple)) {\n apple.hidden = true\n reward(S1, A, S2, 50)\n } else {\n reward(S1, A, S2, 1) \n }\n \n \n if(Q[S2][0] \u003c Q[S2][1]) {\n bird.jump()\n A = 1\n }else {\n A = 0\n }\n \n S1 = S2\n \n \n });\n\n// 根據鳥與蘋果的相對位置來作為「狀態」\nfunction getStatus (bird, apple) {\n\n var x = apple.X - bird.X\n var y = apple.Y - bird.Y\n\n return x + ':' + y\n}\n\n// 獎勵懲罰來更新 Q 大腦\nfunction reward (S1, A, S2, R) {\n Q[S1][A] = (1-alpha) * Q[S1][A] + alpha * (R + beta * Math.max(Q[S2][0], Q[S2][1]))\n}\n","created_at":"2019-05-18T18:40:08.605+08:00","updated_at":"2019-11-12T07:30:47.245+08:00","name":"flappy bird Q-learning 副本","language":"javascript","screenshot":{"url":"https://cdn2.koding.school/uploads/project/screenshot/68694/f6da7cbb0cde339817c6fe0039d3b9a3.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":438,"hashid":"5j3s542j","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":1203817,"file_name":"bg.png","project_id":68694,"asset_id":117221,"created_at":"2019-05-18T18:40:08.610+08:00","updated_at":"2019-05-18T18:40:08.610+08:00"},{"id":1203818,"file_name":"bird.png","project_id":68694,"asset_id":117222,"created_at":"2019-05-18T18:40:08.611+08:00","updated_at":"2019-05-18T18:40:08.611+08:00"},{"id":1203819,"file_name":"ground.png","project_id":68694,"asset_id":117223,"created_at":"2019-05-18T18:40:08.612+08:00","updated_at":"2019-05-18T18:40:08.612+08:00"},{"id":1203820,"file_name":"pipe.png","project_id":68694,"asset_id":117224,"created_at":"2019-05-18T18:40:08.613+08:00","updated_at":"2019-05-18T18:40:08.613+08:00"},{"id":1203821,"file_name":"apple.png","project_id":68694,"asset_id":117225,"created_at":"2019-05-18T18:40:08.614+08:00","updated_at":"2019-05-18T18:40:08.614+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦