code improvements

This commit is contained in:
Ruidy 2025-03-02 17:36:39 +01:00
parent ce648b3fdf
commit 04eb14c01f
No known key found for this signature in database
GPG key ID: E00F51288CB857CC

41
main.go
View file

@ -21,6 +21,11 @@ const (
brickRows = 5 brickRows = 5
brickCols = 10 brickCols = 10
brickGap = 4 brickGap = 4
paddleSpeed = 5
ballSpeed = 3
paddleY = screenHeight - 40
ballStartY = screenHeight - 60
brickOffset = 40
) )
type Game struct { type Game struct {
@ -42,16 +47,16 @@ type ball struct {
type brick struct { type brick struct {
x, y, width, height float64 x, y, width, height float64
active bool active bool
color color.Color color color.Color
score int score int
} }
func (g *Game) init() { func (g *Game) init() {
// Initialize paddle // Initialize paddle
g.paddle = paddle{ g.paddle = paddle{
x: screenWidth/2 - paddleWidth/2, x: screenWidth/2 - paddleWidth/2,
y: screenHeight - 40, y: paddleY,
width: paddleWidth, width: paddleWidth,
height: paddleHeight, height: paddleHeight,
} }
@ -59,31 +64,35 @@ func (g *Game) init() {
// Initialize ball // Initialize ball
g.ball = ball{ g.ball = ball{
x: screenWidth / 2, x: screenWidth / 2,
y: screenHeight - 60, y: ballStartY,
dx: 3, dx: ballSpeed,
dy: -3, dy: -ballSpeed,
size: ballSize, size: ballSize,
} }
// Rainbow colors and scores for the bricks // Initialize bricks
g.initBricks()
g.initialized = true
}
func (g *Game) initBricks() {
brickConfig := []struct { brickConfig := []struct {
color color.Color color color.Color
score int score int
}{ }{
{color.RGBA{0xff, 0x00, 0x00, 0xff}, 50}, // Red (top row) - highest score {color.RGBA{0xff, 0x00, 0x00, 0xff}, 50}, // Red
{color.RGBA{0xff, 0x7f, 0x00, 0xff}, 40}, // Orange {color.RGBA{0xff, 0x7f, 0x00, 0xff}, 40}, // Orange
{color.RGBA{0xff, 0xff, 0x00, 0xff}, 30}, // Yellow {color.RGBA{0xff, 0xff, 0x00, 0xff}, 30}, // Yellow
{color.RGBA{0x00, 0xff, 0x00, 0xff}, 20}, // Green {color.RGBA{0x00, 0xff, 0x00, 0xff}, 20}, // Green
{color.RGBA{0x00, 0x00, 0xff, 0xff}, 10}, // Blue (bottom row) - lowest score {color.RGBA{0x00, 0x00, 0xff, 0xff}, 10}, // Blue
} }
// Initialize bricks
g.bricks = make([]brick, 0, brickRows*brickCols) g.bricks = make([]brick, 0, brickRows*brickCols)
for row := 0; row < brickRows; row++ { for row := 0; row < brickRows; row++ {
for col := 0; col < brickCols; col++ { for col := 0; col < brickCols; col++ {
g.bricks = append(g.bricks, brick{ g.bricks = append(g.bricks, brick{
x: float64(col*(brickWidth+brickGap) + brickGap), x: float64(col*(brickWidth+brickGap) + brickGap),
y: float64(row*(brickHeight+brickGap) + brickGap + 40), y: float64(row*(brickHeight+brickGap) + brickGap + brickOffset),
width: brickWidth, width: brickWidth,
height: brickHeight, height: brickHeight,
active: true, active: true,
@ -92,8 +101,6 @@ func (g *Game) init() {
}) })
} }
} }
g.initialized = true
} }
func (g *Game) Update() error { func (g *Game) Update() error {
@ -112,10 +119,10 @@ func (g *Game) Update() error {
// Update paddle position based on input // Update paddle position based on input
if ebiten.IsKeyPressed(ebiten.KeyLeft) { if ebiten.IsKeyPressed(ebiten.KeyLeft) {
g.paddle.x -= 5 g.paddle.x -= paddleSpeed
} }
if ebiten.IsKeyPressed(ebiten.KeyRight) { if ebiten.IsKeyPressed(ebiten.KeyRight) {
g.paddle.x += 5 g.paddle.x += paddleSpeed
} }
// Keep paddle within screen bounds // Keep paddle within screen bounds
@ -204,4 +211,4 @@ func main() {
if err := ebiten.RunGame(game); err != nil { if err := ebiten.RunGame(game); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }