mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-23 16:30:11 +01:00
Add score, level, lines
This commit is contained in:
parent
0346e81161
commit
b02bce4876
@ -84,3 +84,9 @@ func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func drawTextWithShadowRight(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) error {
|
||||||
|
w := textWidth(str) * scale
|
||||||
|
x += width - w
|
||||||
|
return drawTextWithShadow(rt, str, x, y, scale, clr)
|
||||||
|
}
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -42,6 +43,20 @@ func drawRect(r *ebiten.Image, x, y, width, height int) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func drawTextBox(r *ebiten.Image, label, content string, x, y, width int) error {
|
||||||
|
if err := drawTextWithShadow(r, label, x, y, 1, color.NRGBA{0x00, 0x00, 0x80, 0xff}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
y += blockWidth
|
||||||
|
if err := drawRect(r, x, y, width, 2*blockHeight); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := drawTextWithShadowRight(r, content, x, y+blockHeight*3/4, 1, color.White, width-blockWidth/2); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type GameScene struct {
|
type GameScene struct {
|
||||||
field *Field
|
field *Field
|
||||||
rand *rand.Rand
|
rand *rand.Rand
|
||||||
@ -53,6 +68,7 @@ type GameScene struct {
|
|||||||
nextPiece *Piece
|
nextPiece *Piece
|
||||||
landingCount int
|
landingCount int
|
||||||
currentFrame int
|
currentFrame int
|
||||||
|
score int
|
||||||
lines int
|
lines int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,6 +101,23 @@ func (s *GameScene) level() int {
|
|||||||
return s.lines / 10
|
return s.lines / 10
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *GameScene) addScore(lines int) {
|
||||||
|
base := 0
|
||||||
|
switch lines {
|
||||||
|
case 1:
|
||||||
|
base = 100
|
||||||
|
case 2:
|
||||||
|
base = 300
|
||||||
|
case 3:
|
||||||
|
base = 600
|
||||||
|
case 4:
|
||||||
|
base = 1000
|
||||||
|
default:
|
||||||
|
panic("not reach")
|
||||||
|
}
|
||||||
|
s.score += (s.level() + 1) * base
|
||||||
|
}
|
||||||
|
|
||||||
func (s *GameScene) Update(state *GameState) error {
|
func (s *GameScene) Update(state *GameState) error {
|
||||||
s.currentFrame++
|
s.currentFrame++
|
||||||
|
|
||||||
@ -115,6 +148,9 @@ func (s *GameScene) Update(state *GameState) error {
|
|||||||
if d := state.Input.StateForKey(ebiten.KeyDown); (d-1)%2 == 0 {
|
if d := state.Input.StateForKey(ebiten.KeyDown); (d-1)%2 == 0 {
|
||||||
s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
|
s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
|
||||||
moved = y != s.currentPieceY
|
moved = y != s.currentPieceY
|
||||||
|
if moved {
|
||||||
|
s.score++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drop the current piece with gravity.
|
// Drop the current piece with gravity.
|
||||||
@ -136,6 +172,9 @@ func (s *GameScene) Update(state *GameState) error {
|
|||||||
if maxLandingCount <= s.landingCount {
|
if maxLandingCount <= s.landingCount {
|
||||||
lines := s.field.AbsorbPiece(piece, s.currentPieceX, s.currentPieceY, angle)
|
lines := s.field.AbsorbPiece(piece, s.currentPieceX, s.currentPieceY, angle)
|
||||||
s.lines += lines
|
s.lines += lines
|
||||||
|
if 0 < lines {
|
||||||
|
s.addScore(lines)
|
||||||
|
}
|
||||||
s.initCurrentPiece(s.nextPiece)
|
s.initCurrentPiece(s.nextPiece)
|
||||||
s.nextPiece = nil
|
s.nextPiece = nil
|
||||||
s.landingCount = 0
|
s.landingCount = 0
|
||||||
@ -164,10 +203,31 @@ func (s *GameScene) Draw(r *ebiten.Image) error {
|
|||||||
}
|
}
|
||||||
nextX := x
|
nextX := x
|
||||||
nextY := y + blockHeight
|
nextY := y + blockHeight
|
||||||
if err := drawRect(r, nextX, nextY, blockWidth*5, blockHeight*5); err != nil {
|
if err := drawRect(r, nextX, nextY, 5*blockWidth, 5*blockHeight); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
x = nextX
|
||||||
|
y = nextY + 5*blockHeight + blockHeight
|
||||||
|
|
||||||
|
// Draw score
|
||||||
|
width := ScreenWidth - 2*blockWidth - x
|
||||||
|
if err := drawTextBox(r, "SCORE", strconv.Itoa(s.score), x, y, width); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw level
|
||||||
|
y += 4 * blockHeight
|
||||||
|
if err := drawTextBox(r, "LEVEL", strconv.Itoa(s.level()), x, y, width); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw lines
|
||||||
|
y += 4 * blockHeight
|
||||||
|
if err := drawTextBox(r, "LINES", strconv.Itoa(s.lines), x, y, width); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw blocks
|
||||||
if err := s.field.Draw(r, fieldX, fieldY); err != nil {
|
if err := s.field.Draw(r, fieldX, fieldY); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user