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