File size: 12,899 Bytes
ecae3bb
d96d2fc
264388d
6f7e03f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264388d
 
6f7e03f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f55aec5
f731e95
6f7e03f
 
47654c5
6f7e03f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d96d2fc
6f7e03f
 
 
 
 
 
 
 
 
 
 
 
d96d2fc
f55aec5
6f7e03f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f731e95
6f7e03f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f731e95
6f7e03f
 
 
 
 
 
d96d2fc
6f7e03f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f731e95
6f7e03f
 
 
 
 
 
 
 
 
 
 
 
d96d2fc
f731e95
6f7e03f
f731e95
6f7e03f
f731e95
6f7e03f
f731e95
6f7e03f
f731e95
6f7e03f
 
 
 
 
 
 
f731e95
 
6f7e03f
47654c5
6f7e03f
 
 
 
f731e95
6f7e03f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47654c5
6f7e03f
47654c5
6f7e03f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47654c5
 
f731e95
d96d2fc
6f7e03f
 
 
47654c5
6f7e03f
 
 
47654c5
6f7e03f
f731e95
 
 
4c4af5a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            background: black;
        }
        #gameCanvas {
            background-image: url('stage 1.jpg');
            background-size: cover;
        }
        #message {
            position: fixed;
            bottom: 550px;
            left: 0;
            width: 100%;
            color: white;
            text-align: center;
            font-size: 30px;
            font-family: Arial;
        }
        #stats {
            position: fixed;
            top: 20px;
            left: 20px;
            color: white;
            font-family: Arial;
            font-size: 28px;
        }
        #villainCount {
            position: fixed;
            top: 20px;
            right: 20px;
            color: white;
            font-family: Arial;
            font-size: 28px; 
        }
        #healthBar {
            width: 300px; 
            height: 30px; 
            background: #333;
            margin-top: 5px;
        }
        #healthFill {
            width: 100%;
            height: 100%;
            background: red;
            transition: width 0.3s;
        }
        #bulletCount, #enemyCount {
            font-size: 28px; 
        }
        #replayButton {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            padding: 15px 30px;
            font-size: 20px;
            font-family: Arial;
            background-color: white;
            color: black;
            border: none;
            border-radius: 5px;
            display: none;
            cursor: pointer;
        }
        #replayButton:hover {
            background-color: lightgray;
        }
    </style>
