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)
}
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{}
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
a := float64(c2.A) / max
clr := ebiten.ScaleColor(r, g, b, a)
rt.DrawImage(imageFont, &ebiten.DrawImageOptions{
return rt.DrawImage(imageFont, &ebiten.DrawImageOptions{
Parts: parts,
GeoM: geo,
ColorM: clr,
})
}
func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) {
drawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80})
drawText(rt, str, x, y, scale, clr)
func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) error {
if err := drawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80}); err != nil {
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 (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"image/color"
"math/rand"
"time"
@ -26,10 +25,21 @@ var imageEmpty *ebiten.Image
func init() {
var err error
imageEmpty, _, err = ebitenutil.NewImageFromFile("images/blocks/empty.png", ebiten.FilterNearest)
imageEmpty, err = ebiten.NewImage(16, 16, ebiten.FilterNearest)
if err != nil {
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 {
@ -120,23 +130,41 @@ func (s *GameScene) Draw(r *ebiten.Image) error {
return err
}
field := imageEmpty
w, h := field.Size()
geo := ebiten.ScaleGeo(float64(fieldWidth)/float64(w), float64(fieldHeight)/float64(h))
geo.Concat(ebiten.TranslateGeo(20, 20)) // TODO: magic number?
if err := r.DrawImage(field, &ebiten.DrawImageOptions{
GeoM: geo,
ColorM: ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5),
}); err != nil {
const fieldX, fieldY = 20, 20
// Draw field
if err := drawRect(r, fieldX, fieldY, fieldWidth, fieldHeight); err != nil {
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
}
if err := s.field.Draw(r, fieldX, fieldY); err != nil {
return err
}
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
}

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)
blocks := make([][]BlockType, size)
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)
}

View File

@ -48,17 +48,20 @@ func (s *TitleScene) Update(state *GameState) error {
}
func (s *TitleScene) Draw(r *ebiten.Image) error {
drawTitleBackground(r, s.count)
drawLogo(r, "BLOCKS")
if err := drawTitleBackground(r, s.count); err != nil {
return err
}
if err := drawLogo(r, "BLOCKS"); err != nil {
return err
}
message := "PRESS SPACE TO START"
x := (ScreenWidth - textWidth(message)) / 2
y := ScreenHeight - 48
drawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff})
return nil
return drawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff})
}
func drawTitleBackground(r *ebiten.Image, c int) {
func drawTitleBackground(r *ebiten.Image, c int) error {
w, h := imageBackground.Size()
dx := (-c / 4) % w
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,
})
}
func drawLogo(r *ebiten.Image, str string) {
func drawLogo(r *ebiten.Image, str string) error {
scale := 4
textWidth := textWidth(str) * scale
x := (ScreenWidth - textWidth) / 2
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