From ffe9242a73abfb3e8ee9b6effd57e64155717bfb Mon Sep 17 00:00:00 2001 From: yinqiang Date: Sat, 7 Jun 2025 22:50:07 +0800 Subject: [PATCH] fix IBlock disappear issue --- block.go | 21 +++++++++++---------- game.go | 26 +++++++++++++++----------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/block.go b/block.go index ad276ea..0b3aaee 100644 --- a/block.go +++ b/block.go @@ -14,7 +14,7 @@ type Block struct { type BlockType int const ( - IBlock BlockType = iota + IBlock BlockType = iota + 1 JBlock LBlock OBlock @@ -32,13 +32,14 @@ type Tetromino struct { // Colors for different block types var BlockColors = map[BlockType]color.Color{ - IBlock: color.RGBA{0, 255, 255, 255}, // Cyan - JBlock: color.RGBA{0, 0, 255, 255}, // Blue - LBlock: color.RGBA{255, 165, 0, 255}, // Orange - OBlock: color.RGBA{255, 255, 0, 255}, // Yellow - SBlock: color.RGBA{0, 255, 0, 255}, // Green - TBlock: color.RGBA{128, 0, 128, 255}, // Purple - ZBlock: color.RGBA{255, 0, 0, 255}, // Red + EmptyBlock: color.RGBA{0, 0, 0, 0}, // Black + IBlock: color.RGBA{0, 255, 255, 255}, // Cyan + JBlock: color.RGBA{0, 0, 255, 255}, // Blue + LBlock: color.RGBA{255, 165, 0, 255}, // Orange + OBlock: color.RGBA{255, 255, 0, 255}, // Yellow + SBlock: color.RGBA{0, 255, 0, 255}, // Green + TBlock: color.RGBA{128, 0, 128, 255}, // Purple + ZBlock: color.RGBA{255, 0, 0, 255}, // Red } // TetrominoShapes defines the shape of each tetromino type @@ -102,8 +103,8 @@ func NewTetromino(blockType BlockType, x, y int) *Tetromino { shape := TetrominoShapes[blockType] blocks := make([]Block, 0) - for i := 0; i < len(shape); i++ { - for j := 0; j < len(shape[i]); j++ { + for i := range shape { + for j := range shape[i] { if shape[i][j] { blocks = append(blocks, Block{ X: j, diff --git a/game.go b/game.go index aa9896f..2aa98e9 100644 --- a/game.go +++ b/game.go @@ -24,6 +24,7 @@ const ( // Game constants InitialDropInterval = 60 MinDropInterval = 5 + EmptyBlock = 0 ) // Game represents the main game state @@ -90,9 +91,9 @@ func (g *Game) Draw(screen *ebiten.Image) { } // Draw placed blocks - for y := 0; y < BoardHeight; y++ { - for x := 0; x < BoardWidth; x++ { - if g.board[y][x] != 0 { + for y := range BoardHeight { + for x := range BoardWidth { + if g.board[y][x] != EmptyBlock { g.drawBlock(screen, x, y, g.board[y][x]) } } @@ -311,7 +312,7 @@ func (g *Game) isColliding() bool { } // Check collision with other pieces if y >= 0 && x >= 0 && x < BoardWidth && y < BoardHeight { - if g.board[y][x] != 0 { + if g.board[y][x] != EmptyBlock { return true } } @@ -324,6 +325,9 @@ func (g *Game) lockPiece() { if g.currentPiece == nil { return } + // if g.currentPiece.BlockType == IBlock { + // g.currentPiece.BlockType = IBlock + // } positions := g.currentPiece.GetAbsolutePositions() for _, pos := range positions { @@ -378,8 +382,8 @@ func (g *Game) clearLines() { // isLineFull checks if a line is completely filled func (g *Game) isLineFull(y int) bool { - for x := 0; x < BoardWidth; x++ { - if g.board[y][x] == 0 { + for x := range BoardWidth { + if g.board[y][x] == EmptyBlock { return false } } @@ -391,15 +395,15 @@ func (g *Game) removeLine(y int) { for i := y; i > 0; i-- { copy(g.board[i], g.board[i-1]) } - for x := 0; x < BoardWidth; x++ { - g.board[0][x] = 0 + for x := range BoardWidth { + g.board[0][x] = EmptyBlock } } // spawnNewPiece creates a new piece at the top of the board func (g *Game) spawnNewPiece() { if g.nextPiece == nil { - g.nextPiece = NewTetromino(BlockType(rand.Intn(7)), 0, 0) + g.nextPiece = NewTetromino(BlockType(rand.Intn(7)+1), 0, 0) } g.currentPiece = g.nextPiece @@ -409,7 +413,7 @@ func (g *Game) spawnNewPiece() { } g.currentPiece.Y = 0 - g.nextPiece = NewTetromino(BlockType(rand.Intn(7)), 0, 0) + g.nextPiece = NewTetromino(BlockType(rand.Intn(7)+1), 0, 0) if g.isColliding() { g.gameOver = true @@ -423,7 +427,7 @@ func (g *Game) wouldCollide(piece *Tetromino) bool { if x < 0 || x >= BoardWidth || y >= BoardHeight { return true } - if y >= 0 && y < BoardHeight && x >= 0 && x < BoardWidth && g.board[y][x] != 0 { + if y >= 0 && y < BoardHeight && x >= 0 && x < BoardWidth && g.board[y][x] != EmptyBlock { return true } } -- 2.39.5 (Apple Git-154)