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"
"time"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
@ -39,7 +36,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
resources "github.com/hajimehoshi/ebiten/v2/examples/resources/images/flappy"
"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")
@ -77,11 +74,9 @@ const (
)
var (
gopherImage *ebiten.Image
tilesImage *ebiten.Image
titleArcadeFont font.Face
arcadeFont font.Face
smallArcadeFont font.Face
gopherImage *ebiten.Image
tilesImage *ebiten.Image
arcadeFaceSource *text.GoTextFaceSource
)
func init() {
@ -99,35 +94,11 @@ func init() {
}
func init() {
tt, err := opentype.Parse(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,
})
s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.PressStart2P_ttf))
if err != nil {
log.Fatal(err)
}
arcadeFaceSource = s
}
type Mode int
@ -292,37 +263,62 @@ func (g *Game) Draw(screen *ebiten.Image) {
if g.mode != ModeTitle {
g.drawGopher(screen)
}
var titleTexts []string
var texts []string
var titleTexts string
var texts string
switch g.mode {
case ModeTitle:
titleTexts = []string{"FLAPPY GOPHER"}
texts = []string{"", "", "", "", "", "", "", "PRESS SPACE KEY", "", "OR A/B BUTTON", "", "OR TOUCH SCREEN"}
titleTexts = "FLAPPY GOPHER"
texts = "\n\n\n\n\n\n\nPRESS SPACE KEY\n\nOR A/B BUTTON\n\nOR TOUCH SCREEN"
case ModeGameOver:
texts = []string{"", "GAME 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)
texts = "\nGAME OVER!"
}
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 {
msg := []string{
"Go Gopher by Renee French is",
"licenced under CC BY 3.0.",
}
for i, l := range msg {
x := (screenWidth - len(l)*smallFontSize) / 2
text.Draw(screen, l, smallArcadeFont, x, screenHeight-4+(i-1)*smallFontSize, color.White)
}
const msg = "Go Gopher by Renee French is\nlicenced under CC BY 3.0."
op := &text.DrawOptions{}
op.GeoM.Translate(screenWidth/2, screenHeight)
op.ColorScale.ScaleWithColor(color.White)
op.LineHeight = smallFontSize
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())
text.Draw(screen, scoreStr, arcadeFont, screenWidth-len(scoreStr)*fontSize, fontSize, color.White)
op = &text.DrawOptions{}
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()))
}