Fix game over panic
This commit is contained in:
parent
545a76109e
commit
6f5081ab57
|
@ -21,12 +21,12 @@ func InitBoard() *Board {
|
|||
board := &Board{
|
||||
NextPiece: elements.GetRandomPiece(),
|
||||
}
|
||||
board.getNextPiece()
|
||||
_ = board.getNextPiece()
|
||||
|
||||
return board
|
||||
}
|
||||
|
||||
func (b *Board) getNextPiece() bool {
|
||||
func (b *Board) getNextPiece() error {
|
||||
b.AlreadyHeld = false
|
||||
b.ActivePiecePos = rl.NewVector2(5, 0)
|
||||
b.ActivePiece = b.NextPiece
|
||||
|
@ -34,20 +34,25 @@ func (b *Board) getNextPiece() bool {
|
|||
for _, block := range b.ActivePiece.Blocks {
|
||||
newX, newY := b.ActivePiecePos.X+block.Position.X, b.ActivePiecePos.Y+block.Position.Y
|
||||
if newY >= 0 && b.Board[int(newX)][int(newY)] != nil {
|
||||
return true
|
||||
return ErrGameOver
|
||||
}
|
||||
}
|
||||
return false
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Board) setBlocksFromActivePiece() {
|
||||
func (b *Board) setBlocksFromActivePiece() error {
|
||||
for _, block := range b.ActivePiece.Blocks {
|
||||
newX, newY := b.ActivePiecePos.X+block.Position.X, b.ActivePiecePos.Y+block.Position.Y
|
||||
if newY < 0 {
|
||||
return ErrGameOver
|
||||
}
|
||||
b.Board[int(newX)][int(newY)] = &elements.Block{
|
||||
Position: rl.NewVector2(newX, newY),
|
||||
Color: b.ActivePiece.Blocks[0].Color,
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Board) checkHorizontalCollision(left bool) bool {
|
||||
|
@ -73,37 +78,43 @@ func (b *Board) checkHorizontalCollision(left bool) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (b *Board) descendActivePiece() bool {
|
||||
func (b *Board) descendActivePiece() (bool, error) {
|
||||
b.ActivePiecePos.Y += 1
|
||||
for _, activeBlock := range b.ActivePiece.Blocks {
|
||||
if b.ActivePiecePos.Y+activeBlock.Position.Y == 20 {
|
||||
if b.ActivePiecePos.Y+activeBlock.Position.Y == 20 ||
|
||||
b.Board[int(b.ActivePiecePos.X+activeBlock.Position.X)][int(b.ActivePiecePos.Y+activeBlock.Position.Y)] != nil {
|
||||
b.ActivePiecePos.Y -= 1
|
||||
b.setBlocksFromActivePiece()
|
||||
return true
|
||||
}
|
||||
|
||||
if b.Board[int(b.ActivePiecePos.X+activeBlock.Position.X)][int(b.ActivePiecePos.Y+activeBlock.Position.Y)] != nil {
|
||||
b.ActivePiecePos.Y -= 1
|
||||
b.setBlocksFromActivePiece()
|
||||
return true
|
||||
return true, b.setBlocksFromActivePiece()
|
||||
}
|
||||
}
|
||||
return false
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// DescendActivePiece moves the piece down by one block. If the piece cannot move down, it will be placed on the board.
|
||||
// After that, a new piece will be generated. If the new piece cannot be placed, true is returned.
|
||||
func (b *Board) DescendActivePiece() bool {
|
||||
if b.descendActivePiece() {
|
||||
// After that, a new piece will be generated. If the new piece cannot be placed, a game over error is returned.
|
||||
func (b *Board) DescendActivePiece() error {
|
||||
placed, err := b.descendActivePiece()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if placed {
|
||||
return b.getNextPiece()
|
||||
}
|
||||
return false
|
||||
return nil
|
||||
}
|
||||
|
||||
// DropActivePiece moves the piece down until it cannot move down anymore. After that, a new piece will be generated.
|
||||
// If the new piece cannot be placed, true is returned.
|
||||
func (b *Board) DropActivePiece() bool {
|
||||
for !b.descendActivePiece() {
|
||||
// If the new piece cannot be placed, a game over error is returned.
|
||||
func (b *Board) DropActivePiece() error {
|
||||
for {
|
||||
placed, err := b.descendActivePiece()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if placed {
|
||||
break
|
||||
}
|
||||
}
|
||||
return b.getNextPiece()
|
||||
}
|
||||
|
@ -197,7 +208,7 @@ func (b *Board) HoldPiece() {
|
|||
b.AlreadyHeld = true
|
||||
if b.HeldPiece == nil {
|
||||
b.HeldPiece = b.ActivePiece
|
||||
b.getNextPiece()
|
||||
_ = b.getNextPiece()
|
||||
return
|
||||
}
|
||||
b.HeldPiece, b.ActivePiece = b.ActivePiece, b.HeldPiece
|
||||
|
|
18
game/game.go
18
game/game.go
|
@ -1,11 +1,13 @@
|
|||
package game
|
||||
|
||||
import (
|
||||
"gitea.theedgeofrage.com/theedgeofrage/yeetris/elements"
|
||||
"github.com/gen2brain/raylib-go/raylib"
|
||||
"github.com/go-errors/errors"
|
||||
|
||||
"gitea.theedgeofrage.com/theedgeofrage/yeetris/elements"
|
||||
)
|
||||
|
||||
const DEBUG = false
|
||||
var ErrGameOver = errors.New("game over")
|
||||
|
||||
// Game type
|
||||
type Game struct {
|
||||
|
@ -33,6 +35,7 @@ func (g *Game) Init() {
|
|||
}
|
||||
|
||||
func (g *Game) Update() {
|
||||
var err error
|
||||
defer g.Board.ClearLines()
|
||||
if g.GameOver {
|
||||
if rl.IsKeyPressed(rl.KeyEnter) {
|
||||
|
@ -62,13 +65,16 @@ func (g *Game) Update() {
|
|||
g.Board.RotateActivePiece(true)
|
||||
}
|
||||
if rl.IsKeyPressed(rl.KeyN) {
|
||||
g.Board.DescendActivePiece()
|
||||
err = g.Board.DescendActivePiece()
|
||||
}
|
||||
if rl.IsKeyPressed(rl.KeySpace) {
|
||||
g.Board.DropActivePiece()
|
||||
err = g.Board.DropActivePiece()
|
||||
}
|
||||
if g.FramesCounter%g.TickRate == 0 && !DEBUG {
|
||||
g.Board.DescendActivePiece()
|
||||
if g.FramesCounter%g.TickRate == 0 {
|
||||
err = g.Board.DescendActivePiece()
|
||||
}
|
||||
if errors.Is(err, ErrGameOver) {
|
||||
g.GameOver = true
|
||||
}
|
||||
g.FramesCounter++
|
||||
}
|
||||
|
|
5
go.mod
5
go.mod
|
@ -2,7 +2,10 @@ module gitea.theedgeofrage.com/theedgeofrage/yeetris
|
|||
|
||||
go 1.21.6
|
||||
|
||||
require github.com/gen2brain/raylib-go/raylib v0.0.0-20240125111008-83d871a38f28
|
||||
require (
|
||||
github.com/gen2brain/raylib-go/raylib v0.0.0-20240125111008-83d871a38f28
|
||||
github.com/go-errors/errors v1.5.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -2,5 +2,7 @@ github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa h1:Ik7
|
|||
github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
|
||||
github.com/gen2brain/raylib-go/raylib v0.0.0-20240125111008-83d871a38f28 h1:e0HOaKlvsdC0ZtfWbEaaWAxYTSxUZoCAFS36jLfsVRA=
|
||||
github.com/gen2brain/raylib-go/raylib v0.0.0-20240125111008-83d871a38f28/go.mod h1:P/hDjVwz/9fhR0ww3+umzDpDA7Bf7Tce4xNChHIEFqE=
|
||||
github.com/go-errors/errors v1.5.1 h1:ZwEMSLRCapFLflTpT7NKaAc7ukJ8ZPEjzlxt8rPN8bk=
|
||||
github.com/go-errors/errors v1.5.1/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
|
||||
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
|
||||
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
|
|
Loading…
Reference in New Issue