{"id":320185,"student_id":10,"content":"let bg = createSprite('bg.png'); //背景圖\nlet master = createSprite('master_0.png', 'master_1.png', 'master_2.png'); //魔王\nlet myLaser = createSprite('laser_b.png'); //玩家雷射\nlet player = createSprite('player.png'); //玩家飛船\nlet info = createSprite('info.png', 'success.png', 'fail.png'); //遊戲說明\n\nlet enemies = []; //存放小兵角色\nlet lasers = []; //存放敵人雷射\nlet clock = 0; //紀錄遊戲迴圈執行次數\nlet level = 2; //關卡等級\nlet playerLife = 10; //玩家血量\nlet masterLife = 10; //魔王血量\nlet count = 0; //消滅小兵數量\nlet vx = 3; //魔王水平移動速度\n\nmaster.y = 100; //\nplayer.y = 800;\nmyLaser.direction = 0;\nmyLaser.hidden = true;\ninfo.layer = 1;\nplayer.layer = 2;\n\ncreateSound('bgm.mp3', true);\n\n//初始化關卡\nfunction startLevel () {\n clock = -500; //重置計時器\n level += 1; //切換下個關卡等級\n count = 0; //重置小兵消滅數\n masterLife = 10; //恢復魔王血量\n playerLife = 10; //恢復玩家血量\n enemies = []; // 清空陣列\n\n // 創造小兵\n for (let y = 0; y \u003c level; y++) {\n for (let x = 0; x \u003c 9; x++) {\n let e = createSprite('enemy_0.png', 'enemy_1.png', 'enemy_2.png');\n e.x = 50 + x * 100;\n e.y = 250 + y * 100 - 500;\n e.costumeId = level - 1;\n enemies.push(e); //存入陣列\n }\n }\n master.costumeId = level - 1;\n master.y = -400;\n}\n\n//遊戲主迴圈\nfunction gameloop () {\n if (info.opacity \u003c 1) {\n clock++;\n myLaser.y -= 30;\n updatePlayer(); //更新玩家\n updateMaster(); //更新魔王\n updatelasers(); //更新敵人雷射\n enemies.forEach(updateEnemy); //更新小兵\n checkIsGameover(); //檢查遊戲是否結束\n updateBg(); //更新背景圖\n drawTexts(); //顯示變數資訊\n }\n if (key.space) {\n info.opacity = 0; //隱藏遊戲說明\n }\n if (info.costumeId != 0 \u0026\u0026 info.opacity \u003c 1) {\n info.opacity += 0.01;\n }\n}\n\n//更新玩家角色\nfunction updatePlayer () {\n // 玩家向右移動\n if (key.right \u0026\u0026 player.x \u003c 1100) {\n player.x += 5;\n }\n // 玩家向左移動\n if (key.left \u0026\u0026 player.x \u003e 100) {\n player.x -= 5;\n }\n // 玩家發射雷射\n if (key.up \u0026\u0026 (myLaser.y \u003c 0 || myLaser.hidden)) {\n myLaser.x = player.x;\n myLaser.y = player.y;\n myLaser.hidden = false;\n createSound('laser.mp3');\n }\n}\n\n//更新魔王角色\nfunction updateMaster () {\n master.x += vx;\n // 移動到邊界轉向以及有 1/100 的機率會隨機轉向\n if (master.x \u003e 1100 || master.x \u003c 100 || Math.random() \u003c 0.01) {\n vx = -vx;\n }\n\n // 如果雷射碰到魔王\n master.hidden = false;\n if (master.touched(myLaser)) {\n masterLife -= 1; //魔王血量減少\n myLaser.hidden = true; //隱藏玩家雷射\n master.hidden = true;\n createSound('boom.mp3');\n }\n \n if (clock % 60 == 0) {\n if (level == 1 || level == 3) {\n createLaser(master.x, master.y, 180); \n }\n if (level == 2 || level == 3) {\n createLaser(master.x - 40, master.y, 180); \n createLaser(master.x + 40, master.y, 180);\n }\n }\n \n if (clock % 300 == 0) {\n for (let i = 0; i \u003c 12; i++) {\n createLaser(master.x, master.y, i * 30);\n }\n }\n \n if (clock \u003c 0) {\n master.y += 1;\n }\n}\n\n//更新小兵\nfunction updateEnemy(e) {\n // 如果小兵碰到雷射\n if (e.touched(myLaser)) {\n e.destroy(); //移除小兵角色\n myLaser.hidden = true; //隱藏玩家雷射\n count++; //紀錄擊殺小兵數加一\n for (let i = 0; i \u003c 8; i++) {\n createLaser(e.x, e.y, i * 45);\n }\n createSound('boom.mp3');\n }\n\n // 沒有被消滅的小兵移動到玩家範圍就結束遊戲\n if (e.y \u003e 800 \u0026\u0026 e.hidden == false) playerLife = 0;\n\n if (clock \u003c 0) {\n e.y += 1;\n } else {\n let re = clock % 700;\n if (re \u003c 300) e.x += 1;\n else if (re \u003c 350) e.y += 1;\n else if (re \u003c 650) e.x -= 1;\n else e.y += 1; \n }\n\n}\n\n//創造敵人雷射\nfunction createLaser (x, y, d) {\n let laser = createSprite('laser_r.png');\n laser.moveTo(x, y);\n laser.direction = d;\n lasers.push(laser);\n}\n\n//更新敵人雷射\nfunction updatelasers () {\n player.hidden = false;\n // 使用迴圈將雷射逐一取出更新\n for (let i = 0; i \u003c lasers.length; i++) {\n lasers[i].stepForward(5); //雷射向下移動\n if (lasers[i].touched(player)) {\n lasers[i].destroy(); //移除敵人雷射\n playerLife -= 1; //玩家血量減少 1\n player.hidden = true;\n createSound('boom.mp3');\n }\n }\n}\n\n//檢查遊戲是否結束\nfunction checkIsGameover () {\n if (masterLife \u003c= 0) {\n if (level == 3) {\n info.costumeId = 1; //切換獲勝圖 \n } else {\n startLevel();\n }\n }\n if (playerLife \u003c= 0) {\n info.costumeId = 2; //切換失敗圖\n }\n}\n\n//更新背景圖\nfunction updateBg () {\n bg.y += 2;\n if (bg.y \u003e= 900) {\n bg.y -= 900;\n }\n}\n\n//顯示遊戲資訊\nfunction drawTexts () {\n drawText('masterLife: ' + masterLife, 10, 10, 'white', 30);\n drawText('playerLife: ' + playerLife, 10, 40, 'white', 30);\n drawText('level: ' + level, 10, 70, 'white', 30);\n drawText('count: ' + count, 10, 100, 'white', 30);\n}\n\nforever(gameloop); //不斷重複執行遊戲迴圈\nstartLevel(); //開始新的關卡","created_at":"2021-11-28T18:26:38.817+08:00","updated_at":"2022-06-15T13:25:00.021+08:00","name":"太空防衛戰(預設版)","language":"javascript","screenshot":{"url":"https://cdn3.koding.school/uploads/project/screenshot/320185/b230fa3bcc0fafe3f75ee57f07ce202f.jpg"},"parent_id":2,"plugin":"Game.set({width: 1200, height: 900})","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":161,"hashid":"36ysm92ev","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":6654864,"file_name":"bgm.mp3","project_id":320185,"asset_id":280651,"created_at":"2021-11-29T14:31:30.760+08:00","updated_at":"2021-11-29T14:31:30.760+08:00"},{"id":6653816,"file_name":"player.png","project_id":320185,"asset_id":443922,"created_at":"2021-11-28T18:28:14.196+08:00","updated_at":"2021-11-28T18:28:14.196+08:00"},{"id":6654580,"file_name":"success.png","project_id":320185,"asset_id":329760,"created_at":"2021-11-29T10:30:20.761+08:00","updated_at":"2021-11-29T23:29:59.004+08:00"},{"id":6654213,"file_name":"laser_b.png","project_id":320185,"asset_id":329772,"created_at":"2021-11-28T20:20:38.876+08:00","updated_at":"2021-11-28T20:20:38.876+08:00"},{"id":6654863,"file_name":"laser.mp3","project_id":320185,"asset_id":280652,"created_at":"2021-11-29T14:31:26.926+08:00","updated_at":"2021-11-29T14:31:34.353+08:00"},{"id":6654575,"file_name":"master_1.png","project_id":320185,"asset_id":443955,"created_at":"2021-11-29T10:30:03.216+08:00","updated_at":"2021-11-29T10:30:03.216+08:00"},{"id":6654576,"file_name":"master_2.png","project_id":320185,"asset_id":443956,"created_at":"2021-11-29T10:30:03.217+08:00","updated_at":"2021-11-29T10:30:03.217+08:00"},{"id":6653815,"file_name":"master_0.png","project_id":320185,"asset_id":443921,"created_at":"2021-11-28T18:28:12.946+08:00","updated_at":"2021-11-28T18:28:12.946+08:00"},{"id":6654862,"file_name":"boom.mp3","project_id":320185,"asset_id":280653,"created_at":"2021-11-29T14:31:25.461+08:00","updated_at":"2021-11-29T14:31:25.461+08:00"},{"id":6654212,"file_name":"laser_r.png","project_id":320185,"asset_id":329773,"created_at":"2021-11-28T20:20:24.203+08:00","updated_at":"2021-11-28T20:20:24.203+08:00"},{"id":6654574,"file_name":"info.png","project_id":320185,"asset_id":443954,"created_at":"2021-11-29T10:27:00.184+08:00","updated_at":"2021-11-29T10:27:00.184+08:00"},{"id":6657462,"file_name":"bg.png","project_id":320185,"asset_id":444006,"created_at":"2021-11-30T00:50:11.171+08:00","updated_at":"2021-11-30T00:50:11.171+08:00"},{"id":6654579,"file_name":"fail.png","project_id":320185,"asset_id":329764,"created_at":"2021-11-29T10:30:15.005+08:00","updated_at":"2021-11-29T23:30:04.569+08:00"},{"id":6657621,"file_name":"enemy_0.png","project_id":320185,"asset_id":444007,"created_at":"2021-11-30T11:23:45.788+08:00","updated_at":"2021-11-30T11:23:45.788+08:00"},{"id":6657622,"file_name":"enemy_2.png","project_id":320185,"asset_id":444008,"created_at":"2021-11-30T11:23:45.790+08:00","updated_at":"2021-11-30T11:23:45.790+08:00"},{"id":6657623,"file_name":"enemy_1.png","project_id":320185,"asset_id":444009,"created_at":"2021-11-30T11:23:46.310+08:00","updated_at":"2021-11-30T11:23:46.310+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