</head>
<body>
    <canvas id="gameCanvas"></canvas>
    <div id="message"></div>
    <div id="stats">
        Health:<div id="healthBar"><div id="healthFill"></div></div>
        Bullets: <span id="bulletCount">35</span>
    </div>
    <div id="villainCount">Villains: <span id="enemyCount">30</span></div>
    <button id="replayButton">Replay</button>
    <audio id="bgMusic" src="cinematic-time-lapse-115672.mp3" loop></audio>
    <audio id="playerShootSound" src="Maincharactergunsound.mp3"></audio>
    <audio id="enemyShootSound" src="Renovatorsgunshot.mp3"></audio>
    <audio id="playerHitSound" src="Maincharactershot.mp3"></audio>
    <audio id="enemyHitSound" src="Renovatorsgetshot.mp3"></audio>

    <script>
        const canvas = document.getElementById('gameCanvas');    
        const ctx = canvas.getContext('2d');
        const message = document.getElementById('message');
        const healthFill = document.getElementById('healthFill');
        const bulletCount = document.getElementById('bulletCount');
        const enemyCount = document.getElementById('enemyCount');
        const bgMusic = document.getElementById('bgMusic');
        const replayButton = document.getElementById('replayButton');
        const playerShootSound = document.getElementById('playerShootSound');
        const enemyShootSound = document.getElementById('enemyShootSound');
        const playerHitSound = document.getElementById('playerHitSound');
        const enemyHitSound = document.getElementById('enemyHitSound');

        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        const player = {
            x: canvas.width / 2.3,
            y: canvas.height - 260,
            width: 250,
            height: 250,
            speed: 10,
            health: 10,
            bullets: 35,
            direction: 1,
            image: new Image(),
            imageLeft: 'Maincharacterdefaultleft.png',
            imageRight: 'Maincharacterdefaultright.png',
            imageShootLeft: 'gunshotleft.png',
            imageShootRight: 'gunshotright.png',
            imageHitLeft: 'ExplosionLeft.png',
            imageHitRight: 'ExplosionRight.png'
        };
        player.image.src = player.imageRight;

        let enemies = [];
        let bullets = [];
        let enemyBullets = [];
        let enemiesRemaining = 30;
        let mouseX = 0;
        let gameStarted = false;
        let gameOver = false;

        function spawnEnemy() {
    if (!gameStarted || gameOver) {
        return;
    }

    if (enemies.length >= 10) { // λ™μ‹œμ— μ‘΄μž¬ν•  수 μžˆλŠ” 적의 수 μ œν•œ
        return;
    }

    const side = Math.random() < 0.5 ? 0 : canvas.width;
    const enemy = {
        x: side,
        y: Math.random() * (canvas.height - 150),
        width: 250,
        height: 250,
        speed: side === 0 ? 1 : -1,
        image: new Image(),
        imageLeft: 'Renovators Default Right.png',
        imageRight: 'RenovatorsDefaultLeft.png',
        imageShootLeft: 'RenovatorsgunLeft.png',
        imageShootRight: 'RenovatorsGunRight.png',
        imageHitLeft: 'RenovatorsExplodeLeft.png',
        imageHitRight: 'RenovatorsExplodeRight.png',
        lastShootTime: Date.now()
    };
    enemy.image.src = side === 0 ? enemy.imageLeft : enemy.imageRight;
    enemies.push(enemy);
}
        function updateGame() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    player.direction = mouseX > player.x ? 1 : -1;
    player.image.src = player.direction === 1 ? player.imageRight : player.imageLeft;
    ctx.drawImage(player.image, player.x, player.y, player.width, player.height);

    // ν”Œλ ˆμ΄μ–΄κ°€ λ°œμ‚¬ν•œ μ΄μ•Œ 처리
    bullets.forEach((bullet, index) => {
        bullet.x += bullet.speed;
        ctx.fillStyle = 'yellow';
        ctx.fillRect(bullet.x, bullet.y, 10, 5);
    });

    // 적 처리
    enemies.forEach((enemy, enemyIndex) => {
        enemy.x += enemy.speed;
        ctx.drawImage(enemy.image, enemy.x, enemy.y, enemy.width, enemy.height);

        // ν”Œλ ˆμ΄μ–΄μ™€ 적이 μΆ©λŒν–ˆμ„ 경우 처리
        if (checkCollision(player, enemy)) {
            player.health--;
            healthFill.style.width = (player.health / 5 * 100) + '%';
            player.image.src = player.direction === 1 ? player.imageHitRight : player.imageHitLeft;
            enemies.splice(enemyIndex, 1);  // 적 제거
            playerHitSound.play();

            if (player.health <= 0) {
                endGame("Game Over! Go back to the past and change the current result!");
            }
        }

        // ν”Œλ ˆμ΄μ–΄μ˜ μ΄μ•Œμ΄ 적을 λ§žμ·„μ„ 경우 처리
        bullets.forEach((bullet, bulletIndex) => {
            if (checkCollision(bullet, enemy)) {
                // μ΄μ•Œμ΄ 적을 λ§žμ·„μ„ λ•Œ 처리
                enemy.image.src = enemy.speed > 0 ? enemy.imageHitRight : enemy.imageHitLeft;
                setTimeout(() => {
                    enemies.splice(enemyIndex, 1);  // 적 제거
                    bullets.splice(bulletIndex, 1);  // μ΄μ•Œ 제거
                    enemiesRemaining--;
                    enemyCount.textContent = enemiesRemaining;
                }, 100);
                enemyHitSound.play();

                if (enemiesRemaining <= 0) {
                    endGame("You have successfully protected New York. Return to the time machine.");
                }
            }
        });

        // 적의 μ΄μ•Œ λ°œμ‚¬
        enemyShoot(enemy);
    });
