From 52a753a12f72865198e4185281e2781da85b29e7 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 30 Jan 2018 03:57:07 +0900 Subject: [PATCH] examples/platformer: Refactoring --- examples/platformer/main.go | 49 ++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/examples/platformer/main.go b/examples/platformer/main.go index 77ca3316b..d76f76b57 100644 --- a/examples/platformer/main.go +++ b/examples/platformer/main.go @@ -25,8 +25,8 @@ import ( const ( // Settings - width = 1024 - height = 512 + screenWidth = 1024 + screenHeight = 512 ) var ( @@ -35,7 +35,30 @@ var ( rightSprite *ebiten.Image idleSprite *ebiten.Image backgroundImage *ebiten.Image +) +func init() { + // Preload images + var err error + rightSprite, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/right.png", ebiten.FilterNearest) + if err != nil { + panic(err) + } + leftSprite, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/left.png", ebiten.FilterNearest) + if err != nil { + panic(err) + } + idleSprite, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/mainchar.png", ebiten.FilterNearest) + if err != nil { + panic(err) + } + backgroundImage, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/background.png", ebiten.FilterNearest) + if err != nil { + panic(err) + } +} + +var ( charX = 50 charY = 380 ) @@ -79,27 +102,7 @@ func update(screen *ebiten.Image) error { } func main() { - // Preload images - var err error - rightSprite, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/right.png", ebiten.FilterNearest) - if err != nil { - panic(err) - } - leftSprite, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/left.png", ebiten.FilterNearest) - if err != nil { - panic(err) - } - idleSprite, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/mainchar.png", ebiten.FilterNearest) - if err != nil { - panic(err) - } - backgroundImage, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/background.png", ebiten.FilterNearest) - if err != nil { - panic(err) - } - - // Starts the program - if err := ebiten.Run(update, width, height, 1, "Platformer (Ebiten Demo)"); err != nil { + if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Platformer (Ebiten Demo)"); err != nil { panic(err) } }