extracat brick

This commit is contained in:
Ruidy 2025-03-02 17:57:13 +01:00
parent 04eb14c01f
commit 947936cf6c
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
2 changed files with 51 additions and 43 deletions

50
brick.go Normal file
View file

@ -0,0 +1,50 @@
package main
import (
"image/color"
)
const (
brickWidth = 60
brickHeight = 20
brickRows = 5
brickCols = 10
brickGap = 4
brickOffset = 40
)
type brick struct {
x, y, width, height float32
active bool
color color.Color
score int
}
func initBricks() []brick {
brickConfig := []struct {
color color.Color
score int
}{
{color.RGBA{0xff, 0x00, 0x00, 0xff}, 50}, // Red
{color.RGBA{0xff, 0x7f, 0x00, 0xff}, 40}, // Orange
{color.RGBA{0xff, 0xff, 0x00, 0xff}, 30}, // Yellow
{color.RGBA{0x00, 0xff, 0x00, 0xff}, 20}, // Green
{color.RGBA{0x00, 0x00, 0xff, 0xff}, 10}, // Blue
}
bricks := make([]brick, 0, brickRows*brickCols)
for row := 0; row < brickRows; row++ {
for col := 0; col < brickCols; col++ {
bricks = append(bricks, brick{
x: float32(col*(brickWidth+brickGap) + brickGap),
y: float32(row*(brickHeight+brickGap) + brickGap + brickOffset),
width: brickWidth,
height: brickHeight,
active: true,
color: brickConfig[row].color,
score: brickConfig[row].score,
})
}
}
return bricks
}

44
main.go
View file

@ -16,16 +16,9 @@ const (
paddleHeight = 12
paddleWidth = 60
ballSize = 8
brickWidth = 60
brickHeight = 20
brickRows = 5
brickCols = 10
brickGap = 4
paddleSpeed = 5
ballSpeed = 3
paddleY = screenHeight - 40
ballStartY = screenHeight - 60
brickOffset = 40
)
type Game struct {
@ -45,13 +38,6 @@ type ball struct {
x, y, dx, dy, size float64
}
type brick struct {
x, y, width, height float64
active bool
color color.Color
score int
}
func (g *Game) init() {
// Initialize paddle
g.paddle = paddle{
@ -71,38 +57,10 @@ func (g *Game) init() {
}
// Initialize bricks
g.initBricks()
g.bricks = initBricks()
g.initialized = true
}
func (g *Game) initBricks() {
brickConfig := []struct {
color color.Color
score int
}{
{color.RGBA{0xff, 0x00, 0x00, 0xff}, 50}, // Red
{color.RGBA{0xff, 0x7f, 0x00, 0xff}, 40}, // Orange
{color.RGBA{0xff, 0xff, 0x00, 0xff}, 30}, // Yellow
{color.RGBA{0x00, 0xff, 0x00, 0xff}, 20}, // Green
{color.RGBA{0x00, 0x00, 0xff, 0xff}, 10}, // Blue
}
g.bricks = make([]brick, 0, brickRows*brickCols)
for row := 0; row < brickRows; row++ {
for col := 0; col < brickCols; col++ {
g.bricks = append(g.bricks, brick{
x: float64(col*(brickWidth+brickGap) + brickGap),
y: float64(row*(brickHeight+brickGap) + brickGap + brickOffset),
width: brickWidth,
height: brickHeight,
active: true,
color: brickConfig[row].color,
score: brickConfig[row].score,
})
}
}
}
func (g *Game) Update() error {
if !g.initialized {
g.init()