2023-11-30 06:28:09 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/text"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
text.Draw(img, strconv.Itoa(s.left), s.fonts.robotoRegularFont, bounds.Max.X/4, y, scoreTextColor)
|
|
|
|
text.Draw(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
|
|
|
|
}
|
2023-12-01 06:19:38 +00:00
|
|
|
|
|
|
|
func (s *score) reset() {
|
|
|
|
s.left = 0
|
|
|
|
s.right = 0
|
|
|
|
}
|