mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-24 18:02:02 +01:00
examples/2048: Draw texts
This commit is contained in:
parent
90b6e3871a
commit
399d29b1a0
@ -18,8 +18,10 @@ import (
|
||||
"image/color"
|
||||
"math/rand"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/examples/common"
|
||||
)
|
||||
|
||||
type Board struct {
|
||||
@ -144,8 +146,8 @@ func (b *Board) Move(dir Dir) {
|
||||
}
|
||||
|
||||
const (
|
||||
tileSize = 80
|
||||
tileMargin = 4
|
||||
tileSize = 40
|
||||
tileMargin = 2
|
||||
)
|
||||
|
||||
var (
|
||||
@ -170,9 +172,11 @@ func colorToScale(clr color.Color) (float64, float64, float64, float64) {
|
||||
bf := float64(b) / 0xffff
|
||||
af := float64(a) / 0xffff
|
||||
// Convert to non-premultiplied alpha components.
|
||||
rf /= af
|
||||
gf /= af
|
||||
bf /= af
|
||||
if 0 < af {
|
||||
rf /= af
|
||||
gf /= af
|
||||
bf /= af
|
||||
}
|
||||
return rf, gf, bf, af
|
||||
}
|
||||
|
||||
@ -202,6 +206,18 @@ func (b *Board) Draw(screen *ebiten.Image) error {
|
||||
if err := screen.DrawImage(tileImage, op); err != nil {
|
||||
return err
|
||||
}
|
||||
if t != nil {
|
||||
str := strconv.Itoa(t.value)
|
||||
scale := 2
|
||||
if 2 < len(str) {
|
||||
scale = 1
|
||||
}
|
||||
w := common.ArcadeFont.TextWidth(str) * scale
|
||||
h := common.ArcadeFont.TextHeight(str) * scale
|
||||
x := x + (tileSize-w)/2
|
||||
y := y + (tileSize-h)/2
|
||||
common.ArcadeFont.DrawText(screen, str, x, y, scale, tileColor(t.value))
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
@ -26,8 +26,8 @@ func init() {
|
||||
}
|
||||
|
||||
const (
|
||||
ScreenWidth = 420
|
||||
ScreenHeight = 600
|
||||
ScreenWidth = 210
|
||||
ScreenHeight = 300
|
||||
boardSize = 4
|
||||
)
|
||||
|
||||
|
@ -39,7 +39,7 @@ func update(screen *ebiten.Image) error {
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := ebiten.Run(update, twenty48.ScreenWidth, twenty48.ScreenHeight, 1, "2048 (Ebiten Demo)"); err != nil {
|
||||
if err := ebiten.Run(update, twenty48.ScreenWidth, twenty48.ScreenHeight, 2, "2048 (Ebiten Demo)"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -45,6 +45,11 @@ func (f *Font) TextWidth(str string) int {
|
||||
return f.charWidth * len(str)
|
||||
}
|
||||
|
||||
func (f *Font) TextHeight(str string) int {
|
||||
// TODO: Take care about '\n'
|
||||
return f.charHeight
|
||||
}
|
||||
|
||||
func init() {
|
||||
dir := ""
|
||||
if runtime.GOARCH != "js" {
|
||||
|
Loading…
Reference in New Issue
Block a user