blocks: Show next

This commit is contained in:
Hajime Hoshi 2014-12-29 14:27:43 +09:00
parent f0277727d1
commit bb0dfcfc3e
5 changed files with 68 additions and 29 deletions

View File

@ -39,7 +39,7 @@ func textWidth(str string) int {
return charWidth * len(str) return charWidth * len(str)
} }
func drawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) { func drawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) error {
parts := []ebiten.ImagePart{} parts := []ebiten.ImagePart{}
locationX, locationY := 0, 0 locationX, locationY := 0, 0
@ -68,14 +68,19 @@ func drawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) {
b := float64(c2.B) / max b := float64(c2.B) / max
a := float64(c2.A) / max a := float64(c2.A) / max
clr := ebiten.ScaleColor(r, g, b, a) clr := ebiten.ScaleColor(r, g, b, a)
rt.DrawImage(imageFont, &ebiten.DrawImageOptions{ return rt.DrawImage(imageFont, &ebiten.DrawImageOptions{
Parts: parts, Parts: parts,
GeoM: geo, GeoM: geo,
ColorM: clr, ColorM: clr,
}) })
} }
func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) { func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) error {
drawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80}) if err := drawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80}); err != nil {
drawText(rt, str, x, y, scale, clr) return err
}
if err := drawText(rt, str, x, y, scale, clr); err != nil {
return err
}
return nil
} }

View File

@ -16,7 +16,6 @@ package blocks
import ( import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"image/color" "image/color"
"math/rand" "math/rand"
"time" "time"
@ -26,10 +25,21 @@ var imageEmpty *ebiten.Image
func init() { func init() {
var err error var err error
imageEmpty, _, err = ebitenutil.NewImageFromFile("images/blocks/empty.png", ebiten.FilterNearest) imageEmpty, err = ebiten.NewImage(16, 16, ebiten.FilterNearest)
if err != nil { if err != nil {
panic(err) panic(err)
} }
imageEmpty.Fill(color.White)
}
func drawRect(r *ebiten.Image, x, y, width, height int) error {
w, h := imageEmpty.Size()
geo := ebiten.ScaleGeo(float64(width)/float64(w), float64(height)/float64(h))
geo.Concat(ebiten.TranslateGeo(float64(x), float64(y)))
return r.DrawImage(imageEmpty, &ebiten.DrawImageOptions{
GeoM: geo,
ColorM: ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5),
})
} }
type GameScene struct { type GameScene struct {
@ -120,23 +130,41 @@ func (s *GameScene) Draw(r *ebiten.Image) error {
return err return err
} }
field := imageEmpty const fieldX, fieldY = 20, 20
w, h := field.Size()
geo := ebiten.ScaleGeo(float64(fieldWidth)/float64(w), float64(fieldHeight)/float64(h)) // Draw field
geo.Concat(ebiten.TranslateGeo(20, 20)) // TODO: magic number? if err := drawRect(r, fieldX, fieldY, fieldWidth, fieldHeight); err != nil {
if err := r.DrawImage(field, &ebiten.DrawImageOptions{
GeoM: geo,
ColorM: ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5),
}); err != nil {
return err return err
} }
if err := s.field.Draw(r, 20, 20); err != nil { // Draw next
x := fieldX + fieldWidth + blockWidth*2
y := fieldY
if err := drawTextWithShadow(r, "NEXT", x, y, 1, color.NRGBA{0x00, 0x00, 0x80, 0xff}); err != nil {
return err
}
nextX := x
nextY := y + blockHeight
if err := drawRect(r, nextX, nextY, blockWidth*5, blockHeight*5); err != nil {
return err return err
} }
if err := s.field.Draw(r, fieldX, fieldY); err != nil {
return err
}
if s.currentPiece != nil { if s.currentPiece != nil {
return s.currentPiece.Draw(r, 20, 20, s.currentPieceX, s.currentPieceY, s.currentPieceAngle) x := fieldX + s.currentPieceX*blockWidth
y := fieldY + s.currentPieceY*blockHeight
if err := s.currentPiece.Draw(r, x, y, s.currentPieceAngle); err != nil {
return err
}
}
if s.nextPiece != nil {
x := nextX
y := nextY
if err := s.nextPiece.DrawAtCenter(r, x, y, blockWidth*5, blockHeight*5, 0); err != nil {
return err
}
} }
return nil return nil
} }

View File

@ -221,7 +221,13 @@ func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) {
} }
} }
func (p *Piece) Draw(r *ebiten.Image, fieldX, fieldY int, pieceX, pieceY int, angle Angle) error { func (p *Piece) DrawAtCenter(r *ebiten.Image, x, y, width, height int, angle Angle) error {
x += (width - len(p.blocks[0])*blockWidth) / 2
y += (height - len(p.blocks)*blockHeight) / 2
return p.Draw(r, x, y, angle)
}
func (p *Piece) Draw(r *ebiten.Image, x, y int, angle Angle) error {
size := len(p.blocks) size := len(p.blocks)
blocks := make([][]BlockType, size) blocks := make([][]BlockType, size)
for i := range p.blocks { for i := range p.blocks {
@ -232,8 +238,5 @@ func (p *Piece) Draw(r *ebiten.Image, fieldX, fieldY int, pieceX, pieceY int, an
} }
} }
} }
x := fieldX + pieceX*blockWidth
y := fieldY + pieceY*blockHeight
return drawBlocks(r, blocks, x, y) return drawBlocks(r, blocks, x, y)
} }

View File

@ -48,17 +48,20 @@ func (s *TitleScene) Update(state *GameState) error {
} }
func (s *TitleScene) Draw(r *ebiten.Image) error { func (s *TitleScene) Draw(r *ebiten.Image) error {
drawTitleBackground(r, s.count) if err := drawTitleBackground(r, s.count); err != nil {
drawLogo(r, "BLOCKS") return err
}
if err := drawLogo(r, "BLOCKS"); err != nil {
return err
}
message := "PRESS SPACE TO START" message := "PRESS SPACE TO START"
x := (ScreenWidth - textWidth(message)) / 2 x := (ScreenWidth - textWidth(message)) / 2
y := ScreenHeight - 48 y := ScreenHeight - 48
drawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff}) return drawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff})
return nil
} }
func drawTitleBackground(r *ebiten.Image, c int) { func drawTitleBackground(r *ebiten.Image, c int) error {
w, h := imageBackground.Size() w, h := imageBackground.Size()
dx := (-c / 4) % w dx := (-c / 4) % w
dy := (c / 4) % h dy := (c / 4) % h
@ -75,15 +78,15 @@ func drawTitleBackground(r *ebiten.Image, c int) {
} }
} }
r.DrawImage(imageBackground, &ebiten.DrawImageOptions{ return r.DrawImage(imageBackground, &ebiten.DrawImageOptions{
Parts: parts, Parts: parts,
}) })
} }
func drawLogo(r *ebiten.Image, str string) { func drawLogo(r *ebiten.Image, str string) error {
scale := 4 scale := 4
textWidth := textWidth(str) * scale textWidth := textWidth(str) * scale
x := (ScreenWidth - textWidth) / 2 x := (ScreenWidth - textWidth) / 2
y := 32 y := 32
drawTextWithShadow(r, str, x, y, scale, color.NRGBA{0x00, 0x00, 0x80, 0xff}) return drawTextWithShadow(r, str, x, y, scale, color.NRGBA{0x00, 0x00, 0x80, 0xff})
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 122 B