{"id":485351,"student_id":2589,"content":"let bg = createSprite('bg.png'); //背景圖\nlet master = createSprite('master_0.png', 'master_1.png', 'master_2.png'); //魔王\nlet laser = createSprite('laser_b.png'); //玩家子彈\nlet player = createSprite('player.png'); //玩家飛船\nlet info = createSprite('info.png', 'success.png', 'fail.png'); //遊戲說明\n\nlet clock = 0; //紀錄遊戲迴圈執行次數\nlet level = 0; //關卡等級\nlet vx = 3; //魔王水平移動速度\nlet playerLife = 10; //玩家血量\nlet masterLife = 10; //魔王血量\nlet count = 0; //每一關小兵消滅數\nlet enemies = []; //存放小兵角色\nlet lasers = []; //存放敵人子彈\n\nmaster.y = 100; //初始化魔王位置\nplayer.y = 800; //初始化玩家位置\nlaser.direction = 0; //玩家雷射朝向上方\nlaser.hidden = true; //隱藏玩家雷射\n// info.opacity = 0; //遊戲說明\ninfo.layer = 1; //遊戲說明置於圖層最上\nplayer.layer = 2;\n\ncreateSound('bgm.mp3', true); //播放背景音樂\n\n//初始化關卡\nfunction startLevel () {\n masterLife = 10; //恢復魔王血量\n playerLife = 10; //恢復玩家血量\n count = 0; //重新紀錄每關小兵消滅數\n enemies = []; //清空存放小兵角色陣列\n clock = -100; //重置計時器\n level += 1; //關卡等級加 1\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 = x * 100 + 50;\n e.y = y * 100 + 250;\n e.y -= 500;\n e.costumeId = level - 1;\n enemies.push(e);\n }\n }\n master.y -= 500;\n master.costumeId = level - 1;\n}\n\n//遊戲主迴圈\nfunction gameloop () {\n // 如果角色全透明消失就開始遊戲\n if (info.opacity \u003c 1) {\n clock++;\n updatePlayer(); //更新玩家\n updateMaster(); //更新魔王\n updateLasers(); //更新雷射\n enemies.forEach(updateEnemy); //更新小兵\n checkIsGameover(); //檢查遊戲是否結束\n drawTexts(); //顯示變數資訊\n updateBg(); //捲動背景圖\n }\n //按下空白按鍵開始遊戲\n if (key.space) {\n info.opacity = 0;\n }\n //如果遊戲結束就慢慢淡入\n if (info.costumeId != 0 \u0026\u0026 info.opacity \u003c 1) {\n info.opacity += 0.01;\n }\n}\n\n//更新玩家角色\nfunction updatePlayer () {\n // 按下右鍵且沒超出右邊邊界,就向右移動 5\n if (key.right \u0026\u0026 player.x \u003c 1100) player.x += 5;\n\n // 按下左鍵且沒超出左邊邊界,就向左移動 5\n if (key.left \u0026\u0026 player.x \u003e 100) player.x -= 5;\n\n // 按下上鍵且雷射已經隱藏或超出場景上方\n if (key.up \u0026\u0026 (laser.hidden || laser.y \u003c 0)) {\n laser.moveTo(player); //移動至玩家所在座標\n laser.hidden = false; //將隱藏的雷射重新顯示\n createSound('laser.mp3'); //播放發射音效\n }\n}\n\n//更新魔王角色\nfunction updateMaster () {\n \n if (clock \u003c 0) master.y += 5;\n\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 if (master.touched(laser)) {\n masterLife -= 1; //血量減一\n laser.hidden = true; //隱藏玩家雷射\n createSound('boom.mp3'); //播放音效\n master.opacity = 0; //魔王變透明\n } else {\n master.opacity = 1; //魔王變不透明\n }\n\n master.hidden = masterLife \u003c= 0; //如果魔王血量低於 0 就隱藏\n \n if (Math.random() \u003c 0.01 * level \u0026\u0026 masterLife \u003e 0) {\n createLaser(master.x, master.y, 180);\n }\n}\n\n//更新小兵\nfunction updateEnemy(e) {\n\n // 如果小兵碰到雷射\n if (e.touched(laser)) {\n e.destroy(); //移除小兵角色\n laser.hidden = true; //隱藏玩家雷射\n count++; //小兵消滅數加一\n createSound('boom.mp3');\n for (let i = 0; i \u003c 8; i++) {\n createLaser(e.x, e.y, 45 * i);\n }\n }\n \n // 如果小兵超過下方界線,就讓玩家生命歸零來結束遊戲\n if (e.y \u003e 800) playerLife = 0;\n \n // 更新小兵位置\n re = clock % 700;\n if (clock \u003c 0) e.y += 5;\n else 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//創造敵人子彈\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 laser.y -= 30; //玩家雷射以 30 速度向上移動\n for (let i = 0; i \u003c lasers.length; i++) {\n lasers[i].stepForward(5); //敵人雷射以 5 速度向前移動\n if (lasers[i].touched(player)) {\n lasers[i].destroy();\n playerLife -= 1;\n createSound('boom.mp3');\n player.opacity = 0;\n } else {\n player.opacity = 1;\n }\n }\n}\n\n//檢查遊戲是否結束\nfunction checkIsGameover () {\n if (masterLife \u003c= 0 \u0026\u0026 count == level * 9) {\n if (level == 3) { // 如果是第三關就\n info.costumeId = 1; //結束遊戲\n } else {\n startLevel(); //重新下一關\n }\n }\n // 如果玩家血量低於 0 就結束遊戲\n if (playerLife \u003c= 0) info.costumeId = 2;\n}\n\n//捲動背景圖\nfunction updateBg () {\n bg.y += 3;\n if (bg.y \u003e 900) bg.y -= 900;\n}\n\n//顯示遊戲資訊\nfunction drawTexts () {\n drawText('masterLife: ' + masterLife, 10, 10, 'white', 20);\n drawText('playerLife: ' + playerLife, 10, 30, 'white', 20);\n drawText('level: ' + level, 10, 50, 'white', 20);\n drawText('count: ' + count, 10, 70, 'white', 20);\n drawText('clock: ' + count, 10, 90, 'white', 20);\n}\n\nforever(gameloop);\nstartLevel();","created_at":"2023-01-17T19:53:04.074+08:00","updated_at":"2023-03-14T10:04:26.299+08:00","name":"太空防衛戰 - 開始後用滑鼠點擊舞台以啟用按鍵,重整畫面即可重新遊戲。","language":"javascript","screenshot":{"url":null},"parent_id":485346,"plugin":"Game.set({width: 1200, height: 900})","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":50,"hashid":"gk4spyw26","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":11045665,"file_name":"enemy_1.png","project_id":485351,"asset_id":597674,"created_at":"2023-01-17T19:53:18.783+08:00","updated_at":"2023-01-17T19:53:18.783+08:00"},{"id":11045666,"file_name":"enemy_2.png","project_id":485351,"asset_id":597675,"created_at":"2023-01-17T19:53:18.787+08:00","updated_at":"2023-01-17T19:53:18.787+08:00"},{"id":11045667,"file_name":"enemy_0.png","project_id":485351,"asset_id":597676,"created_at":"2023-01-17T19:53:18.791+08:00","updated_at":"2023-01-17T19:53:18.791+08:00"},{"id":11045668,"file_name":"bg.png","project_id":485351,"asset_id":597677,"created_at":"2023-01-17T19:53:18.794+08:00","updated_at":"2023-01-17T19:53:18.794+08:00"},{"id":11045669,"file_name":"fail.png","project_id":485351,"asset_id":597678,"created_at":"2023-01-17T19:53:18.799+08:00","updated_at":"2023-01-17T19:53:18.799+08:00"},{"id":11045670,"file_name":"bgm.mp3","project_id":485351,"asset_id":597679,"created_at":"2023-01-17T19:53:18.918+08:00","updated_at":"2023-01-17T19:53:18.918+08:00"},{"id":11045671,"file_name":"laser.mp3","project_id":485351,"asset_id":597680,"created_at":"2023-01-17T19:53:18.923+08:00","updated_at":"2023-01-17T19:53:18.923+08:00"},{"id":11045672,"file_name":"boom.mp3","project_id":485351,"asset_id":597681,"created_at":"2023-01-17T19:53:18.927+08:00","updated_at":"2023-01-17T19:53:18.927+08:00"},{"id":11045673,"file_name":"success.png","project_id":485351,"asset_id":597682,"created_at":"2023-01-17T19:53:18.931+08:00","updated_at":"2023-01-17T19:53:18.931+08:00"},{"id":11045674,"file_name":"master_2.png","project_id":485351,"asset_id":597683,"created_at":"2023-01-17T19:53:18.934+08:00","updated_at":"2023-01-17T19:53:18.934+08:00"},{"id":11045675,"file_name":"master_1.png","project_id":485351,"asset_id":597684,"created_at":"2023-01-17T19:53:18.938+08:00","updated_at":"2023-01-17T19:53:18.938+08:00"},{"id":11045676,"file_name":"info.png","project_id":485351,"asset_id":597685,"created_at":"2023-01-17T19:53:18.942+08:00","updated_at":"2023-01-17T19:53:18.942+08:00"},{"id":11045677,"file_name":"laser_b.png","project_id":485351,"asset_id":597686,"created_at":"2023-01-17T19:53:18.945+08:00","updated_at":"2023-01-17T19:53:18.945+08:00"},{"id":11045678,"file_name":"laser_r.png","project_id":485351,"asset_id":597687,"created_at":"2023-01-17T19:53:18.949+08:00","updated_at":"2023-01-17T19:53:18.949+08:00"},{"id":11045679,"file_name":"player.png","project_id":485351,"asset_id":597688,"created_at":"2023-01-17T19:53:18.952+08:00","updated_at":"2023-01-17T19:53:18.952+08:00"},{"id":11045680,"file_name":"master_0.png","project_id":485351,"asset_id":597689,"created_at":"2023-01-17T19:53:18.956+08:00","updated_at":"2023-01-17T19:53:18.956+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