Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "errors"
- "github.com/hajimehoshi/ebiten/v2"
- "github.com/hajimehoshi/ebiten/v2/ebitenutil"
- "log"
- )
- var errRegularTermination = errors.New("regular termination")
- type Game struct {
- exit bool
- }
- func (g *Game) Layout(width, height int) (int, int) {
- return width, height
- }
- func (g *Game) Update() error {
- if ok, exitGame := g.verifyExit(); ok {
- return exitGame
- }
- return nil
- }
- func (g *Game) Draw(screen *ebiten.Image) {
- g.checkExitstatus(screen)
- }
- func (g *Game) verifyExit() (bool, error) {
- if ebiten.IsKeyPressed(ebiten.KeyEscape) || ebiten.IsWindowBeingClosed() {
- g.exit = true
- }
- if g.exit {
- if ebiten.IsKeyPressed(ebiten.KeyY) {
- return true, errRegularTermination
- }
- if ebiten.IsKeyPressed(ebiten.KeyN) {
- g.exit = false
- }
- }
- return false, nil
- }
- func (g *Game) checkExitstatus(screen *ebiten.Image) {
- if g.exit {
- ebitenutil.DebugPrint(screen, "Do you really want to close this window? [y/n]")
- return
- }
- }
- func main() {
- ebiten.SetWindowClosingHandled(true)
- // nolint: exhaustivestruct
- if err := ebiten.RunGame(&Game{}); err != nil && !errors.Is(err, errRegularTermination) {
- log.Fatal(err)
- }
- }
Add Comment
Please, Sign In to add comment