tennis-game/internal/score.go

67 lines
1.1 KiB
Go

package internal
import (
"image"
"image/color"
"strconv"
"github.com/hajimehoshi/ebiten/v2"
)
type score struct {
left int
right int
fonts *fonts
}
var scoreTextColor = color.White
func (s *score) draw(img *ebiten.Image) {
bounds := img.Bounds()
y := bounds.Max.Y / 4
drawCenteredText(img, strconv.Itoa(s.left), s.fonts.robotoRegularFont, bounds.Max.X/4, y, scoreTextColor)
drawCenteredText(img, strconv.Itoa(s.right), s.fonts.robotoRegularFont, bounds.Max.X*3/4, y, scoreTextColor)
}
func (s *score) update(
bounds image.Rectangle,
b *ball,
leftPaddle *paddle,
rightPaddle *paddle,
) error {
ballBounds := b.bounds()
if ballBounds.
Intersect(image.Rect(
int(leftPaddle.x),
bounds.Min.Y,
int(leftPaddle.width/2),
bounds.Max.Y,
)) != (image.Rectangle{}) {
s.right++
b.reset()
leftPaddle.reset()
rightPaddle.reset()
}
if ballBounds.
Intersect(image.Rect(
int(rightPaddle.x+rightPaddle.width/2),
bounds.Min.Y,
bounds.Max.X,
bounds.Max.Y,
)) != (image.Rectangle{}) {
s.left++
b.reset()
leftPaddle.reset()
rightPaddle.reset()
}
return nil
}
func (s *score) reset() {
s.left = 0
s.right = 0
}