Nymbo commited on
Commit
20032bf
1 Parent(s): 59bd458

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +112 -113
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>Space Invaders</title>
7
  <style>
8
  body {
9
  display: flex;
@@ -19,143 +19,142 @@
19
  </style>
20
  </head>
21
  <body>
22
- <canvas id="gameCanvas" width="800" height="600"></canvas>
23
  <script>
24
  const canvas = document.getElementById('gameCanvas');
25
  const ctx = canvas.getContext('2d');
26
 
27
  // Game variables
28
- let playerX = 370;
29
- let playerY = 480;
30
- let playerWidth = 64;
31
- let playerHeight = 64;
32
- let playerSpeed = 5;
33
-
34
- let enemies = [];
35
- const numEnemies = 6;
36
- const enemyWidth = 64;
37
- const enemyHeight = 64;
38
-
39
- let bullet = {
40
- x: 0,
41
- y: 480,
42
- width: 8,
43
- height: 20,
44
- speed: 10,
45
- active: false
46
  };
47
 
48
- let score = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- // Initialize enemies
51
- for (let i = 0; i < numEnemies; i++) {
52
- enemies.push({
53
- x: Math.random() * (canvas.width - enemyWidth),
54
- y: Math.random() * 100 + 50,
55
- speed: 2,
56
- direction: 1
57
- });
58
- }
59
 
60
- // Game loop
61
- function gameLoop() {
62
- update();
63
- draw();
64
- requestAnimationFrame(gameLoop);
65
- }
66
-
67
- function update() {
68
- // Move player
69
- if (keys.ArrowLeft && playerX > 0) playerX -= playerSpeed;
70
- if (keys.ArrowRight && playerX < canvas.width - playerWidth) playerX += playerSpeed;
71
 
72
- // Move bullet
73
- if (bullet.active) {
74
- bullet.y -= bullet.speed;
75
- if (bullet.y < 0) bullet.active = false;
 
 
76
  }
77
 
78
- // Move enemies
79
- enemies.forEach(enemy => {
80
- enemy.x += enemy.speed * enemy.direction;
81
- if (enemy.x <= 0 || enemy.x >= canvas.width - enemyWidth) {
82
- enemy.direction *= -1;
83
- enemy.y += 20;
84
- }
85
-
86
- // Check for collision with bullet
87
- if (bullet.active && collision(bullet, enemy)) {
88
- bullet.active = false;
89
- enemy.y = Math.random() * 100 + 50;
90
- enemy.x = Math.random() * (canvas.width - enemyWidth);
91
- score++;
92
- }
93
-
94
- // Check for game over
95
- if (enemy.y > playerY - enemyHeight) {
96
- alert('Game Over! Your score: ' + score);
97
- resetGame();
98
- }
99
- });
100
  }
101
 
102
- function draw() {
103
- // Clear canvas
104
- ctx.fillStyle = 'black';
105
- ctx.fillRect(0, 0, canvas.width, canvas.height);
106
-
107
- // Draw player
108
- ctx.fillStyle = 'lime';
109
- ctx.fillRect(playerX, playerY, playerWidth, playerHeight);
110
-
111
- // Draw enemies
112
- ctx.fillStyle = 'red';
113
- enemies.forEach(enemy => {
114
- ctx.fillRect(enemy.x, enemy.y, enemyWidth, enemyHeight);
115
- });
116
-
117
- // Draw bullet
118
- if (bullet.active) {
119
- ctx.fillStyle = 'white';
120
- ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
121
- }
122
 
123
- // Draw score
124
- ctx.fillStyle = 'white';
125
- ctx.font = '24px Arial';
126
- ctx.fillText('Score: ' + score, 10, 30);
127
  }
128
 
129
- function collision(rect1, rect2) {
130
- return rect1.x < rect2.x + enemyWidth &&
131
- rect1.x + rect1.width > rect2.x &&
132
- rect1.y < rect2.y + enemyHeight &&
133
- rect1.y + rect1.height > rect2.y;
134
  }
135
 
136
- function resetGame() {
137
- playerX = 370;
138
- score = 0;
139
- enemies.forEach(enemy => {
140
- enemy.y = Math.random() * 100 + 50;
141
- enemy.x = Math.random() * (canvas.width - enemyWidth);
142
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  }
144
 
145
- // Keyboard input
146
- let keys = {};
 
 
 
 
 
147
 
148
- document.addEventListener('keydown', (e) => {
149
- keys[e.code] = true;
150
- if (e.code === 'Space' && !bullet.active) {
151
- bullet.active = true;
152
- bullet.x = playerX + playerWidth / 2 - bullet.width / 2;
153
- bullet.y = playerY;
 
 
 
 
154
  }
155
  });
156
 
157
- document.addEventListener('keyup', (e) => {
158
- keys[e.code] = false;
 
 
 
 
159
  });
160
 
161
  // Start the game
 
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;
 
19
  </style>
20
  </head>
21
  <body>
22
+ <canvas id="gameCanvas" width="800" height="500"></canvas>
23
  <script>
24
  const canvas = document.getElementById('gameCanvas');
25
  const ctx = canvas.getContext('2d');
26
 
27
  // Game variables
28
+ const screenWidth = 800;
29
+ const screenHeight = 500;
30
+ let playerScore = 0;
31
+ let opponentScore = 0;
32
+
33
+ // Ball
34
+ const ball = {
35
+ x: screenWidth / 2,
36
+ y: screenHeight / 2,
37
+ width: 30,
38
+ height: 30,
39
+ speedX: 7,
40
+ speedY: 7
 
 
 
 
 
41
  };
42
 
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() {
62
+ ball.x += ball.speedX;
63
+ ball.y += ball.speedY;
 
 
 
 
 
 
64
 
65
+ if (ball.y <= 0 || ball.y + ball.height >= screenHeight) {
66
+ ball.speedY *= -1;
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() {
98
+ ball.x = screenWidth / 2;
99
+ ball.y = screenHeight / 2;
100
+ ball.speedX *= -1;
 
101
  }
102
 
103
+ function draw() {
104
+ // Clear canvas
105
+ ctx.fillStyle = 'rgb(30, 30, 30)';
106
+ ctx.fillRect(0, 0, screenWidth, screenHeight);
107
+
108
+ // Draw middle line
109
+ ctx.strokeStyle = 'rgb(200, 200, 200)';
110
+ ctx.beginPath();
111
+ ctx.moveTo(screenWidth / 2, 0);
112
+ ctx.lineTo(screenWidth / 2, screenHeight);
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
 
160
  // Start the game