Gregniuki commited on
Commit
a4b6ccc
·
verified ·
1 Parent(s): a430f05

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +59 -31
script.js CHANGED
@@ -2,6 +2,8 @@ const gameArea = document.getElementById('gameArea');
2
  const paddleLeft = document.getElementById('paddleLeft');
3
  const paddleRight = document.getElementById('paddleRight');
4
  const ball = document.getElementById('ball');
 
 
5
 
6
  let paddleLeftY = window.innerHeight / 2 - 40; // Center the paddle
7
  let paddleRightY = window.innerHeight / 2 - 40; // Center the paddle
@@ -9,6 +11,9 @@ let ballX = window.innerWidth / 2; // Center the ball
9
  let ballY = window.innerHeight / 2; // Center the ball
10
  let ballSpeedX = 3; // Horizontal speed
11
  let ballSpeedY = 3; // Vertical speed
 
 
 
12
 
13
  const paddleHeight = 80;
14
  const paddleWidth = 10;
@@ -26,40 +31,53 @@ gameArea.addEventListener('touchmove', (e) => {
26
  paddleLeftY = touchY - paddleHeight / 2;
27
  if (paddleLeftY < 0) paddleLeftY = 0;
28
  if (paddleLeftY > window.innerHeight - paddleHeight) paddleLeftY = window.innerHeight - paddleHeight;
 
 
 
 
 
29
  }
30
  });
31
 
32
  function update() {
33
- // Update ball position
34
- ballX += ballSpeedX;
35
- ballY += ballSpeedY;
36
-
37
- // Ball collision with top and bottom walls
38
- if (ballY <= 0 || ballY >= window.innerHeight - ballSize) {
39
- ballSpeedY = -ballSpeedY;
40
- }
 
41
 
42
- // Ball collision with left paddle
43
- if (
44
- ballX <= paddleWidth + 10 && // Left paddle
45
- ballY + ballSize >= paddleLeftY &&
46
- ballY <= paddleLeftY + paddleHeight
47
- ) {
48
- ballSpeedX = -ballSpeedX;
49
- }
50
 
51
- // Ball collision with right paddle
52
- if (
53
- ballX >= window.innerWidth - paddleWidth - 25 && // Right paddle
54
- ballY + ballSize >= paddleRightY &&
55
- ballY <= paddleRightY + paddleHeight
56
- ) {
57
- ballSpeedX = -ballSpeedX;
58
- }
59
 
60
- // Ball out of bounds (left or right)
61
- if (ballX <= 0 || ballX >= window.innerWidth) {
62
- resetBall();
 
 
 
 
 
 
 
63
  }
64
 
65
  // Bot logic for right paddle
@@ -84,10 +102,20 @@ function update() {
84
  requestAnimationFrame(update);
85
  }
86
 
87
- function resetBall() {
88
- ballX = window.innerWidth / 2;
89
- ballY = window.innerHeight / 2;
90
- ballSpeedX = -ballSpeedX; // Reverse direction
 
 
 
 
 
 
 
 
 
 
91
  }
92
 
93
  // Start the game loop
 
2
  const paddleLeft = document.getElementById('paddleLeft');
3
  const paddleRight = document.getElementById('paddleRight');
4
  const ball = document.getElementById('ball');
5
+ const playerScoreDisplay = document.getElementById('playerScore');
6
+ const botScoreDisplay = document.getElementById('botScore');
7
 
8
  let paddleLeftY = window.innerHeight / 2 - 40; // Center the paddle
9
  let paddleRightY = window.innerHeight / 2 - 40; // Center the paddle
 
11
  let ballY = window.innerHeight / 2; // Center the ball
12
  let ballSpeedX = 3; // Horizontal speed
13
  let ballSpeedY = 3; // Vertical speed
14
+ let playerScore = 0;
15
+ let botScore = 0;
16
+ let isGamePaused = true; // Pause the game until the player moves
17
 
18
  const paddleHeight = 80;
19
  const paddleWidth = 10;
 
31
  paddleLeftY = touchY - paddleHeight / 2;
32
  if (paddleLeftY < 0) paddleLeftY = 0;
33
  if (paddleLeftY > window.innerHeight - paddleHeight) paddleLeftY = window.innerHeight - paddleHeight;
34
+
35
+ // Start the game if it's paused
36
+ if (isGamePaused) {
37
+ isGamePaused = false;
38
+ }
39
  }
40
  });
41
 
42
  function update() {
43
+ if (!isGamePaused) {
44
+ // Update ball position
45
+ ballX += ballSpeedX;
46
+ ballY += ballSpeedY;
47
+
48
+ // Ball collision with top and bottom walls
49
+ if (ballY <= 0 || ballY >= window.innerHeight - ballSize) {
50
+ ballSpeedY = -ballSpeedY;
51
+ }
52
 
53
+ // Ball collision with left paddle
54
+ if (
55
+ ballX <= paddleWidth + 10 && // Left paddle
56
+ ballY + ballSize >= paddleLeftY &&
57
+ ballY <= paddleLeftY + paddleHeight
58
+ ) {
59
+ ballSpeedX = -ballSpeedX;
60
+ }
61
 
62
+ // Ball collision with right paddle
63
+ if (
64
+ ballX >= window.innerWidth - paddleWidth - 25 && // Right paddle
65
+ ballY + ballSize >= paddleRightY &&
66
+ ballY <= paddleRightY + paddleHeight
67
+ ) {
68
+ ballSpeedX = -ballSpeedX;
69
+ }
70
 
71
+ // Ball out of bounds (left or right)
72
+ if (ballX <= 0) {
73
+ botScore++;
74
+ botScoreDisplay.textContent = botScore;
75
+ resetBall('right');
76
+ } else if (ballX >= window.innerWidth) {
77
+ playerScore++;
78
+ playerScoreDisplay.textContent = playerScore;
79
+ resetBall('left');
80
+ }
81
  }
82
 
83
  // Bot logic for right paddle
 
102
  requestAnimationFrame(update);
103
  }
104
 
105
+ function resetBall(side) {
106
+ isGamePaused = true; // Pause the game
107
+
108
+ if (side === 'left') {
109
+ ballX = paddleWidth + 10; // Place ball on the left paddle
110
+ ballY = paddleLeftY + paddleHeight / 2;
111
+ ballSpeedX = Math.abs(ballSpeedX); // Move ball to the right
112
+ } else if (side === 'right') {
113
+ ballX = window.innerWidth - paddleWidth - 25; // Place ball on the right paddle
114
+ ballY = paddleRightY + paddleHeight / 2;
115
+ ballSpeedX = -Math.abs(ballSpeedX); // Move ball to the left
116
+ }
117
+
118
+ ballSpeedY = (Math.random() - 0.5) * 6; // Randomize vertical speed
119
  }
120
 
121
  // Start the game loop