Gregniuki commited on
Commit
1d3028f
·
verified ·
1 Parent(s): 698ed3b

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +10 -2
script.js CHANGED
@@ -56,7 +56,11 @@ function update() {
56
  ballY + ballSize >= paddleLeftY &&
57
  ballY <= paddleLeftY + paddleHeight
58
  ) {
59
- ballSpeedX = -ballSpeedX;
 
 
 
 
60
  }
61
 
62
  // Ball collision with right paddle
@@ -65,7 +69,11 @@ function update() {
65
  ballY + ballSize >= paddleRightY &&
66
  ballY <= paddleRightY + paddleHeight
67
  ) {
68
- ballSpeedX = -ballSpeedX;
 
 
 
 
69
  }
70
 
71
  // Ball out of bounds (left or right)
 
56
  ballY + ballSize >= paddleLeftY &&
57
  ballY <= paddleLeftY + paddleHeight
58
  ) {
59
+ // Calculate hit position (how far from the center of the paddle)
60
+ const hitPosition = (ballY - (paddleLeftY + paddleHeight / 2)) / (paddleHeight / 2);
61
+ // Adjust ball angle based on hit position
62
+ ballSpeedX = Math.abs(ballSpeedX); // Ensure ball moves right
63
+ ballSpeedY = hitPosition * 5; // Increase angle based on hit position
64
  }
65
 
66
  // Ball collision with right paddle
 
69
  ballY + ballSize >= paddleRightY &&
70
  ballY <= paddleRightY + paddleHeight
71
  ) {
72
+ // Calculate hit position (how far from the center of the paddle)
73
+ const hitPosition = (ballY - (paddleRightY + paddleHeight / 2)) / (paddleHeight / 2);
74
+ // Adjust ball angle based on hit position
75
+ ballSpeedX = -Math.abs(ballSpeedX); // Ensure ball moves left
76
+ ballSpeedY = hitPosition * 5; // Increase angle based on hit position
77
  }
78
 
79
  // Ball out of bounds (left or right)