feat: random direction of the ball at the beginning of the match/after a restart #4
|
@ -3,6 +3,8 @@ package internal
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||||
|
@ -20,23 +22,25 @@ type ball struct {
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ballBaseRadius = 5
|
ballBaseRadius = 5
|
||||||
ballBaseSpeedX = 1
|
ballBaseSpeedX = 1.5
|
||||||
ballBaseSpeedY = 1
|
ballBaseSpeedY = 1.5
|
||||||
)
|
)
|
||||||
|
|
||||||
func newBall(screenWidth, screenHeight int) *ball {
|
func newBall(screenWidth, screenHeight int) *ball {
|
||||||
fScreenWidth := float32(screenWidth)
|
fScreenWidth := float32(screenWidth)
|
||||||
fScreenHeight := float32(screenHeight)
|
fScreenHeight := float32(screenHeight)
|
||||||
|
|
||||||
r := ballBaseRadius * fScreenHeight / BaseHeight
|
r := ballBaseRadius * fScreenHeight / BaseHeight
|
||||||
x := fScreenWidth/2.0 - r
|
x := fScreenWidth/2.0 - r
|
||||||
y := fScreenHeight/2.0 - r
|
y := fScreenHeight/2.0 - r
|
||||||
|
|
||||||
return &ball{
|
return &ball{
|
||||||
x: x,
|
x: x,
|
||||||
y: y,
|
y: y,
|
||||||
initX: x,
|
initX: x,
|
||||||
initY: y,
|
initY: y,
|
||||||
speedX: ballBaseSpeedX * fScreenWidth / BaseWidth,
|
speedX: randSpeedDirection() * ballBaseSpeedX * fScreenWidth / BaseWidth,
|
||||||
speedY: ballBaseSpeedY * fScreenHeight / BaseHeight,
|
speedY: randSpeedDirection() * ballBaseSpeedY * fScreenHeight / BaseHeight,
|
||||||
r: r,
|
r: r,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,6 +103,8 @@ func (b *ball) nextSpeed(bounds image.Rectangle, obstacles ...image.Rectangle) (
|
||||||
func (b *ball) reset() {
|
func (b *ball) reset() {
|
||||||
b.x = b.initX
|
b.x = b.initX
|
||||||
b.y = b.initY
|
b.y = b.initY
|
||||||
|
b.speedX *= randSpeedDirection()
|
||||||
|
b.speedY *= randSpeedDirection()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *ball) resize(x, y float32) {
|
func (b *ball) resize(x, y float32) {
|
||||||
|
@ -114,3 +120,14 @@ func (b *ball) resize(x, y float32) {
|
||||||
func (b *ball) bounds() image.Rectangle {
|
func (b *ball) bounds() image.Rectangle {
|
||||||
return image.Rect(int(b.x-b.r), int(b.y-b.r), int(b.x+b.r), int(b.y+b.r))
|
return image.Rect(int(b.x-b.r), int(b.y-b.r), int(b.x+b.r), int(b.y+b.r))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func randSpeedDirection() float32 {
|
||||||
|
//nolint:gosec
|
||||||
|
r := rand.New(rand.NewSource(time.Now().Unix()))
|
||||||
|
|
||||||
|
if r.Intn(101) >= 50 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user