Nymbo commited on
Commit
a126240
1 Parent(s): 91e9681

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +44 -38
index.html CHANGED
@@ -3,7 +3,7 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Pong Game</title>
7
  <style>
8
  body {
9
  display: flex;
@@ -28,7 +28,7 @@
28
  const screenWidth = 800;
29
  const screenHeight = 500;
30
  let playerScore = 0;
31
- let opponentScore = 0;
32
 
33
  // Ball
34
  const ball = {
@@ -43,19 +43,19 @@
43
  // Paddles
44
  const paddleWidth = 10;
45
  const paddleHeight = 140;
46
- const player = {
47
- x: screenWidth - 20,
48
  y: screenHeight / 2 - paddleHeight / 2,
49
  width: paddleWidth,
50
  height: paddleHeight,
51
  speed: 0
52
  };
53
- const opponent = {
54
- x: 10,
55
  y: screenHeight / 2 - paddleHeight / 2,
56
  width: paddleWidth,
57
  height: paddleHeight,
58
- speed: 0
59
  };
60
 
61
  function ballAnimation() {
@@ -67,31 +67,40 @@
67
  }
68
 
69
  if (ball.x + ball.width >= screenWidth) {
70
- opponentScore++;
71
  ballRestart();
72
  } else if (ball.x <= 0) {
73
- playerScore++;
74
  ballRestart();
75
  }
76
 
77
  if (
78
- (ball.x <= opponent.x + opponent.width &&
79
- ball.y + ball.height >= opponent.y &&
80
- ball.y <= opponent.y + opponent.height) ||
81
- (ball.x + ball.width >= player.x &&
82
- ball.y + ball.height >= player.y &&
83
- ball.y <= player.y + player.height)
84
  ) {
85
  ball.speedX *= -1;
86
  }
87
  }
88
 
89
- function playerAnimation() {
90
- player.y = Math.max(0, Math.min(screenHeight - player.height, player.y + player.speed));
91
  }
92
 
93
- function opponentAnimation() {
94
- opponent.y = Math.max(0, Math.min(screenHeight - opponent.height, opponent.y + opponent.speed));
 
 
 
 
 
 
 
 
 
95
  }
96
 
97
  function ballRestart() {
@@ -113,47 +122,44 @@
113
  ctx.stroke();
114
 
115
  // Draw paddles
116
- ctx.fillStyle = 'rgb(200, 200, 200)';
117
- ctx.fillRect(player.x, player.y, player.width, player.height);
118
- ctx.fillRect(opponent.x, opponent.y, opponent.width, opponent.height);
 
119
 
120
  // Draw ball
 
121
  ctx.beginPath();
122
  ctx.arc(ball.x + ball.width / 2, ball.y + ball.height / 2, ball.width / 2, 0, Math.PI * 2);
123
  ctx.fill();
124
 
125
  // Draw scores
126
  ctx.font = '32px Arial';
127
- ctx.fillText(playerScore, 420, 40);
128
- ctx.fillText(opponentScore, 360, 40);
 
129
  }
130
 
131
  function gameLoop() {
132
  ballAnimation();
133
- playerAnimation();
134
- opponentAnimation();
135
  draw();
136
  requestAnimationFrame(gameLoop);
137
  }
138
 
139
  // Keyboard controls
140
  document.addEventListener('keydown', (event) => {
141
- if (event.key === 'ArrowUp') {
142
- player.speed = -7;
143
- } else if (event.key === 'ArrowDown') {
144
- player.speed = 7;
145
- } else if (event.key === 'w') {
146
- opponent.speed = -7;
147
- } else if (event.key === 's') {
148
- opponent.speed = 7;
149
  }
150
  });
151
 
152
  document.addEventListener('keyup', (event) => {
153
- if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
154
- player.speed = 0;
155
- } else if (event.key === 'w' || event.key === 's') {
156
- opponent.speed = 0;
157
  }
158
  });
159
 
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Pong Game (Player vs AI)</title>
7
  <style>
