Add 0002-getting-started-with-ebitengine
This commit is contained in:
parent
6c78a639f5
commit
78185b6e21
2 changed files with 80 additions and 12 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue