summaryrefslogtreecommitdiffstats
path: root/pongo.go
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-08-26 20:47:27 -0700
committerRose Hogenson <rosehogenson@posteo.net>2024-08-26 20:47:27 -0700
commit1a386dc5ff5cf88182e930d19965dda2692551dc (patch)
tree1cd1c7ee66b19b97028d3b082d1aee7dd3bab65b /pongo.go
parentUse GPU rendering and fix FPS limiter. (diff)
downloadpongo-1a386dc5ff5cf88182e930d19965dda2692551dc.tar.zst
Fix bounce logic.
Diffstat (limited to 'pongo.go')
-rw-r--r--pongo.go14
1 files changed, 5 insertions, 9 deletions
diff --git a/pongo.go b/pongo.go
index cb0ebe3..2a68a0d 100644
--- a/pongo.go
+++ b/pongo.go
@@ -192,10 +192,9 @@ func pong() error {
}
}
- oldPos := ball.pos
ball.pos = ball.pos.add(ball.velocity)
switch {
- case ball.bounds().HasIntersection(paddle1.bounds()):
+ case ball.bounds().HasIntersection(paddle1.bounds()) && ball.velocity.x <= 0:
ping.Play(-1, 0)
d := ball.pos.y - paddle1.pos
if d < 0 {
@@ -207,8 +206,7 @@ func pong() error {
}
speed := ballSpeed * (1 + 4*d/(float64(paddle1.bounds().H)/2+float64(ball.bounds().H)))
ball.velocity = vector2{speed * math.Cos(angle), speed * math.Sin(angle)}
- ball.pos = oldPos
- case ball.bounds().HasIntersection(paddle2.bounds()):
+ case ball.bounds().HasIntersection(paddle2.bounds()) && ball.velocity.x >= 0:
ping.Play(-1, 0)
d := ball.pos.y - paddle2.pos
if d < 0 {
@@ -221,16 +219,14 @@ func pong() error {
angle += math.Pi
speed := ballSpeed * (1 + 4*d/(float64(paddle2.bounds().H)/2+float64(ball.bounds().H)))
ball.velocity = vector2{speed * math.Cos(angle), speed * math.Sin(angle)}
- ball.pos = oldPos
case rectRight(ball.bounds()) <= 0:
aiScore++
ball.reset(right)
case ball.bounds().X >= screenWidth:
playerScore++
ball.reset(left)
- case ball.bounds().Y <= 0 || rectBottom(ball.bounds()) >= screenHeight:
+ case ball.bounds().Y <= 0 && ball.velocity.y < 0 || rectBottom(ball.bounds()) >= screenHeight && ball.velocity.y > 0:
ping.Play(-1, 0)
- ball.pos = oldPos
ball.velocity.y = -ball.velocity.y
}
}
@@ -261,10 +257,10 @@ func pong() error {
fmt.Fprintln(os.Stderr, err)
}
- if err := numbers.Draw(renderer, playerScore, paddle1.bounds().X+20, 30); err != nil {
+ if err := numbers.Draw(renderer, playerScore, 150, 30); err != nil {
fmt.Fprintln(os.Stderr, err)
}
- if err := numbers.Draw(renderer, aiScore, paddle2.bounds().X-500, 30); err != nil {
+ if err := numbers.Draw(renderer, aiScore, 1300, 30); err != nil {
fmt.Fprintln(os.Stderr, err)
}