From 78185b6e2197ad296285461210872a3dc0ee6c8a Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Mon, 5 Aug 2024 18:33:42 -0700 Subject: [PATCH] Add 0002-getting-started-with-ebitengine --- 0002-getting-started-with-ebitengine/game.go | 87 +++++++++++++++++--- 0002-getting-started-with-ebitengine/main.go | 5 +- 2 files changed, 80 insertions(+), 12 deletions(-) diff --git a/0002-getting-started-with-ebitengine/game.go b/0002-getting-started-with-ebitengine/game.go index 4bc52cd..66d0598 100644 --- a/0002-getting-started-with-ebitengine/game.go +++ b/0002-getting-started-with-ebitengine/game.go @@ -1,22 +1,89 @@ package main -import "github.com/hajimehoshi/ebiten/v2" +import ( + "image" + "image/color" + + "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/inpututil" +) + +const ( + screenW, screenH = 256, 144 + playerW, playerH = 22, 42 + groundH = 42 +) + +var ( + backgroundColor = color.RGBA{255, 255, 255, 255} + groundColor = color.RGBA{71, 39, 2, 255} + playerColor = color.RGBA{6, 154, 46, 255} +) type game struct { + // Player image. + playerImg *ebiten.Image + + // Player attributes. + positionX, positionY float64 + + // Game attributes. + fullscreen bool + initialized bool } -func (g game) Update() error { +// initialize sets up the initial state of the game. +func (g *game) initialize() { + if g.initialized { + return + } + + // Initialize player image. + g.playerImg = ebiten.NewImage(playerW, playerH) + g.playerImg.Fill(playerColor) + + // Initialize player attributes. + g.positionX = screenW/2 - playerW/2 + g.positionY = screenH - groundH + + // Initialize game attributes. + g.fullscreen = true + g.initialized = true +} + +// Layout is called each frame with the current size of the window. +func (g *game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) { + // Use fixed screen size. + return screenW, screenH +} + +// Update is where we update the game state. +func (g *game) Update() error { + // Handle initialization. + if !g.initialized { + g.initialize() + } + + // Handle fullscreen. + if inpututil.IsKeyJustPressed(ebiten.KeyEnter) && ebiten.IsKeyPressed(ebiten.KeyAlt) { + g.fullscreen = !g.fullscreen + ebiten.SetFullscreen(g.fullscreen) + } + return nil } -func (g game) Draw(screen *ebiten.Image) { +// Draw is where we draw the game state. +func (g *game) Draw(screen *ebiten.Image) { + // Draw background. + screen.Fill(backgroundColor) -} + // Draw ground. + groundRect := image.Rect(0, screenH-groundH, screenW, screenH) + screen.SubImage(groundRect).(*ebiten.Image).Fill(groundColor) -func (g game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) { - return outsideWidth, outsideHeight // Set viewport size to new window size. -} - -func newGame() *game { - return &game{} + // Draw player. + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(g.positionX, g.positionY-playerH) + screen.DrawImage(g.playerImg, op) } diff --git a/0002-getting-started-with-ebitengine/main.go b/0002-getting-started-with-ebitengine/main.go index a1dbd1d..8288f7f 100644 --- a/0002-getting-started-with-ebitengine/main.go +++ b/0002-getting-started-with-ebitengine/main.go @@ -9,11 +9,12 @@ import ( func main() { ebiten.SetWindowTitle("TT0002: Getting Started with Ebitengine") ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled) - ebiten.SetWindowSize(800, 600) + ebiten.SetWindowSize(screenW, screenH) + ebiten.SetFullscreen(true) ebiten.SetVsyncEnabled(true) ebiten.SetTPS(60) - err := ebiten.RunGame(newGame()) + err := ebiten.RunGame(&game{}) if err != nil { log.Fatal(err) }