if (enemiesRemaining <= 0 && enemies.length === 0) {
    endGame("You have successfully protected New York. Return to the time machine.");
    return; // κ²Œμž„ μ’…λ£Œ ν›„ 더 이상 μ—…λ°μ΄νŠΈν•˜μ§€ μ•ŠμŒ
}
    // 적의 μ΄μ•Œ 처리
    enemyBullets.forEach((bullet, index) => {
        bullet.x += bullet.speed;
        ctx.fillStyle = 'orange';
        ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);

        // μ΄μ•Œμ΄ ν”Œλ ˆμ΄μ–΄μ™€ μΆ©λŒν–ˆμ„ 경우 처리
        if (
            bullet.x < player.x + player.width - 30 &&
            bullet.x + bullet.width > player.x + 30 &&
            bullet.y < player.y + player.height - 30 &&
            bullet.y + bullet.height > player.y + 30
        ) {
            player.health--;
            healthFill.style.width = (player.health / 5 * 100) + '%';
            player.image.src = player.direction === 1 ? player.imageHitRight : player.imageHitLeft;
            enemyBullets.splice(index, 1);  // μ΄μ•Œ 제거
            playerHitSound.play();

            if (player.health <= 0) {
                endGame("Game Over! Go back to the past and change the current result!");
            }
        }
    });

    // ν™”λ©΄ λ°–μœΌλ‘œ λ‚˜κ°„ μ΄μ•Œ 제거
    bullets = bullets.filter(bullet => 
        bullet.x >= 0 && bullet.x <= canvas.width
    );
    enemyBullets = enemyBullets.filter(bullet => 
        bullet.x >= 0 && bullet.x <= canvas.width
    );

    // κ²Œμž„μ΄ μ’…λ£Œλ˜μ§€ μ•ŠμœΌλ©΄ 계속 μ—…λ°μ΄νŠΈ
    if (!gameOver) {
        requestAnimationFrame(updateGame);
    }
}
        setTimeout(() => {
            message.textContent = "Click on the character";
            setTimeout(() => {
                message.textContent = "You can move your character using the arrow keys.";
                setTimeout(() => {
                    message.textContent = "The character's direction follows the mouse cursor.";
                    setTimeout(() => {
                        message.textContent = "Press the space bar to shoot a bullet.";
                        setTimeout(() => {
                            message.textContent = "Your mission is to defeat the Renovators who have attacked New York. Defeat them all and save New York in 2030!";
                            setTimeout(() => {
                                message.textContent = "";
                                gameStarted = true;
                            }, 5000);
                        }, 3000);
                    }, 4000);
                }, 4000);
            }, 4000);
        }, 0);

        function endGame(messageText) {
            gameOver = true;
            message.textContent = messageText;
            replayButton.style.display = 'block';
        }
        function checkCollision(rect1, rect2) {
    return (
        rect1.x + rect1.width * 0.2 < rect2.x + rect2.width * 0.8 && 
        rect1.x + rect1.width * 0.8 > rect2.x + rect2.width * 0.2 && 
        rect1.y + rect1.height * 0.2 < rect2.y + rect2.height * 0.8 && 
        rect1.y + rect1.height * 0.8 > rect2.y + rect2.height * 0.2
    );
}
        function enemyShoot(enemy) {
            const now = Date.now();
            if (now - enemy.lastShootTime >= 3000 && !gameOver) {
                enemy.image.src = enemy.speed > 0 ? enemy.imageShootRight : enemy.imageShootLeft;
                enemyBullets.push({
                    x: enemy.x + enemy.width / 2,
                    y: enemy.y + enemy.height / 2.5,
                    width: 10,
                    height: 5,
                    speed: enemy.speed > 0 ? 5 : -5
                });
                enemy.lastShootTime = now; // Update the last shoot time
                enemyShootSound.play();
            }
        }

        window.addEventListener('keydown', (e) => {
            if (!gameOver) {
                switch (e.key) {
                    case 'ArrowUp':
                        if (player.y > 0) player.y -= player.speed;
                        break;
                    case 'ArrowDown':
                        if (player.y < canvas.height - player.height) player.y += player.speed;
                        break;
                    case 'ArrowLeft':
                        if (player.x > 0) player.x -= player.speed;
                        break;
                    case 'ArrowRight':
                        if (player.x < canvas.width - player.width) player.x += player.speed;
                        break;
                    case ' ':
                        if (player.bullets > 0) {
                            player.image.src = player.direction === 1 ? player.imageShootRight : player.imageShootLeft;
                            bullets.push({
                                x: player.x + player.width / 2,
                                y: player.y + player.height / 2.5,
                                width: 10,
                                height: 5,
                                speed: player.direction * 10
                            });
                            player.bullets--;
                            bulletCount.textContent = player.bullets;
                            playerShootSound.play();
                        }
                        break;
                }
            }
        });

        window.addEventListener('mousemove', (e) => {
            mouseX = e.clientX;
        });

        replayButton.addEventListener('click', () => {
            location.reload();
        });

        setInterval(spawnEnemy, 2000);
        bgMusic.play();
        updateGame();
    </script>
</body>
</html>