GitHub시작하기

이 페이지는 아직 한국어로 번역되지 않아 영문 원문을 표시하고 있습니다.

튜토리얼

Tutorial 1: An Empty Project

This page is self-contained: the commands, nsgo.yaml and main.go below and the complete game.go and game_test.go at the end of the page are everything chapter 1 needs in a new module.

Create the Go module for Star Catcher, describe the app in nsgo.yaml, open a window that paints the playfield in a Canvas, and test it without a window.

The finished code is in examples/tutorial/ch01-empty-project.

Create the module

A game is an ordinary Go module that requires the game library and NSGo, with the nsgo command as one of its tools:

mkdir starcatcher && cd starcatcher
go mod init example.com/starcatcher
go get github.com/nsgo-dev/game
go get -tool github.com/nsgo-dev/nsgo/cmd/nsgo@v0.4.0

go tool nsgo now runs the nsgo command at the version the module pins, so everyone who builds the game builds it with the same one.

Describe the app

nsgo.yaml, next to go.mod, names the app and says how nsgo dev runs it:

name: Star Catcher 1
id: dev.nsgo.starcatcher.ch01
version: 0.1.0

dev:
  target: native

id is the app's bundle identifier on Apple platforms and its application ID on Android. Each chapter has its own, so two chapters installed side by side on a phone do not replace each other. Chapter 9 sets the name, version and icon the stores will show.

Open a window

main.go hands NSGo the root component:

package main

import "github.com/nsgo-dev/nsgo"

func main() { nsgo.Run(Root) }

Root is an NSGo component: a function of a *core.Scope that returns the views to show. Star Catcher draws its playfield in a core.Canvas that fills the window, and shows the title in a native core.Text above it:

func Root(cx *core.Scope) core.Node {
	return core.View{
		Style: core.Style{FlexGrow: 1, FlexShrink: 1, Background: skyColor},
		Children: []core.Node{
			core.Canvas{
				TestID: "game-canvas",
				Label:  "Star Catcher playfield",
				Style:  fill(),
				Draw:   drawPlayfield,
			},
			core.View{
				PointerEvents: core.PointerEventsNone,
				Style: core.Style{
					Position:   core.Absolute,
					Top:        core.Pt(0),
					Left:       core.Pt(0),
					Right:      core.Pt(0),
					AlignItems: core.AlignCenter,
					Padding:    core.All(24),
					PaddingEnv: core.EnvAll(core.EnvSafeArea),
				},
				Children: []core.Node{
					core.Text{
						TestID:  "title",
						Content: "Star Catcher",
						Style:   core.Style{Color: textColor, FontSize: 28, FontWeight: core.FontWeightBold},
					},
				},
			},
		},
	}
}
  • fill() returns a style that pins a child to all four edges of its parent, so the canvas covers the window at any size.
  • PaddingEnv: core.EnvAll(core.EnvSafeArea) keeps the title clear of a phone's notch and home indicator.
  • PointerEventsNone lets touches pass through the title to the playfield, which chapter 3 starts listening to.
  • TestID names a view for tests, and Label names it for screen readers.

The canvas calls Draw with a *core.Drawing and its own size in points, and draws again whenever the size changes:

func drawPlayfield(d *core.Drawing, size core.Size) {
	w, h := float32(size.Width), float32(size.Height)
	d.SetFillColor(skyColor).FillRect(0, 0, w, h)
	d.SetFillColor(groundColor).FillRect(0, h-groundHeight, w, groundHeight)
}

Run it:

go tool nsgo dev

A dark sky fills the window, with the ground along its bottom and the title at its top. Resize the window, and the ground stays at the bottom.

Test it

NSGo's nsgotest package runs an app without a window: nsgotest.Start mounts it in a window of a size the test picks, Find looks up a view by its TestID or text and fails the test if there is none, and a canvas's Ops are the drawing operations it recorded.

func TestMain(m *testing.M) { nsgotest.Main(m) }

func TestShowsTitleOverPlayfield(t *testing.T) {
	app := nsgotest.Start(t, Root, nsgotest.Size(360, 640))
	app.Find("text:Star Catcher")

	ops := app.Find("game-canvas").Ops
	if !hasFillColor(ops, skyColor) {
		t.Fatal("canvas did not paint the sky")
	}
	if !hasFillColor(ops, groundColor) {
		t.Fatal("canvas did not paint the ground")
	}
}

