File size: 6,301 Bytes
c319e21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fortress Game</title>
    <style>
        body {
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #1a1a1a;
        }

        #gameCanvas {
            background: #000;
            border: 2px solid #444;
        }

        .controls {
            position: fixed;
            bottom: 20px;
            display: flex;
            gap: 10px;
        }

        button {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            background: #444;
            color: white;
            border: none;
            border-radius: 4px;
        }

        button:hover {
            background: #555;
        }

        #power {
            color: white;
            position: fixed;
            top: 20px;
            left: 20px;
        }

        #angle {
            color: white;
            position: fixed;
            top: 50px;
            left: 20px;
        }
    </style>
</head>
<body>
    <canvas id="gameCanvas" width="800" height="600"></canvas>
    <div id="power">Power: 50</div>
    <div id="angle">Angle: 45°</div>
    <div class="controls">
        <button id="powerDown">Power -</button>
        <button id="powerUp">Power +</button>
        <button id="angleDown">Angle -</button>
        <button id="angleUp">Angle +</button>
        <button id="fire">FIRE!</button>
    </div>

    <script>
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');
        const powerDisplay = document.getElementById('power');
        const angleDisplay = document.getElementById('angle');

        let power = 50;
        let angle = 45;
        let projectiles = [];
        let targets = [];
        let isGameOver = false;

        // Tank properties
        const tank = {
            x: 50,
            y: canvas.height - 30,
            width: 40,
            height: 20,
            barrelLength: 30
        };

        // Generate random targets
        for(let i = 0; i < 3; i++) {
            targets.push({
                x: Math.random() * (canvas.width - 200) + 400,
                y: canvas.height - 30,
                width: 30,
                height: 30,
                isHit: false
            });
        }

        function drawTank() {
            ctx.fillStyle = '#4a4';
            ctx.fillRect(tank.x, tank.y, tank.width, tank.height);
            
            // Draw barrel
            ctx.beginPath();
            ctx.strokeStyle = '#4a4';
            ctx.lineWidth = 5;
            const radians = angle * Math.PI / 180;
            ctx.moveTo(tank.x + tank.width/2, tank.y);
            ctx.lineTo(
                tank.x + tank.width/2 + Math.cos(radians) * tank.barrelLength,
                tank.y - Math.sin(radians) * tank.barrelLength
            );
            ctx.stroke();
        }

        function drawTargets() {
            targets.forEach(target => {
                if(!target.isHit) {
                    ctx.fillStyle = '#a44';
                    ctx.fillRect(target.x, target.y, target.width, target.height);
                }
            });
        }

        function fire() {
            const radians = angle * Math.PI / 180;
            projectiles.push({
                x: tank.x + tank.width/2 + Math.cos(radians) * tank.barrelLength,
                y: tank.y - Math.sin(radians) * tank.barrelLength,
                vx: Math.cos(radians) * power / 10,
                vy: -Math.sin(radians) * power / 10
            });
        }

        function update() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            drawTank();
            drawTargets();

            // Update and draw projectiles
            projectiles.forEach((proj, index) => {
                proj.x += proj.vx;
                proj.y += proj.vy;
                proj.vy += 0.2; // gravity

                ctx.beginPath();
                ctx.arc(proj.x, proj.y, 3, 0, Math.PI * 2);
                ctx.fillStyle = '#fff';
                ctx.fill();

                // Check for collisions with targets
                targets.forEach(target => {
                    if(!target.isHit && 
                       proj.x > target.x && 
                       proj.x < target.x + target.width &&
                       proj.y > target.y && 
                       proj.y < target.y + target.height) {
                        target.isHit = true;
                        projectiles.splice(index, 1);
                    }
                });

                // Remove projectiles that are off screen
                if(proj.y > canvas.height || proj.x > canvas.width) {
                    projectiles.splice(index, 1);
                }
            });

            // Check win condition
            if(targets.every(target => target.isHit)) {
                ctx.fillStyle = '#fff';
                ctx.font = '48px Arial';
                ctx.fillText('YOU WIN!', canvas.width/2 - 100, canvas.height/2);
                isGameOver = true;
            }

            if(!isGameOver) {
                requestAnimationFrame(update);
            }
        }

        // Controls
        document.getElementById('powerDown').addEventListener('click', () => {
            if(power > 10) power -= 5;
            powerDisplay.textContent = `Power: ${power}`;
        });

        document.getElementById('powerUp').addEventListener('click', () => {
            if(power < 100) power += 5;
            powerDisplay.textContent = `Power: ${power}`;
        });

        document.getElementById('angleDown').addEventListener('click', () => {
            if(angle > 0) angle -= 5;
            angleDisplay.textContent = `Angle: ${angle}°`;
        });

        document.getElementById('angleUp').addEventListener('click', () => {
            if(angle < 90) angle += 5;
            angleDisplay.textContent = `Angle: ${angle}°`;
        });

        document.getElementById('fire').addEventListener('click', fire);

        update();
    </script>
</body>
</html>