mirror of
https://github.com/rjNemo/breakout
synced 2026-06-12 11:36:38 +00:00
extracat brick
This commit is contained in:
parent
04eb14c01f
commit
947936cf6c
2 changed files with 51 additions and 43 deletions
50
brick.go
Normal file
50
brick.go
Normal 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
44
main.go
|
|
@ -16,16 +16,9 @@ const (
|
||||||
paddleHeight = 12
|
paddleHeight = 12
|
||||||
paddleWidth = 60
|
paddleWidth = 60
|
||||||
ballSize = 8
|
ballSize = 8
|
||||||
brickWidth = 60
|
|
||||||
brickHeight = 20
|
|
||||||
brickRows = 5
|
|
||||||
brickCols = 10
|
|
||||||
brickGap = 4
|
|
||||||
paddleSpeed = 5
|
|
||||||
ballSpeed = 3
|
ballSpeed = 3
|
||||||
paddleY = screenHeight - 40
|
paddleY = screenHeight - 40
|
||||||
ballStartY = screenHeight - 60
|
ballStartY = screenHeight - 60
|
||||||
brickOffset = 40
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
|
|
@ -45,13 +38,6 @@ type ball struct {
|
||||||
x, y, dx, dy, size float64
|
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() {
|
func (g *Game) init() {
|
||||||
// Initialize paddle
|
// Initialize paddle
|
||||||
g.paddle = paddle{
|
g.paddle = paddle{
|
||||||
|
|
@ -71,38 +57,10 @@ func (g *Game) init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize bricks
|
// Initialize bricks
|
||||||
g.initBricks()
|
g.bricks = initBricks()
|
||||||
g.initialized = true
|
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 {
|
func (g *Game) Update() error {
|
||||||
if !g.initialized {
|
if !g.initialized {
|
||||||
g.init()
|
g.init()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue