{"id":35255,"student_id":119,"content":"\ngame.displayApple = true;\n\nvar Q = new Brain; //鳥的大腦,記錄在不同的狀態下要做什麼反應(跳/不跳)\nvar S; //上一次的狀態\nvar A; //上一次的行為\nvar _S; //這次的狀態\nvar alpha = 0.3; //學習速率\nvar beta = 0.8; //獎勵延遲\nforever(function () {\n var apple = game.getApple();\n if (bird.touched(apple)) {\n apple.hidden = true;\n reward(S, A, 50, _S);\n } else if (bird.touched(pipes) || bird.touched(grounds)) {\n game.restart();\n reward(S, A, -1000, _S);\n } else {\n reward(S, A, 1, _S);\n }\n if (Q[_S][0] \u003c Q[_S][1]) {\n A = 1;\n bird.jump();\n } else {\n A = 0;\n }\n S = _S;\n _S = getStatus(bird, apple);\n print(_S, 100, 10, 'white', 30);\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\nfunction reward (S, A, R, _S) {\n var mr = Math.max(Q[_S][0], Q[_S][1]);\n \n Q[S][A] = Q[S][A] * (1-alpha) + alpha * (R + beta * mr);\n \n}\n","created_at":"2018-07-18T10:26:58.615+08:00","updated_at":"2019-10-28T00:39:13.025+08:00","name":"flappy bird Q-learning(S) 副本 AI","language":"javascript","screenshot":{"url":"https://cdn4.koding.school/uploads/project/screenshot/35255/a391dca44a582b69ee4f1aa5c89aa9e3.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 grounds: [ground1, ground2],\n pipes: [pipe1, pipe2, pipe3, pipe4, pipe5, pipe6],\n getApple: getApple,\n displayApple: false,\n }\n})();\n\nvar bird = game.bird;\nvar grounds = game.grounds;\nvar pipes = game.pipes;\ngame.restart();","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":83,"hashid":"p93sr2p9","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":640064,"file_name":"apple.png","project_id":35255,"asset_id":63917,"created_at":"2018-07-18T10:26:58.626+08:00","updated_at":"2018-07-18T10:26:58.626+08:00"},{"id":640065,"file_name":"pipe.png","project_id":35255,"asset_id":57152,"created_at":"2018-07-18T10:26:58.628+08:00","updated_at":"2018-07-18T10:26:58.628+08:00"},{"id":640066,"file_name":"ground.png","project_id":35255,"asset_id":57151,"created_at":"2018-07-18T10:26:58.630+08:00","updated_at":"2018-07-18T10:26:58.630+08:00"},{"id":640067,"file_name":"bird.png","project_id":35255,"asset_id":57150,"created_at":"2018-07-18T10:26:58.631+08:00","updated_at":"2018-07-18T10:26:58.631+08:00"},{"id":640068,"file_name":"bg.png","project_id":35255,"asset_id":57149,"created_at":"2018-07-18T10:26:58.633+08:00","updated_at":"2018-07-18T10:26:58.633+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