Nymbo commited on
Commit
d12c958
1 Parent(s): be3b168

swapping to spaceship

Browse files
Files changed (1) hide show
  1. index.html +106 -173
index.html CHANGED
@@ -1,12 +1,9 @@
1
  <!DOCTYPE html>
2
-
3
- <!-- this was mostly written by Claude-3.5-Sonnet btw -->
4
-
5
  <html lang="en">
6
  <head>
7
  <meta charset="UTF-8">
8
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
9
- <title>Snake Game (WASD Controls, Larger Area, Obstacles, Slower Enemies)</title>
10
  <style>
11
  body {
12
  display: flex;
@@ -14,218 +11,154 @@
14
  align-items: center;
15
  height: 100vh;
16
  margin: 0;
17
- background-color: #f0f0f0;
18
  }
19
  canvas {
20
- border: 2px solid #333;
21
  }
22
  </style>
23
  </head>
24
  <body>
25
- <canvas id="gameCanvas" width="800" height="800"></canvas>
26
  <script>
27
  const canvas = document.getElementById('gameCanvas');
28
  const ctx = canvas.getContext('2d');
29
 
30
- const gridSize = 20;
31
- const tileCount = canvas.width / gridSize;
 
 
 
 
32
 
33
- let snake = [
34
- {x: 20, y: 20},
35
- ];
36
- let food = {x: 15, y: 15};
37
- let dx = 0;
38
- let dy = 0;
39
- let score = 0;
40
- let obstacles = [];
41
  let enemies = [];
42
- let enemyMoveCounter = 0;
43
-
44
- function generateObstacles() {
45
- obstacles = [];
46
- for (let i = 0; i < 20; i++) {
47
- obstacles.push({
48
- x: Math.floor(Math.random() * tileCount),
49
- y: Math.floor(Math.random() * tileCount)
50
- });
51
- }
52
- }
 
53
 
54
- function generateEnemies() {
55
- enemies = [];
56
- for (let i = 0; i < 3; i++) {
57
- enemies.push({
58
- x: Math.floor(Math.random() * tileCount),
59
- y: Math.floor(Math.random() * tileCount)
60
- });
61
- }
62
- }
63
 
64
- generateObstacles();
65
- generateEnemies();
66
-
67
- function drawGame() {
68
- clearCanvas();
69
- moveSnake();
70
- moveEnemies();
71
- drawSnake();
72
- drawFood();
73
- drawObstacles();
74
- drawEnemies();
75
- checkCollision();
76
- drawScore();
77
  }
78
 
79
- function clearCanvas() {
80
- ctx.fillStyle = 'white';
81
- ctx.fillRect(0, 0, canvas.width, canvas.height);
 
 
82
  }
83
 
84
- function moveSnake() {
85
- const head = {x: snake[0].x + dx, y: snake[0].y + dy};
86
- snake.unshift(head);
 
87
 
88
- if (head.x === food.x && head.y === food.y) {
89
- score++;
90
- generateFood();
91
- } else {
92
- snake.pop();
93
  }
94
- }
95
 
96
- function moveEnemies() {
97
- enemyMoveCounter++;
98
- if (enemyMoveCounter >= 2.5) { // Move enemies every 2.5 game loops (60% slower)
99
- enemyMoveCounter = 0;
100
- enemies.forEach(enemy => {
101
- const head = snake[0];
102
- if (enemy.x < head.x) enemy.x++;
103
- else if (enemy.x > head.x) enemy.x--;
104
- if (enemy.y < head.y) enemy.y++;
105
- else if (enemy.y > head.y) enemy.y--;
106
- });
107
- }
108
- }
 
 
109
 
110
- function drawSnake() {
111
- ctx.fillStyle = 'green';
112
- snake.forEach(segment => {
113
- ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2);
 
114
  });
115
  }
116
 
117
- function drawFood() {
118
- ctx.fillStyle = 'red';
119
- ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize - 2, gridSize - 2);
120
- }
121
 
122
- function drawObstacles() {
123
- ctx.fillStyle = 'gray';
124
- obstacles.forEach(obstacle => {
125
- ctx.fillRect(obstacle.x * gridSize, obstacle.y * gridSize, gridSize - 2, gridSize - 2);
126
- });
127
- }
128
 
129
- function drawEnemies() {
130
- ctx.fillStyle = 'purple';
131
  enemies.forEach(enemy => {
132
- ctx.fillRect(enemy.x * gridSize, enemy.y * gridSize, gridSize - 2, gridSize - 2);
133
  });
134
- }
135
-
136
- function generateFood() {
137
- do {
138
- food.x = Math.floor(Math.random() * tileCount);
139
- food.y = Math.floor(Math.random() * tileCount);
140
- } while (isCollision(food) || isOnSnake(food) || isOnEnemy(food));
141
- }
142
-
143
- function isCollision(pos) {
144
- return obstacles.some(obstacle => obstacle.x === pos.x && obstacle.y === pos.y);
145
- }
146
-
147
- function isOnSnake(pos) {
148
- return snake.some(segment => segment.x === pos.x && segment.y === pos.y);
149
- }
150
-
151
- function isOnEnemy(pos) {
152
- return enemies.some(enemy => enemy.x === pos.x && enemy.y === pos.y);
153
- }
154
-
155
- function checkCollision() {
156
- const head = snake[0];
157
 
158
- if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
159
- resetGame();
160
- return;
 
161
  }
162
 
163
- for (let i = 1; i < snake.length; i++) {
164
- if (head.x === snake[i].x && head.y === snake[i].y) {
165
- resetGame();
166
- return;
167
- }
168
- }
169
 
170
- if (isCollision(head) || isOnEnemy(head)) {
171
- resetGame();
172
- }
 
 
173
  }
174
 
175
  function resetGame() {
176
- snake = [{x: 20, y: 20}];
177
- food = {x: 15, y: 15};
178
- dx = 0;
179
- dy = 0;
180
  score = 0;
181
- enemyMoveCounter = 0;
182
- generateObstacles();
183
- generateEnemies();
184
- generateFood();
185
- }
186
-
187
- function drawScore() {
188
- ctx.fillStyle = 'black';
189
- ctx.font = '20px Arial';
190
- ctx.fillText(`Score: ${score}`, 10, 30);
191
  }
192
 
193
- document.addEventListener('keydown', changeDirection);
194
-
195
- function changeDirection(event) {
196
- const key = event.key.toLowerCase();
197
-
198
- const goingUp = dy === -1;
199
- const goingDown = dy === 1;
200
- const goingRight = dx === 1;
201
- const goingLeft = dx === -1;
202
-
203
- if (key === 'a' && !goingRight) {
204
- dx = -1;
205
- dy = 0;
206
- }
207
 
208
- if (key === 'w' && !goingDown) {
209
- dx = 0;
210
- dy = -1;
 
 
 
211
  }
 
212
 
213
- if (key === 'd' && !goingLeft) {
214
- dx = 1;
215
- dy = 0;
216
- }
217
-
218
- if (key === 's' && !goingUp) {
219
- dx = 0;
220
- dy = 1;
221
- }
222
- }
223
-
224
- function gameLoop() {
225
- drawGame();
226
- setTimeout(gameLoop, 100);
227
- }
228
 
 
229
  gameLoop();
230
  </script>
231
  </body>
 
1
  <!DOCTYPE html>
 
 
 
2
  <html lang="en">
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;
 
11
  align-items: center;
12
  height: 100vh;
13
  margin: 0;
14
+ background-color: #000;
15
  }
16
  canvas {
17
+ border: 2px solid #fff;
18
  }
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
162
  gameLoop();
163
  </script>
164
  </body>