8
  body {
9
  display: flex;
 
28
  const screenWidth = 800;
29
  const screenHeight = 500;
30
  let playerScore = 0;
31
+ let aiScore = 0;
32
 
33
  // Ball
34
  const ball = {
 
43
  // Paddles
44
  const paddleWidth = 10;
45
  const paddleHeight = 140;
46
+ const playerPaddle = {
47
+ x: 10,
48
  y: screenHeight / 2 - paddleHeight / 2,
49
  width: paddleWidth,
50
  height: paddleHeight,
51
  speed: 0
52
  };
53
+ const aiPaddle = {
54
+ x: screenWidth - 20,
55
  y: screenHeight / 2 - paddleHeight / 2,
56
  width: paddleWidth,
57
  height: paddleHeight,
58
+ speed: 5
59
  };
60
 
61
  function ballAnimation() {
 
67
  }
68
 
69
  if (ball.x + ball.width >= screenWidth) {
70
+ playerScore++;
71
  ballRestart();
72
  } else if (ball.x <= 0) {
73
+ aiScore++;
74
  ballRestart();
75
  }
76
 
77
  if (
78
+ (ball.x <= playerPaddle.x + playerPaddle.width &&
79
+ ball.y + ball.height >= playerPaddle.y &&
80
+ ball.y <= playerPaddle.y + playerPaddle.height) ||
81
+ (ball.x + ball.width >= aiPaddle.x &&
82
+ ball.y + ball.height >= aiPaddle.y &&
83
+ ball.y <= aiPaddle.y + aiPaddle.height)
84
  ) {
85
  ball.speedX *= -1;
86
  }
87
  }
88
 
89
+ function playerPaddleAnimation() {
90
+ playerPaddle.y = Math.max(0, Math.min(screenHeight - playerPaddle.height, playerPaddle.y + playerPaddle.speed));
91
  }
92
 
93
+ function aiPaddleAnimation() {
94
+ const paddleCenter = aiPaddle.y + aiPaddle.height / 2;
95
+ const ballCenter = ball.y + ball.height / 2;
96
+
97
+ if (paddleCenter < ballCenter - 35) {
98
+ aiPaddle.y += aiPaddle.speed;
99
+ } else if (paddleCenter > ballCenter + 35) {
100
+ aiPaddle.y -= aiPaddle.speed;
101
+ }
102
+
103
+ aiPaddle.y = Math.max(0, Math.min(screenHeight - aiPaddle.height, aiPaddle.y));
104
  }
105
 
106
  function ballRestart() {
 
122
  ctx.stroke();
123
 
124
  // Draw paddles
125
+ ctx.fillStyle = 'blue';
126
+ ctx.fillRect(playerPaddle.x, playerPaddle.y, playerPaddle.width, playerPaddle.height);
127
+ ctx.fillStyle = 'red';
128
+ ctx.fillRect(aiPaddle.x, aiPaddle.y, aiPaddle.width, aiPaddle.height);
129
 
130
  // Draw ball
131
+ ctx.fillStyle = 'white';
132
  ctx.beginPath();
133
  ctx.arc(ball.x + ball.width / 2, ball.y + ball.height / 2, ball.width / 2, 0, Math.PI * 2);
134
  ctx.fill();
135
 
136
  // Draw scores
137
  ctx.font = '32px Arial';
138
+ ctx.fillStyle = 'white';
139
+ ctx.fillText(playerScore, 360, 40);
140
+ ctx.fillText(aiScore, 420, 40);
141
  }
142
 
143
  function gameLoop() {
144
  ballAnimation();
145
+ playerPaddleAnimation();
146
+ aiPaddleAnimation();
147
  draw();
148
  requestAnimationFrame(gameLoop);
149
  }
150
 
151
  // Keyboard controls
152
  document.addEventListener('keydown', (event) => {
153
+ if (event.key === 'w' || event.key === 'W') {
154
+ playerPaddle.speed = -7;
155
+ } else if (event.key === 's' || event.key === 'S') {
156
+ playerPaddle.speed = 7;
 
 
 
 
157
  }
158
  });
159
 
160
  document.addEventListener('keyup', (event) => {
161
+ if (event.key === 'w' || event.key === 'W' || event.key === 's' || event.key === 'S') {
162
+ playerPaddle.speed = 0;
 
 
163
  }
164
  });
165