mirror of
https://github.com/rjNemo/breakout
synced 2026-06-06 00:26:39 +00:00
init: game
This commit is contained in:
commit
2e9bdd5dda
3 changed files with 253 additions and 0 deletions
15
go.mod
Normal file
15
go.mod
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
module breakout
|
||||||
|
|
||||||
|
go 1.21
|
||||||
|
|
||||||
|
require github.com/hajimehoshi/ebiten/v2 v2.6.6
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/ebitengine/purego v0.6.0 // indirect
|
||||||
|
github.com/jezek/xgb v1.1.0 // indirect
|
||||||
|
golang.org/x/exp/shiny v0.0.0-20230817173708-d852ddb80c63 // indirect
|
||||||
|
golang.org/x/image v0.12.0 // indirect
|
||||||
|
golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57 // indirect
|
||||||
|
golang.org/x/sync v0.3.0 // indirect
|
||||||
|
golang.org/x/sys v0.12.0 // indirect
|
||||||
|
)
|
||||||
47
go.sum
Normal file
47
go.sum
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
github.com/ebitengine/purego v0.6.0 h1:Yo9uBc1x+ETQbfEaf6wcBsjrQfCEnh/gaGUg7lguEJY=
|
||||||
|
github.com/ebitengine/purego v0.6.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
|
||||||
|
github.com/hajimehoshi/ebiten/v2 v2.6.6 h1:E5X87Or4VwKZIKjeC9+Vr4ComhZAz9h839myF4Q21kc=
|
||||||
|
github.com/hajimehoshi/ebiten/v2 v2.6.6/go.mod h1:gKgQI26zfoSb6j5QbrEz2L6nuHMbAYwrsXa5qsGrQKo=
|
||||||
|
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
|
||||||
|
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/exp/shiny v0.0.0-20230817173708-d852ddb80c63 h1:3AGKexOYqL+ztdWdkB1bDwXgPBuTS/S8A4WzuTvJ8Cg=
|
||||||
|
golang.org/x/exp/shiny v0.0.0-20230817173708-d852ddb80c63/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0=
|
||||||
|
golang.org/x/image v0.12.0 h1:w13vZbU4o5rKOFFR8y7M+c4A5jXDC0uXTdHYRP8X2DQ=
|
||||||
|
golang.org/x/image v0.12.0/go.mod h1:Lu90jvHG7GfemOIcldsh9A2hS01ocl6oNO7ype5mEnk=
|
||||||
|
golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57 h1:Q6NT8ckDYNcwmi/bmxe+XbiDMXqMRW1xFBtJ+bIpie4=
|
||||||
|
golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57/go.mod h1:wEyOn6VvNW7tcf+bW/wBz1sehi2s2BZ4TimyR7qZen4=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
|
||||||
|
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
|
||||||
|
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
191
main.go
Normal file
191
main.go
Normal file
|
|
@ -0,0 +1,191 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
screenWidth = 640
|
||||||
|
screenHeight = 480
|
||||||
|
paddleHeight = 12
|
||||||
|
paddleWidth = 60
|
||||||
|
ballSize = 8
|
||||||
|
brickWidth = 60
|
||||||
|
brickHeight = 20
|
||||||
|
brickRows = 5
|
||||||
|
brickCols = 10
|
||||||
|
brickGap = 4
|
||||||
|
)
|
||||||
|
|
||||||
|
type Game struct {
|
||||||
|
paddle paddle
|
||||||
|
ball ball
|
||||||
|
bricks []brick
|
||||||
|
score int
|
||||||
|
gameOver bool
|
||||||
|
initialized bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type paddle struct {
|
||||||
|
x, y, width, height float64
|
||||||
|
}
|
||||||
|
|
||||||
|
type ball struct {
|
||||||
|
x, y, dx, dy, size float64
|
||||||
|
}
|
||||||
|
|
||||||
|
type brick struct {
|
||||||
|
x, y, width, height float64
|
||||||
|
active bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) init() {
|
||||||
|
// Initialize paddle
|
||||||
|
g.paddle = paddle{
|
||||||
|
x: screenWidth/2 - paddleWidth/2,
|
||||||
|
y: screenHeight - 40,
|
||||||
|
width: paddleWidth,
|
||||||
|
height: paddleHeight,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize ball
|
||||||
|
g.ball = ball{
|
||||||
|
x: screenWidth / 2,
|
||||||
|
y: screenHeight - 60,
|
||||||
|
dx: 3,
|
||||||
|
dy: -3,
|
||||||
|
size: ballSize,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize bricks
|
||||||
|
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 + 40),
|
||||||
|
width: brickWidth,
|
||||||
|
height: brickHeight,
|
||||||
|
active: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
g.initialized = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Update() error {
|
||||||
|
if !g.initialized {
|
||||||
|
g.init()
|
||||||
|
}
|
||||||
|
|
||||||
|
if g.gameOver {
|
||||||
|
if ebiten.IsKeyPressed(ebiten.KeySpace) {
|
||||||
|
g.init()
|
||||||
|
g.gameOver = false
|
||||||
|
g.score = 0
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update paddle position based on input
|
||||||
|
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
|
||||||
|
g.paddle.x -= 5
|
||||||
|
}
|
||||||
|
if ebiten.IsKeyPressed(ebiten.KeyRight) {
|
||||||
|
g.paddle.x += 5
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep paddle within screen bounds
|
||||||
|
if g.paddle.x < 0 {
|
||||||
|
g.paddle.x = 0
|
||||||
|
}
|
||||||
|
if g.paddle.x > screenWidth-g.paddle.width {
|
||||||
|
g.paddle.x = screenWidth - g.paddle.width
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update ball position
|
||||||
|
g.ball.x += g.ball.dx
|
||||||
|
g.ball.y += g.ball.dy
|
||||||
|
|
||||||
|
// Ball collision with walls
|
||||||
|
if g.ball.x <= 0 || g.ball.x >= screenWidth-g.ball.size {
|
||||||
|
g.ball.dx = -g.ball.dx
|
||||||
|
}
|
||||||
|
if g.ball.y <= 0 {
|
||||||
|
g.ball.dy = -g.ball.dy
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ball collision with paddle
|
||||||
|
if g.ball.y+g.ball.size >= g.paddle.y &&
|
||||||
|
g.ball.x+g.ball.size >= g.paddle.x &&
|
||||||
|
g.ball.x <= g.paddle.x+g.paddle.width &&
|
||||||
|
g.ball.y <= g.paddle.y+g.paddle.height {
|
||||||
|
g.ball.dy = -g.ball.dy
|
||||||
|
// Add some randomness to the ball direction
|
||||||
|
g.ball.dx += (rand.Float64()*2 - 1) * 0.5
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ball collision with bricks
|
||||||
|
for i := range g.bricks {
|
||||||
|
if !g.bricks[i].active {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if g.ball.x+g.ball.size >= g.bricks[i].x &&
|
||||||
|
g.ball.x <= g.bricks[i].x+g.bricks[i].width &&
|
||||||
|
g.ball.y+g.ball.size >= g.bricks[i].y &&
|
||||||
|
g.ball.y <= g.bricks[i].y+g.bricks[i].height {
|
||||||
|
g.bricks[i].active = false
|
||||||
|
g.ball.dy = -g.ball.dy
|
||||||
|
g.score += 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for game over
|
||||||
|
if g.ball.y > screenHeight {
|
||||||
|
g.gameOver = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Draw(screen *ebiten.Image) {
|
||||||
|
// Draw paddle
|
||||||
|
ebitenutil.DrawRect(screen, g.paddle.x, g.paddle.y, g.paddle.width, g.paddle.height, color.White)
|
||||||
|
|
||||||
|
// Draw ball
|
||||||
|
ebitenutil.DrawRect(screen, g.ball.x, g.ball.y, g.ball.size, g.ball.size, color.White)
|
||||||
|
|
||||||
|
// Draw bricks
|
||||||
|
for _, brick := range g.bricks {
|
||||||
|
if brick.active {
|
||||||
|
ebitenutil.DrawRect(screen, brick.x, brick.y, brick.width, brick.height, color.RGBA{0xff, 0x40, 0x40, 0xff})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw score and game over text
|
||||||
|
if g.gameOver {
|
||||||
|
ebitenutil.DebugPrint(screen, "Game Over! Press SPACE to restart")
|
||||||
|
}
|
||||||
|
ebitenutil.DebugPrint(screen, "Score: "+strconv.Itoa(g.score))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||||
|
return screenWidth, screenHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ebiten.SetWindowSize(screenWidth, screenHeight)
|
||||||
|
ebiten.SetWindowTitle("Breakout")
|
||||||
|
|
||||||
|
game := &Game{}
|
||||||
|
if err := ebiten.RunGame(game); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue