examples/flappy: use text/v2

Updates #2454
This commit is contained in:
Hajime Hoshi 2023-11-16 22:49:45 +09:00
parent a749f6fbc3
commit 3635b67f31

View File

@ -27,9 +27,6 @@ import (
"math/rand" "math/rand"
"time" "time"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio" "github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/vorbis" "github.com/hajimehoshi/ebiten/v2/audio/vorbis"
@ -39,7 +36,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts" "github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
resources "github.com/hajimehoshi/ebiten/v2/examples/resources/images/flappy" resources "github.com/hajimehoshi/ebiten/v2/examples/resources/images/flappy"
"github.com/hajimehoshi/ebiten/v2/inpututil" "github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text" "github.com/hajimehoshi/ebiten/v2/text/v2"
) )
var flagCRT = flag.Bool("crt", false, "enable the CRT effect") var flagCRT = flag.Bool("crt", false, "enable the CRT effect")
@ -77,11 +74,9 @@ const (
) )
var ( var (
gopherImage *ebiten.Image gopherImage *ebiten.Image
tilesImage *ebiten.Image tilesImage *ebiten.Image
titleArcadeFont font.Face arcadeFaceSource *text.GoTextFaceSource
arcadeFont font.Face
smallArcadeFont font.Face
) )
func init() { func init() {
@ -99,35 +94,11 @@ func init() {
} }
func init() { func init() {
tt, err := opentype.Parse(fonts.PressStart2P_ttf) s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.PressStart2P_ttf))
if err != nil {
log.Fatal(err)
}
const dpi = 72
titleArcadeFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: titleFontSize,
DPI: dpi,
Hinting: font.HintingFull,
})
if err != nil {
log.Fatal(err)
}
arcadeFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: fontSize,
DPI: dpi,
Hinting: font.HintingFull,
})
if err != nil {
log.Fatal(err)
}
smallArcadeFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: smallFontSize,
DPI: dpi,
Hinting: font.HintingFull,
})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
arcadeFaceSource = s
} }
type Mode int type Mode int
@ -292,37 +263,62 @@ func (g *Game) Draw(screen *ebiten.Image) {
if g.mode != ModeTitle { if g.mode != ModeTitle {
g.drawGopher(screen) g.drawGopher(screen)
} }
var titleTexts []string
var texts []string var titleTexts string
var texts string
switch g.mode { switch g.mode {
case ModeTitle: case ModeTitle:
titleTexts = []string{"FLAPPY GOPHER"} titleTexts = "FLAPPY GOPHER"
texts = []string{"", "", "", "", "", "", "", "PRESS SPACE KEY", "", "OR A/B BUTTON", "", "OR TOUCH SCREEN"} texts = "\n\n\n\n\n\n\nPRESS SPACE KEY\n\nOR A/B BUTTON\n\nOR TOUCH SCREEN"
case ModeGameOver: case ModeGameOver:
texts = []string{"", "GAME OVER!"} texts = "\nGAME OVER!"
}
for i, l := range titleTexts {
x := (screenWidth - len(l)*titleFontSize) / 2
text.Draw(screen, l, titleArcadeFont, x, (i+4)*titleFontSize, color.White)
}
for i, l := range texts {
x := (screenWidth - len(l)*fontSize) / 2
text.Draw(screen, l, arcadeFont, x, (i+4)*fontSize, color.White)
} }
op := &text.DrawOptions{}
op.GeoM.Translate(screenWidth/2, 3*titleFontSize)
op.ColorScale.ScaleWithColor(color.White)
op.LineHeight = titleFontSize
op.PrimaryAlign = text.AlignCenter
text.Draw(screen, titleTexts, &text.GoTextFace{
Source: arcadeFaceSource,
Size: titleFontSize,
}, op)
op = &text.DrawOptions{}
op.GeoM.Translate(screenWidth/2, 3*titleFontSize)
op.ColorScale.ScaleWithColor(color.White)
op.LineHeight = fontSize
op.PrimaryAlign = text.AlignCenter
text.Draw(screen, texts, &text.GoTextFace{
Source: arcadeFaceSource,
Size: fontSize,
}, op)
if g.mode == ModeTitle { if g.mode == ModeTitle {
msg := []string{ const msg = "Go Gopher by Renee French is\nlicenced under CC BY 3.0."
"Go Gopher by Renee French is",
"licenced under CC BY 3.0.", op := &text.DrawOptions{}
} op.GeoM.Translate(screenWidth/2, screenHeight)
for i, l := range msg { op.ColorScale.ScaleWithColor(color.White)
x := (screenWidth - len(l)*smallFontSize) / 2 op.LineHeight = smallFontSize
text.Draw(screen, l, smallArcadeFont, x, screenHeight-4+(i-1)*smallFontSize, color.White) op.PrimaryAlign = text.AlignCenter
} op.SecondaryAlign = text.AlignEnd
text.Draw(screen, msg, &text.GoTextFace{
Source: arcadeFaceSource,
Size: smallFontSize,
}, op)
} }
scoreStr := fmt.Sprintf("%04d", g.score()) op = &text.DrawOptions{}
text.Draw(screen, scoreStr, arcadeFont, screenWidth-len(scoreStr)*fontSize, fontSize, color.White) op.GeoM.Translate(screenWidth, 0)
op.ColorScale.ScaleWithColor(color.White)
op.LineHeight = fontSize
op.PrimaryAlign = text.AlignEnd
text.Draw(screen, fmt.Sprintf("%04d", g.score()), &text.GoTextFace{
Source: arcadeFaceSource,
Size: fontSize,
}, op)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.ActualTPS())) ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.ActualTPS()))
} }