From b303e6d357396e8a013752529d6e76356c21cb28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Sat, 2 Dec 2023 09:08:52 +0100 Subject: [PATCH] feat: render net --- internal/ball.go | 4 ++-- internal/match.go | 3 +++ internal/net.go | 24 ++++++++++++++++++++++++ internal/paddle.go | 2 +- internal/score.go | 5 ++--- 5 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 internal/net.go diff --git a/internal/ball.go b/internal/ball.go index e4a2ffb..dd9ab13 100644 --- a/internal/ball.go +++ b/internal/ball.go @@ -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 { diff --git a/internal/match.go b/internal/match.go index 501afb8..98c7e2f 100644 --- a/internal/match.go +++ b/internal/match.go @@ -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!" diff --git a/internal/net.go b/internal/net.go new file mode 100644 index 0000000..3d36301 --- /dev/null +++ b/internal/net.go @@ -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) + } +} diff --git a/internal/paddle.go b/internal/paddle.go index 1baa2bd..e7f23c1 100644 --- a/internal/paddle.go +++ b/internal/paddle.go @@ -43,7 +43,7 @@ func newLeftPaddle(screenWidth, screenHeight int, isControlledByPlayer bool) *pa const ( paddleBaseWidth = 10 - paddleBaseHeight = 100 + paddleBaseHeight = 75 paddleBaseSpeed = 1 ) diff --git a/internal/score.go b/internal/score.go index 03432ff..8fb85d1 100644 --- a/internal/score.go +++ b/internal/score.go @@ -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(