{"id":455161,"student_id":10,"content":"// // 設為 true 則顯示蘋果\ngame.displayApple = false;\n\nvar bird = game.bird; //鳥\nvar ground = game.ground; //地板\nvar pipes = game.pipes; //水管們\n\n// 可以在主控台中印出 Q 觀察看看\nvar Q = new Brain; // 鳥的大腦,記錄在不同的狀態下要做什麼反應(跳/不跳)\nQ.display = false;\n\nvar alpha = 0.3; // 學習速率\nvar beta = 0.9; // 獎勵延遲\n\n// 手動點擊畫面使鳥起飛\non('click', bird.jump);\non('keydown', 'space', game.restart);\n\n// 遊戲迴圈\nfunction gameloop () {\n var apple = game.getApple(); // 取得眼前最近的蘋果\n \n var S1 = getStatus(bird, apple);\n print(S1, 100, 10, 'white', 30);\n \n if (Q[S1][0] \u003c Q[S1][1]) {\n var A = 1;\n bird.jump();\n } else {\n var A = 0;\n }\n \n // 更新角色位置與畫面,因次必須寫在 S1 執行 A 行為後 S2 取得狀態之前\n game.update();\n \n var 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 if (bird.touched(apple)) {\n apple.hidden = true;\n reward(S1, A, S2, 50); //如果鳥吃到蘋果獎勵 +50 分\n }\n reward(S1, A, S2, 1); //如果鳥活著獎勵 +1 分\n \n}\n\n// 根據鳥與蘋果的相對位置來作為「狀態」\nfunction getStatus (bird, apple) {\n var x = Math.floor((apple.x - bird.x) / 20);\n var y = Math.floor((apple.y - bird.y) / 20);\n return x + ':' + y\n}\n\n// 獎勵懲罰來更新 Q 大腦\nfunction reward (S1, A, S2, R) {\n var mr = Math.min(Q[S2][0], Q[S2][1])\n Q[S1][A] = Q[S1][A]*(1 - alpha) + (R + beta * mr) * alpha;\n}\n\nforever(gameloop);\n\n","created_at":"2022-11-03T18:39:54.242+08:00","updated_at":"2022-11-03T18:40:42.289+08:00","name":"AI 學會玩 Flappy Bird(完整版)","language":"javascript","screenshot":{"url":"https://cdn2.koding.school/uploads/project/screenshot/455161/bb83cd76fb9f40e93f789c2bc911a1b7.jpg"},"parent_id":4,"plugin":"const stageWidth = 1200\nconst stageHeight = 900\nconst spaceBetweenPipe = 500\nconst cellWidth = 30\nconst speed = 5\nconst gravity = 0.4\n\nsetBackdrop('bg.png')\nGame.set({ width: 1200, height: 900 })\n\nlet count = 0\n\nfunction myGame () {\n \n let instance = {}\n \n let status = 'playing' // playing, stop\n let score = 0\n \n let bird = createSprite('bird.png')\n bird.vy = 0\n \n let g1 = group(400, 450)\n let g2 = group(800, 450)\n let g3 = group(1200, 450)\n let g4 = group(1600, 450)\n let ground1 = createSprite('ground.png')\n let ground2 = createSprite('ground.png')\n let groups = [g1, g2, g3, g4]\n let pipes = [g1.pipe1, g1.pipe2, g2.pipe1, g2.pipe2, g3.pipe1, g3.pipe2, g4.pipe1, g4.pipe2]\n let apples = [g1.apple, g2.apple, g3.apple,, g4.apple]\n let grounds = [ground1, ground2]\n \n // forever(update)\n\n bird.jump = function () {\n bird.vy = -8\n }\n \n function update () {\n print(score, 10, 10, 'white', 90)\n \n if (bird.touched(pipes) || bird.touched(grounds)) {\n status = 'stop'\n }\n if (status === 'stop') return\n \n \n groups.forEach(g =\u003e {\n g.x -= speed\n if (g.x \u003c 0) {\n g.x += spaceBetweenPipe * 4\n g.y = Math.random() * 400 + 200\n score += 1\n g.apple.hidden = false\n }\n g.update()\n })\n \n apples.forEach(apple =\u003e {\n if (instance.displayApple) {\n apple.opacity = 0.5\n getApple().opacity = 1 \n } else {\n apple.opacity = 0.01\n }\n \n })\n \n bird.vy += gravity\n bird.y += bird.vy\n \n ground1.x -= speed\n ground2.x -= speed\n if (ground1.x \u003c -600) ground1.x += 2400\n if (ground2.x \u003c -600) ground2.x += 2400\n }\n \n function restart () {\n count++;\n console.log('count:' + count)\n ground1.y = 850\n ground2.y = 850\n ground1.x = 0\n ground2.x = 1200\n bird.x = 200\n bird.y = 450\n bird.vy = 0\n g1.x = 200 + spaceBetweenPipe * 1\n g2.x = 200 + spaceBetweenPipe * 2\n g3.x = 200 + spaceBetweenPipe * 3\n g4.x = 200 + spaceBetweenPipe * 4\n g1.y = Math.random() * 400 + 200\n g2.y = Math.random() * 400 + 200\n g3.y = Math.random() * 400 + 200\n g4.y = Math.random() * 400 + 200\n g1.update()\n g2.update()\n g3.update()\n g4.update()\n status = 'playing'\n score = 0\n }\n \n function getApple () {\n return apples.filter(apple =\u003e apple.x \u003e bird.x).sort((a, b) =\u003e a.x - b.x)[0]\n }\n \n instance.restart = restart\n instance.getApple = getApple\n instance.bird = bird\n instance.pipes = pipes\n instance.ground = grounds\n instance.groups = groups\n instance.update = update\n \n return instance\n}\n\n\nfunction group (x, y) {\n let g = { x, y }\n g.apple = createSprite('apple.png')\n g.pipe1 = createSprite('pipe.png')\n g.pipe2 = createSprite('pipe.png')\n g.pipe2.direction += 180\n g.update = function () {\n g.apple.moveTo(g)\n g.pipe1.moveTo(g)\n g.pipe2.moveTo(g)\n g.pipe1.move(-60, -500)\n g.pipe2.move(-60, +500)\n }\n return g\n}\n\n\nvar game = myGame()\ngame.restart()\n\n\n// // Brain\nfunction Brain () {\n var w = 25;\n var h = 30\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 \n obj.display = false\n \n forever(() =\u003e {\n if (obj.display === false) return\n game.groups.forEach(g =\u003e {\n var w = 25;\n var h = 30;\n for (var x = 0; x \u003c w; x++) {\n for (var y= -h; y \u003c h; y++) {\n pen.color = 'black'\n let s = Q[x + ':' + y]\n if (s[1] \u003e s[0]) pen.fillColor = '#ff000055'\n else if (s[1] \u003c s[0]) pen.fillColor = '#0000ff55'\n else pen.fillColor = '#00000000'\n pen.size = 0.5\n pen.drawRect(g.x - x*20 - 20, g.y - y*20, 20, 20)\n }\n }\n \n pen.size = 4\n pen.drawLine(g.x, g.y, g.x - 20 * w, g.y)\n pen.drawLine(g.x, g.y - y * 20, g.x, g.y + y * 20)\n pen.size = 1\n })\n })\n \n return obj;\n}\n\n\n\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":119,"hashid":"npmsz54z5","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":10316087,"file_name":"apple.png","project_id":455161,"asset_id":578724,"created_at":"2022-11-03T18:40:33.260+08:00","updated_at":"2022-11-03T18:40:33.260+08:00"},{"id":10316088,"file_name":"pipe.png","project_id":455161,"asset_id":578725,"created_at":"2022-11-03T18:40:33.265+08:00","updated_at":"2022-11-03T18:40:33.265+08:00"},{"id":10316089,"file_name":"ground.png","project_id":455161,"asset_id":578726,"created_at":"2022-11-03T18:40:33.269+08:00","updated_at":"2022-11-03T18:40:33.269+08:00"},{"id":10316090,"file_name":"bird.png","project_id":455161,"asset_id":578727,"created_at":"2022-11-03T18:40:33.274+08:00","updated_at":"2022-11-03T18:40:33.274+08:00"},{"id":10316091,"file_name":"bg.png","project_id":455161,"asset_id":578728,"created_at":"2022-11-03T18:40:33.282+08:00","updated_at":"2022-11-03T18:40:33.282+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