feat: render net

This commit is contained in:
Dawid Wysokiński 2023-12-02 09:08:52 +01:00
parent 62a2658cd1
commit b303e6d357
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
5 changed files with 32 additions and 6 deletions

View File

@ -22,8 +22,8 @@ type ball struct {
const (
ballBaseRadius = 5
ballBaseSpeedX = 1.5
ballBaseSpeedY = 1.5
ballBaseSpeedX = 1.25
ballBaseSpeedY = 1.25
)
func newBall(screenWidth, screenHeight int) *ball {

View File

@ -22,6 +22,7 @@ type match struct {
ball *ball
leftPaddle *paddle
rightPaddle *paddle
net *net
score *score
fonts *fonts
}
@ -31,6 +32,7 @@ func newMatch(screenWidth, screenHeight int, f *fonts) *match {
ball: newBall(screenWidth, screenHeight),
leftPaddle: newLeftPaddle(screenWidth, screenHeight, true),
rightPaddle: newRightPaddle(screenWidth, screenHeight, false),
net: &net{},
score: &score{
fonts: f,
},
@ -59,6 +61,7 @@ func (m *match) draw(img *ebiten.Image) {
m.leftPaddle.draw(img)
m.rightPaddle.draw(img)
m.score.draw(img)
m.net.draw(img)
case matchStatusEnded:
bounds := img.Bounds()
winLoseMessage := "You win!"

24
internal/net.go Normal file
View File

@ -0,0 +1,24 @@
package internal
import (
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
type net struct {
}
func (n *net) draw(img *ebiten.Image) {
bounds := img.Bounds()
dy := float32(bounds.Dy())
dx := float32(bounds.Dx())
height := dy / 29
width := 1 * dx / BaseWidth
for offset := float32(0); offset < dy; offset += height * 2 {
vector.DrawFilledRect(img, dx/2-width/2, offset, width, height, color.White, false)
}
}

View File

@ -43,7 +43,7 @@ func newLeftPaddle(screenWidth, screenHeight int, isControlledByPlayer bool) *pa
const (
paddleBaseWidth = 10
paddleBaseHeight = 100
paddleBaseHeight = 75
paddleBaseSpeed = 1
)

View File

@ -6,7 +6,6 @@ import (
"strconv"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
)
type score struct {
@ -20,8 +19,8 @@ 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)
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(