Add scoring

This commit is contained in:
Pavle Portic 2024-01-31 11:48:44 +01:00
parent 6f5081ab57
commit ef58dba80a
Signed by: TheEdgeOfRage
GPG Key ID: 66AD4BA646FBC0D2
2 changed files with 23 additions and 6 deletions

View File

@ -216,7 +216,8 @@ func (b *Board) HoldPiece() {
}
// ClearLines clears all lines that are completely filled.
func (g *Board) ClearLines() {
func (g *Board) ClearLines() int {
clearedLines := 0
for y := 19; y >= 0; {
skip := false
for x := 0; x < 10; x++ {
@ -229,8 +230,15 @@ func (g *Board) ClearLines() {
y--
continue
}
for ty := y; ty > 0; ty-- {
clearedLines++
for ty := y; ty >= 0; ty-- {
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 {
g.Board[x][ty] = g.Board[x][ty-1]
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) {

View File

@ -36,7 +36,6 @@ func (g *Game) Init() {
func (g *Game) Update() {
var err error
defer g.Board.ClearLines()
if g.GameOver {
if rl.IsKeyPressed(rl.KeyEnter) {
g.Init()
@ -76,6 +75,18 @@ func (g *Game) Update() {
if errors.Is(err, ErrGameOver) {
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++
}