mirror of
https://github.com/rjNemo/breakout
synced 2026-06-06 00:26:39 +00:00
add ball
This commit is contained in:
parent
2964884563
commit
0573a60c29
2 changed files with 52 additions and 31 deletions
48
ball.go
Normal file
48
ball.go
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "math/rand"
|
||||||
|
|
||||||
|
const (
|
||||||
|
ballSize = 8
|
||||||
|
ballSpeed = 3
|
||||||
|
ballStartY = screenHeight - 60
|
||||||
|
)
|
||||||
|
|
||||||
|
type ball struct {
|
||||||
|
x, y, dx, dy, size float32
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBall() ball {
|
||||||
|
return ball{
|
||||||
|
x: screenWidth / 2,
|
||||||
|
y: ballStartY,
|
||||||
|
dx: ballSpeed,
|
||||||
|
dy: -ballSpeed,
|
||||||
|
size: ballSize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ball) updatePosition() {
|
||||||
|
b.x += b.dx
|
||||||
|
b.y += b.dy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ball) checkWallCollision() {
|
||||||
|
if b.x <= 0 || b.x >= screenWidth-b.size {
|
||||||
|
b.dx = -b.dx
|
||||||
|
}
|
||||||
|
if b.y <= 0 {
|
||||||
|
b.dy = -b.dy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ball) checkPaddleCollision(p paddle) {
|
||||||
|
if b.y+b.size >= p.y &&
|
||||||
|
b.x+b.size >= p.x &&
|
||||||
|
b.x <= p.x+p.width &&
|
||||||
|
b.y <= p.y+p.height {
|
||||||
|
b.dy = -b.dy
|
||||||
|
// Add some randomness to the ball direction
|
||||||
|
b.dx += (rand.Float32()*2 - 1) * 0.5
|
||||||
|
}
|
||||||
|
}
|
||||||
35
main.go
35
main.go
|
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"image/color"
|
"image/color"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
|
@ -13,9 +12,6 @@ import (
|
||||||
const (
|
const (
|
||||||
screenWidth = 640
|
screenWidth = 640
|
||||||
screenHeight = 480
|
screenHeight = 480
|
||||||
ballSize = 8
|
|
||||||
ballSpeed = 3
|
|
||||||
ballStartY = screenHeight - 60
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
|
|
@ -27,22 +23,12 @@ type Game struct {
|
||||||
initialized bool
|
initialized bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type ball struct {
|
|
||||||
x, y, dx, dy, size float64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Game) init() {
|
func (g *Game) init() {
|
||||||
// Initialize paddle
|
// Initialize paddle
|
||||||
g.paddle = newPaddle()
|
g.paddle = newPaddle()
|
||||||
|
|
||||||
// Initialize ball
|
// Initialize ball
|
||||||
g.ball = ball{
|
g.ball = newBall()
|
||||||
x: screenWidth / 2,
|
|
||||||
y: ballStartY,
|
|
||||||
dx: ballSpeed,
|
|
||||||
dy: -ballSpeed,
|
|
||||||
size: ballSize,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize bricks
|
// Initialize bricks
|
||||||
g.bricks = initBricks()
|
g.bricks = initBricks()
|
||||||
|
|
@ -75,26 +61,13 @@ func (g *Game) Update() error {
|
||||||
g.paddle.keepWithinBounds()
|
g.paddle.keepWithinBounds()
|
||||||
|
|
||||||
// Update ball position
|
// Update ball position
|
||||||
g.ball.x += g.ball.dx
|
g.ball.updatePosition()
|
||||||
g.ball.y += g.ball.dy
|
|
||||||
|
|
||||||
// Ball collision with walls
|
// Ball collision with walls
|
||||||
if g.ball.x <= 0 || g.ball.x >= screenWidth-g.ball.size {
|
g.ball.checkWallCollision()
|
||||||
g.ball.dx = -g.ball.dx
|
|
||||||
}
|
|
||||||
if g.ball.y <= 0 {
|
|
||||||
g.ball.dy = -g.ball.dy
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ball collision with paddle
|
// Ball collision with paddle
|
||||||
if g.ball.y+g.ball.size >= g.paddle.y &&
|
g.ball.checkPaddleCollision(g.paddle)
|
||||||
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
|
// Ball collision with bricks
|
||||||
for i := range g.bricks {
|
for i := range g.bricks {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue