examples: Use RunGame

Updates #1111
This commit is contained in:
Hajime Hoshi 2020-05-10 05:34:06 +09:00
parent a1e65a5e90
commit b71fa0b59b
3 changed files with 62 additions and 41 deletions

View File

@ -54,10 +54,6 @@ func init() {
bgImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
var (
theViewport = &viewport{}
)
type viewport struct {
x16 int
y16 int
@ -78,14 +74,17 @@ func (p *viewport) Position() (int, int) {
return p.x16, p.y16
}
func update(screen *ebiten.Image) error {
theViewport.Move()
type Game struct {
viewport viewport
}
if ebiten.IsDrawingSkipped() {
return nil
}
func (g *Game) Update(screen *ebiten.Image) error {
g.viewport.Move()
return nil
}
x16, y16 := theViewport.Position()
func (g *Game) Draw(screen *ebiten.Image) {
x16, y16 := g.viewport.Position()
offsetX, offsetY := float64(-x16)/16, float64(-y16)/16
// Draw bgImage on the screen repeatedly.
@ -101,11 +100,16 @@ func update(screen *ebiten.Image) error {
}
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
return nil
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Infinite Scroll (Ebiten Demo)"); err != nil {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Infinite Scroll (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}

View File

@ -45,18 +45,21 @@ func init() {
keyboardImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
func update(screen *ebiten.Image) error {
var pressed []ebiten.Key
type Game struct {
pressed []ebiten.Key
}
func (g *Game) Update(screen *ebiten.Image) error {
g.pressed = nil
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
if ebiten.IsKeyPressed(k) {
pressed = append(pressed, k)
g.pressed = append(g.pressed, k)
}
}
return nil
}
if ebiten.IsDrawingSkipped() {
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
const (
offsetX = 24
offsetY = 40
@ -70,7 +73,7 @@ func update(screen *ebiten.Image) error {
// Draw the highlighted keys.
op = &ebiten.DrawImageOptions{}
for _, p := range pressed {
for _, p := range g.pressed {
op.GeoM.Reset()
r, ok := keyboard.KeyRect(p)
if !ok {
@ -82,16 +85,20 @@ func update(screen *ebiten.Image) error {
}
keyStrs := []string{}
for _, p := range pressed {
for _, p := range g.pressed {
keyStrs = append(keyStrs, p.String())
}
ebitenutil.DebugPrint(screen, strings.Join(keyStrs, ", "))
}
return nil
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Keyboard (Ebiten Demo)"); err != nil {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Keyboard (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}

View File

@ -33,6 +33,10 @@ import (
"github.com/hajimehoshi/ebiten"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// World represents the game state.
type World struct {
area []bool
@ -51,11 +55,6 @@ func NewWorld(width, height int, maxInitLiveCells int) *World {
return w
}
func init() {
rand.Seed(time.Now().UnixNano())
world = NewWorld(screenWidth, screenHeight, int((screenWidth*screenHeight)/10))
}
// init inits world with a random state.
func (w *World) init(maxLiveCells int) {
for i := 0; i < maxLiveCells; i++ {
@ -156,25 +155,36 @@ const (
screenHeight = 240
)
var (
type Game struct {
world *World
pixels = make([]byte, screenWidth*screenHeight*4)
)
pixels []byte
}
func update(screen *ebiten.Image) error {
world.Update()
if ebiten.IsDrawingSkipped() {
return nil
}
world.Draw(pixels)
screen.ReplacePixels(pixels)
func (g *Game) Update(screen *ebiten.Image) error {
g.world.Update()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
if g.pixels == nil {
g.pixels = make([]byte, screenWidth*screenHeight*4)
}
g.world.Draw(g.pixels)
screen.ReplacePixels(g.pixels)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Game of Life (Ebiten Demo)"); err != nil {
g := &Game{
world: NewWorld(screenWidth, screenHeight, int((screenWidth*screenHeight)/10)),
}
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Game of Life (Ebiten Demo)")
if err := ebiten.RunGame(g); err != nil {
log.Fatal(err)
}
}