From ec042af12a8f9f9ba64ec0c54fb77ba5fd77e704 Mon Sep 17 00:00:00 2001 From: Brandon <103316367+okbrandon@users.noreply.github.com> Date: Sat, 12 Oct 2024 14:13:57 +0200 Subject: [PATCH] game: Limiting ball speed for a playable gameplay --- backend/api/consumers.py | 13 +++++++++---- frontend/src/components/Game/local/GameLocal.js | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/backend/api/consumers.py b/backend/api/consumers.py index a362b757..e0383235 100644 --- a/backend/api/consumers.py +++ b/backend/api/consumers.py @@ -701,6 +701,7 @@ async def run_match_loop(self, match_id): PADDLE_HEIGHT = 60 BALL_RADIUS = 25 / 2 BALL_SPEED = 0.6 + BALL_MAX_SPEED = 17 MAX_SCORE = 10 while match_id in self.active_matches: @@ -718,16 +719,20 @@ async def run_match_loop(self, match_id): match_state['ball']['y'] - BALL_RADIUS <= match_state['playerA']['paddle_y'] + PADDLE_HEIGHT and # Ball's bottom is above paddle's bottom match_state['ball']['y'] + BALL_RADIUS >= match_state['playerA']['paddle_y'] - PADDLE_HEIGHT): # Ball's top is below paddle's top match_state['ball']['dx'] *= -1 - match_state['ball']['dx'] *= 1.1 - match_state['ball']['dy'] *= 1.1 + if abs(match_state['ball']['dx']) < BALL_MAX_SPEED: + match_state['ball']['dx'] *= 1.1 + if abs(match_state['ball']['dy']) < BALL_MAX_SPEED: + match_state['ball']['dy'] *= 1.1 await self.send_paddle_hit(match_state['playerA'], match_state['ball']) elif (match_state['ball']['x'] + BALL_RADIUS >= TERRAIN_WIDTH - PADDLE_WIDTH and # Right side of ball hits Player B's paddle match_state['ball']['y'] - BALL_RADIUS <= match_state['playerB']['paddle_y'] + PADDLE_HEIGHT and # Ball's bottom is above paddle's bottom match_state['ball']['y'] + BALL_RADIUS >= match_state['playerB']['paddle_y'] - PADDLE_HEIGHT): # Ball's top is below paddle's top match_state['ball']['dx'] *= -1 - match_state['ball']['dx'] *= 1.1 - match_state['ball']['dy'] *= 1.1 + if abs(match_state['ball']['dx']) < BALL_MAX_SPEED: + match_state['ball']['dx'] *= 1.1 + if abs(match_state['ball']['dy']) < BALL_MAX_SPEED: + match_state['ball']['dy'] *= 1.1 await self.send_paddle_hit(match_state['playerB'], match_state['ball']) # Check for scoring diff --git a/frontend/src/components/Game/local/GameLocal.js b/frontend/src/components/Game/local/GameLocal.js index 888b31bc..74a50d32 100644 --- a/frontend/src/components/Game/local/GameLocal.js +++ b/frontend/src/components/Game/local/GameLocal.js @@ -34,6 +34,7 @@ const GameLocal = () => { // Ball speed and direction const ballVelocity = useRef({ x: 0.1, y: 0.1 }); + const maxBallSpeed = 0.17; // Paddle movement speed const paddleSpeed = 0.1; @@ -125,8 +126,10 @@ const GameLocal = () => { ball.current.position.y - ballRadius <= paddle1Top && ball.current.position.y + ballRadius >= paddle1Bottom) { ballVelocity.current.x *= -1; - ballVelocity.current.x *= 1.1; - ballVelocity.current.y *= 1.1; + if (Math.abs(ballVelocity.current.x) < maxBallSpeed) + ballVelocity.current.x *= 1.1; + if (Math.abs(ballVelocity.current.y) < maxBallSpeed) + ballVelocity.current.y *= 1.1; hit.current = { x: ball.current.position.x, y: ball.current.position.y }; setIsHit(true); } @@ -136,8 +139,10 @@ const GameLocal = () => { ball.current.position.y - ballRadius <= paddle2Top && ball.current.position.y + ballRadius >= paddle2Bottom) { ballVelocity.current.x *= -1; - ballVelocity.current.x *= 1.1; - ballVelocity.current.y *= 1.1; + if (Math.abs(ballVelocity.current.x) < maxBallSpeed) + ballVelocity.current.x *= 1.1; + if (Math.abs(ballVelocity.current.y) < maxBallSpeed) + ballVelocity.current.y *= 1.1; hit.current = { x: ball.current.position.x, y: ball.current.position.y }; setIsHit(true); }