TestMain hands the main thread to the platform when the tests run on a real one, as go tool nsgo test -target macos does. The chapter's second test resizes the window with app.Resize and checks that the ground moved to the new bottom edge. Run the tests with:

go tool nsgo test ./...

Complete source files

The snippets above are the pieces introduced in this chapter. Copy these complete files when following the tutorial in a new module. They include the colour constants, fill helper and the resize test used by the snippets.

game.go:

package main

import "github.com/nsgo-dev/nsgo/core"

// groundHeight is how tall the strip of ground at the bottom of the playfield is, in points.
const groundHeight = 24

var (
	skyColor    = core.RGB(0x0b1026)
	groundColor = core.RGB(0x1e293b)
	textColor   = core.RGB(0xf8fafc)
)

// Root is the game's root component: a Canvas that fills the window, with the game's title in a native Text above it.
func Root(cx *core.Scope) core.Node {
	return core.View{
		Style: core.Style{FlexGrow: 1, FlexShrink: 1, Background: skyColor},
		Children: []core.Node{
			core.Canvas{
				TestID: "game-canvas",
				Label:  "Star Catcher playfield",
				Style:  fill(),
				Draw:   drawPlayfield,
			},
			core.View{
				PointerEvents: core.PointerEventsNone,
				Style: core.Style{
					Position:   core.Absolute,
					Top:        core.Pt(0),
					Left:       core.Pt(0),
					Right:      core.Pt(0),
					AlignItems: core.AlignCenter,
					Padding:    core.All(24),
					PaddingEnv: core.EnvAll(core.EnvSafeArea),
				},
				Children: []core.Node{
					core.Text{
						TestID:  "title",
						Content: "Star Catcher",
						Style:   core.Style{Color: textColor, FontSize: 28, FontWeight: core.FontWeightBold},
					},
				},
			},
		},
	}
}

// fill is the style of a child that covers its whole parent.
func fill() core.Style {
	return core.Style{
		Position: core.Absolute,
		Top:      core.Pt(0),
		Right:    core.Pt(0),
		Bottom:   core.Pt(0),
		Left:     core.Pt(0),
	}
}

// drawPlayfield paints the night sky and the ground, in the canvas's own points.
func drawPlayfield(d *core.Drawing, size core.Size) {
	w, h := float32(size.Width), float32(size.Height)
	d.SetFillColor(skyColor).FillRect(0, 0, w, h)
	d.SetFillColor(groundColor).FillRect(0, h-groundHeight, w, groundHeight)
}

game_test.go:

package main

import (
	"testing"

	"github.com/nsgo-dev/nsgo/core"
	"github.com/nsgo-dev/nsgo/nsgotest"
)

// TestMain gives the platform the main thread under nsgo test -target.
func TestMain(m *testing.M) { nsgotest.Main(m) }

func TestShowsTitleOverPlayfield(t *testing.T) {
	app := nsgotest.Start(t, Root, nsgotest.Size(360, 640))
	app.Find("text:Star Catcher")

	ops := app.Find("game-canvas").Ops
	if !hasFillColor(ops, skyColor) {
		t.Fatal("canvas did not paint the sky")
	}
	if !hasFillColor(ops, groundColor) {
		t.Fatal("canvas did not paint the ground")
	}
}

func TestGroundFollowsWindowSize(t *testing.T) {
	app := nsgotest.Start(t, Root, nsgotest.Size(360, 640))
	if !hasRect(app.Find("game-canvas").Ops, 0, 640-groundHeight, 360, groundHeight) {
		t.Fatal("ground is not along the bottom of a 360x640 window")
	}

	app.Resize(800, 600)
	if !hasRect(app.Find("game-canvas").Ops, 0, 600-groundHeight, 800, groundHeight) {
		t.Fatal("ground did not follow the window to 800x600")
	}
}

func hasFillColor(ops []core.DrawOp, c core.Color) bool {
	for _, op := range ops {
		if op.Kind == core.DrawFillColor && op.Color == c {
			return true
		}
	}
	return false
}

func hasRect(ops []core.DrawOp, x, y, w, h float32) bool {
	for _, op := range ops {
		if op.Kind == core.DrawRect && op.X == x && op.Y == y && op.W == w && op.H == h {
			return true
		}
	}
	return false
}

Next

The playfield is still a picture. Chapter 2 makes it move with a fixed-step game loop.

이 페이지 편집