photon_ap.html
photon_ap.html
html
~\OneDrive\바 탕 화 면 \photon_ap.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Photon Shooter</title>
5 <style>
6 canvas {
7 border: 2px solid #C3C3C3;
8 }
9 </style>
10
11 <script>
12 // ints
13 let canvas, ctx;
14 let spaceship = { x: 360, y: 240, width: 40, height: 40, health: 3, maxHealth: 5 };
15 let photons = [];
16 let enemies = [];
17 let powerUps = [];
18 let score = 0;
19 let gameOver = false;
20 const boundary = { x: 300, y: 180, width: 160, height: 160 };
21 let keys = {}; // for a smooth movement
22
23
24 // Game reset int
25 function init() {
26 canvas = document.getElementById("gameCanvas");
27 ctx = canvas.getContext("2d");
28
29
30 // key binding
31 document.addEventListener("keydown", (e) => (keys[e.key] = true));
32 document.addEventListener("keyup", (e) => (keys[e.key] = false));
33 document.addEventListener("keydown", (e) => {
34 if (e.code === "Space" && !gameOver) firePhoton(); // bullet fires
35 });
36
37 spawnEnemy(1); // spawns enemy
38 spawnPowerUp(); // spawns power up
39 requestAnimationFram
e(gameLoop); // start game loop
40 }
41
42
43 // player movement ints
44 function updatePlayer() {
45 const speed = 5;
46 if (keys["ArrowUp"] && spaceship.y > boundary.y) spaceship.y -= speed;
47 if (keys["ArrowDown"] && spaceship.y + spaceship.height < boundary.y +
boundary.height) spaceship.y += speed;
localhost:1722/48a0ef77-87de-4800-8d38-9989327012b2/ 1/6
25. 3. 14. 오전 2:14 photon_ap.html
96 dx: Math.cos(angle),
97 dy: Math.sin(angle)
98 });
99
100 setTimeout(() => spawnEnemy(speed + 0.2), 2000); // enemy speed increases
101 }
102
103
104 // power up ints
105 function spawnPowerUp() {
106 powerUps.push({
107 x: Math.random() * (canvas.width - 20),
108 y: Math.random() * (canvas.height - 20),
109 type: "shield",
110 active: true
111 });
112 setTimeout(spawnPowerUp, 10000); // new power up spawns every 10 sec.
113 }
114
115
116
117 // game update ints
118 function updateGame() {
119 updatePlayer(); // player movement update
120
121
122
123 // bullet update
124 for (let i = photons.length - 1; i >= 0; i--) {
125 let bullet = photons[i];
126 bullet.x += bullet.velX;
127 bullet.y += bullet.velY;
128
129 if (bullet.radius < bullet.maxRadius) bullet.radius += bullet.growthRate;
130
131 if (
132 bullet.x - bullet.radius < 0 ||
133 bullet.x + bullet.radius > canvas.width ||
134 bullet.y - bullet.radius < 0 ||
135 bullet.y + bullet.radius > canvas.height
136 ) {
137 photons.splice(i, 1);
138 }
139 }
140
141
142 // enemy update
143 for (let i = enemies.length - 1; i >= 0; i--) {
144 let enemy = enemies[i];
145
localhost:1722/48a0ef77-87de-4800-8d38-9989327012b2/ 3/6
25. 3. 14. 오전 2:14 photon_ap.html
localhost:1722/48a0ef77-87de-4800-8d38-9989327012b2/ 4/6
25. 3. 14. 오전 2:14 photon_ap.html
195 }
196 }
197 }
198
199
200
201
202 // adding +1 health when bullet hits power up
203 for (let i = powerUps.length - 1; i >= 0; i--) {
204 let powerUp = powerUps[i];
205
206 for (let j = photons.length - 1; j >= 0; j--) {
207 let bullet = photons[j];
208
209 if (
210 bullet.x + bullet.radius > powerUp.x &&
211 bullet.x - bullet.radius < powerUp.x + 20 &&
212 bullet.y + bullet.radius > powerUp.y &&
213 bullet.y - bullet.radius < powerUp.y + 20
214 ) {
215 if (spaceship.health < spaceship.maxHealth) spaceship.health++; //
add up the health by one
216 powerUps.splice(i, 1); // remove the power up square
217 photons.splice(j, 1); // remove bullet
218 break;
219 }
220 }
221 }
222 }
223
224
225
226 // display the game element
227 function draw() {
228 ctx.clearRect(0, 0, canvas.width, canvas.height);
229
230 ctx.strokeStyle = "#000";
231 ctx.strokeRect(boundary.x, boundary.y, boundary.width, boundary.height);
232
233 ctx.fillStyle = "blue";
234 ctx.fillRect(spaceship.x, spaceship.y, spaceship.width, spaceship.height);
235
236 ctx.fillStyle = "red";
237 for (const photon of photons) {
238 ctx.beginPath();
239 ctx.arc(photon.x, photon.y, photon.radius, 0, Math.PI * 2);
240 ctx.fill();
241 }
242
243 ctx.fillStyle = "green";
localhost:1722/48a0ef77-87de-4800-8d38-9989327012b2/ 5/6
25. 3. 14. 오전 2:14 photon_ap.html
localhost:1722/48a0ef77-87de-4800-8d38-9989327012b2/ 6/6