blocks: Gameover

This commit is contained in:
Hajime Hoshi 2014-12-29 16:41:58 +09:00
parent b02bce4876
commit 9288c9dcc9
3 changed files with 37 additions and 1 deletions

View File

@ -85,6 +85,12 @@ func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color
return nil
}
func drawTextWithShadowCenter(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) error {
w := textWidth(str) * scale
x += (width - w) / 2
return drawTextWithShadow(rt, str, x, y, scale, clr)
}
func drawTextWithShadowRight(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) error {
w := textWidth(str) * scale
x += width - w

View File

@ -23,6 +23,7 @@ import (
)
var imageEmpty *ebiten.Image
var imageGameover *ebiten.Image
func init() {
var err error
@ -31,6 +32,16 @@ func init() {
panic(err)
}
imageEmpty.Fill(color.White)
imageGameover, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
}
imageGameover.Fill(color.NRGBA{0x00, 0x00, 0x00, 0x80})
y := (ScreenHeight - blockHeight) / 2
if err := drawTextWithShadowCenter(imageGameover, "GAME OVER", 0, y, 1, color.White, ScreenWidth); err != nil {
panic(err)
}
}
func drawRect(r *ebiten.Image, x, y, width, height int) error {
@ -70,6 +81,7 @@ type GameScene struct {
currentFrame int
score int
lines int
gameover bool
}
func NewGameScene() *GameScene {
@ -119,6 +131,10 @@ func (s *GameScene) addScore(lines int) {
}
func (s *GameScene) Update(state *GameState) error {
if s.gameover {
return nil
}
s.currentFrame++
const maxLandingCount = 60
@ -128,6 +144,8 @@ func (s *GameScene) Update(state *GameState) error {
if s.nextPiece == nil {
s.nextPiece = s.choosePiece()
}
// Move piece by user input.
piece := s.currentPiece
x := s.currentPieceX
y := s.currentPieceY
@ -176,8 +194,11 @@ func (s *GameScene) Update(state *GameState) error {
s.addScore(lines)
}
s.initCurrentPiece(s.nextPiece)
s.nextPiece = nil
s.nextPiece = s.choosePiece()
s.landingCount = 0
if s.currentPiece.Collides(s.field, s.currentPieceX, s.currentPieceY, s.currentPieceAngle) {
s.gameover = true
}
}
}
return nil
@ -245,5 +266,10 @@ func (s *GameScene) Draw(r *ebiten.Image) error {
return err
}
}
if s.gameover {
r.DrawImage(imageGameover, nil)
}
return nil
}

View File

@ -210,6 +210,10 @@ func (p *Piece) collides(field *Field, x, y int, angle Angle) bool {
return false
}
func (p *Piece) Collides(field *Field, x, y int, angle Angle) bool {
return p.collides(field, x, y, angle)
}
func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) {
size := len(p.blocks)
for i := 0; i < size; i++ {