Skip to content

Commit

Permalink
game: Limiting ball speed for a playable gameplay
Browse files Browse the repository at this point in the history
  • Loading branch information
okbrandon committed Oct 12, 2024
1 parent c72a6ce commit ec042af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
13 changes: 9 additions & 4 deletions backend/api/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
13 changes: 9 additions & 4 deletions frontend/src/components/Game/local/GameLocal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit ec042af

Please sign in to comment.