教程 9:釋出
讓 Star Catcher 準備好交到玩家手裡:給它商店需要的名稱、版本和圖示,應用失去焦點時暫停,每次釋出前在測試裡玩完整局,檢查所有目標平臺,並構建釋出包。
完成後的遊戲在 examples/tutorial/ch09-ship。下面每一步的更多細節,包括簽名和上傳商店,見構建與釋出。
名稱、版本和圖示
nsgo.yaml 現在寫明商店裡顯示的內容:
name: Star Catcher
id: dev.nsgo.starcatcher
version: 1.0.0
build_number: 1
copyright: "© 2026 Star Catcher authors"
icon:
png: assets/Icon.png
dev:
target: native
build:
output: dist
targets: [macos, windows, linux, android, web, ios]
id在每次釋出中都必須保持不變:App Store、Google Play 和 Microsoft Store 都靠它識別應用。請使用你擁有的域名的反向 DNS 名稱。version是玩家看到的版本;build_number每次上傳商店加一。icon.png是邊長 1024 畫素的正方形 PNG,nsgo build會把它轉換成各平臺需要的所有尺寸。若要 macOS 和 iOS 的分層圖示,再加上icon.apple,一個 Icon Composer.icon檔案。
用程式碼畫圖示
圖示和精靈一樣由程式碼繪製,程式碼在 internal/icon,由一個小工具寫出:
//go:generate go run ./tools/genicon assets/Icon.png
修改後執行 go generate ./... 重新繪製它。PNG 會提交到倉庫,因為 nsgo build 從 assets/ 讀取它;一個測試防止兩者不一致:
func TestIconIsUpToDate(t *testing.T) {
data, err := os.ReadFile("assets/Icon.png")
if err != nil {
t.Fatal(err)
}
got, err := png.Decode(bytes.NewReader(data))
if err != nil {
t.Fatalf("assets/Icon.png: %v", err)
}
if b := got.Bounds(); b.Dx() != 1024 || b.Dy() != 1024 {
t.Fatalf("icon is %dx%d, want 1024x1024", b.Dx(), b.Dy())
}
want := icon.Render(1024)
// Compare every pixel of got and want…
}
使用手繪美術的遊戲把圖示直接放在 assets/ 裡,不需要生成器。
失去焦點時暫停
應用在後臺時,game.Loop 本來就不執行更新;但應用只是處於非活動狀態時,迴圈會繼續前進:來電響鈴時、開啟應用切換器時,或者桌面上另一個視窗拿到焦點時。離開一下的玩家回來時應該看到暫停選單,而不是發現星星在他離開時已經落下。一個副作用監視應用的生命週期,只要應用不處於活動狀態,就暫停正在進行的遊戲:
cx.Effect(func() {
if cx.Env().Lifecycle() != core.LifecycleActive && s.phase.GetUntracked() == phasePlaying {
s.setPhase(phasePaused)
}
})
只要它讀過的訊號變化,cx.Effect 就會再次執行它的函式。這裡它只讀取生命週期:階段是用 untracked 方式讀取的,所以開始或暫停遊戲不會觸發它。
TestLosingTheFocusPausesTheGame 用 app.SetEnvironment 先讓應用進入非活動狀態、再把它送到後臺,每次都把它帶回來,然後找到暫停選單。
釋出前玩完整局
單元測試一次只檢查一條規則。釋出前值得確認這些規則合起來是一個遊戲:能玩、會越來越難、最終會結束。TestAWholeGame 用一個簡單的機器人玩三個種子的完整局,機器人朝最低的星星移動,並避開快要落進籃子的石頭:
func TestAWholeGame(t *testing.T) {
for _, seed := range []uint64{1, 2, 3} {
w := newWorld(seed)
steps := 0
for !w.over && steps < 60*60*20 {
w.step(step, bot(w))
steps++
}
if !w.over {
t.Fatalf("seed %d: a game lasted over 20 minutes", seed)
}
if w.score < 20 {
t.Fatalf("seed %d: the bot caught only %d stars", seed, w.score)
}
}
}
因為世界是確定性的,每個種子每次都玩出同一局,所以這個測試從不時好時壞;又因為它直接推進世界,二十分鐘的遊戲只需幾毫秒。如果某次規則改動讓遊戲贏不了或者永遠結束不了,這個測試會比玩家先發現。
檢查所有目標平臺
nsgo check 為每個目標平臺檢查程式碼,找出在某個平臺上無法構建的呼叫:
go tool nsgo check -target macos,windows,linux,android,web
go tool nsgo check -target ios,ios_simulator # on a Mac with Xcode
倉庫的 CI 在每次改動時為每一章執行這兩條檢查,並在 Linux 和 macOS 上執行每一章的測試,見 .github/workflows/examples.yml。
構建釋出包
nsgo build 構建 nsgo.yaml 列出的目標,在 dist/ 下為每個目標生成一個目錄,旁邊是安裝器和壓縮包:
go tool nsgo build
go tool nsgo build -target web
go tool nsgo build -channel store -target macos,windows,ios,android
Web 構建是一個可以放到任何靜態託管上的資料夾。在本地用 go tool nsgo build -target web -run 試一試,它會啟動服務並開啟瀏覽器。store 渠道生成 Mac App Store、Microsoft Store、App Store 和 Google Play 接受的包;簽名和上傳見構建與釋出。
接下來
Star Catcher 完成了:它能在六個平臺上開啟,在任何幀率下玩起來都一樣,支援觸控、滑鼠和鍵盤,有聲音,會記憶,能暫停,並且從規則到完整的一局都有測試。要繼續擴充套件它,或者開始你自己的遊戲: