From 9288c9dcc928c1865d2b9902a5fdb83ceed7369b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 29 Dec 2014 16:41:58 +0900 Subject: [PATCH] blocks: Gameover --- example/blocks/blocks/font.go | 6 ++++++ example/blocks/blocks/gamescene.go | 28 +++++++++++++++++++++++++++- example/blocks/blocks/piece.go | 4 ++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/example/blocks/blocks/font.go b/example/blocks/blocks/font.go index 11addcb31..2b5c089bd 100644 --- a/example/blocks/blocks/font.go +++ b/example/blocks/blocks/font.go @@ -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 diff --git a/example/blocks/blocks/gamescene.go b/example/blocks/blocks/gamescene.go index 3c32b8d02..58490ca86 100644 --- a/example/blocks/blocks/gamescene.go +++ b/example/blocks/blocks/gamescene.go @@ -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 } diff --git a/example/blocks/blocks/piece.go b/example/blocks/blocks/piece.go index d13ba4098..fdce04a7e 100644 --- a/example/blocks/blocks/piece.go +++ b/example/blocks/blocks/piece.go @@ -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++ {