Add scoring
This commit is contained in:
parent
6f5081ab57
commit
ef58dba80a
|
@ -216,7 +216,8 @@ func (b *Board) HoldPiece() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClearLines clears all lines that are completely filled.
|
// ClearLines clears all lines that are completely filled.
|
||||||
func (g *Board) ClearLines() {
|
func (g *Board) ClearLines() int {
|
||||||
|
clearedLines := 0
|
||||||
for y := 19; y >= 0; {
|
for y := 19; y >= 0; {
|
||||||
skip := false
|
skip := false
|
||||||
for x := 0; x < 10; x++ {
|
for x := 0; x < 10; x++ {
|
||||||
|
@ -229,8 +230,15 @@ func (g *Board) ClearLines() {
|
||||||
y--
|
y--
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for ty := y; ty > 0; ty-- {
|
clearedLines++
|
||||||
|
for ty := y; ty >= 0; ty-- {
|
||||||
for x := 0; x < 10; x++ {
|
for x := 0; x < 10; x++ {
|
||||||
|
// always clear top line
|
||||||
|
if ty == 0 {
|
||||||
|
g.Board[x][ty] = nil
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// move all blocks above cleared line down
|
||||||
if g.Board[x][ty-1] != nil {
|
if g.Board[x][ty-1] != nil {
|
||||||
g.Board[x][ty] = g.Board[x][ty-1]
|
g.Board[x][ty] = g.Board[x][ty-1]
|
||||||
g.Board[x][ty].Position.Y++
|
g.Board[x][ty].Position.Y++
|
||||||
|
@ -239,10 +247,8 @@ func (g *Board) ClearLines() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for x := 0; x < 10; x++ {
|
|
||||||
g.Board[x][0] = nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return clearedLines
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Board) DrawGrid(offset, scale rl.Vector2) {
|
func (b *Board) DrawGrid(offset, scale rl.Vector2) {
|
||||||
|
|
13
game/game.go
13
game/game.go
|
@ -36,7 +36,6 @@ func (g *Game) Init() {
|
||||||
|
|
||||||
func (g *Game) Update() {
|
func (g *Game) Update() {
|
||||||
var err error
|
var err error
|
||||||
defer g.Board.ClearLines()
|
|
||||||
if g.GameOver {
|
if g.GameOver {
|
||||||
if rl.IsKeyPressed(rl.KeyEnter) {
|
if rl.IsKeyPressed(rl.KeyEnter) {
|
||||||
g.Init()
|
g.Init()
|
||||||
|
@ -76,6 +75,18 @@ func (g *Game) Update() {
|
||||||
if errors.Is(err, ErrGameOver) {
|
if errors.Is(err, ErrGameOver) {
|
||||||
g.GameOver = true
|
g.GameOver = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clearedLines := g.Board.ClearLines()
|
||||||
|
switch clearedLines {
|
||||||
|
case 1:
|
||||||
|
g.UI.Score += 40
|
||||||
|
case 2:
|
||||||
|
g.UI.Score += 100
|
||||||
|
case 3:
|
||||||
|
g.UI.Score += 300
|
||||||
|
case 4:
|
||||||
|
g.UI.Score += 1200
|
||||||
|
}
|
||||||
g.FramesCounter++
|
g.FramesCounter++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue