feat: random direction of the ball at the beginning of the match/after a restart (#4)
Reviewed-on: #4
This commit is contained in:
parent
1f7a8aed38
commit
88ecbf6362
|
@ -3,6 +3,8 @@ package internal
|
|||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
|
@ -20,23 +22,25 @@ type ball struct {
|
|||
|
||||
const (
|
||||
ballBaseRadius = 5
|
||||
ballBaseSpeedX = 1
|
||||
ballBaseSpeedY = 1
|
||||
ballBaseSpeedX = 1.5
|
||||
ballBaseSpeedY = 1.5
|
||||
)
|
||||
|
||||
func newBall(screenWidth, screenHeight int) *ball {
|
||||
fScreenWidth := float32(screenWidth)
|
||||
fScreenHeight := float32(screenHeight)
|
||||
|
||||
r := ballBaseRadius * fScreenHeight / BaseHeight
|
||||
x := fScreenWidth/2.0 - r
|
||||
y := fScreenHeight/2.0 - r
|
||||
|
||||
return &ball{
|
||||
x: x,
|
||||
y: y,
|
||||
initX: x,
|
||||
initY: y,
|
||||
speedX: ballBaseSpeedX * fScreenWidth / BaseWidth,
|
||||
speedY: ballBaseSpeedY * fScreenHeight / BaseHeight,
|
||||
speedX: randSpeedDirection() * ballBaseSpeedX * fScreenWidth / BaseWidth,
|
||||
speedY: randSpeedDirection() * ballBaseSpeedY * fScreenHeight / BaseHeight,
|
||||
r: r,
|
||||
}
|
||||
}
|
||||
|
@ -99,6 +103,8 @@ func (b *ball) nextSpeed(bounds image.Rectangle, obstacles ...image.Rectangle) (
|
|||
func (b *ball) reset() {
|
||||
b.x = b.initX
|
||||
b.y = b.initY
|
||||
b.speedX *= randSpeedDirection()
|
||||
b.speedY *= randSpeedDirection()
|
||||
}
|
||||
|
||||
func (b *ball) resize(x, y float32) {
|
||||
|
@ -114,3 +120,14 @@ func (b *ball) resize(x, y float32) {
|
|||
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))
|
||||
}
|
||||
|
||||
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