File size: 8,657 Bytes
fd5acb3
1574a0c
fd5acb3
1574a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd5acb3
 
1574a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd5acb3
1574a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd5acb3
1574a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd5acb3
1574a0c
 
 
fd5acb3
1574a0c
 
 
 
 
fd5acb3
1574a0c
 
 
 
 
 
 
fd5acb3
1574a0c
 
 
 
 
 
 
 
fd5acb3
1574a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd5acb3
 
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
<!DOCTYPE html>
<html>
<head>
  <style>
    body { margin: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background: #1a1a1a; }
    .controls { position: fixed; top: 10px; left: 10px; color: white; }
    button { margin: 5px; padding: 5px 10px; }
    #gameArea { cursor: crosshair; }
    .info-panel {
      position: fixed;
      top: 10px;
      right: 10px;
      color: white;
      background: rgba(0,0,0,0.7);
      padding: 15px;
      border-radius: 5px;
      font-family: Arial, sans-serif;
    }
    .score {
      font-size: 24px;
      font-weight: bold;
      margin-bottom: 10px;
    }
    .points-popup {
      position: absolute;
      color: #4CAF50;
      font-weight: bold;
      font-family: Arial, sans-serif;
      pointer-events: none;
      animation: floatUp 1s ease-out forwards;
    }
    @keyframes floatUp {
      0% { transform: translateY(0); opacity: 1; }
      100% { transform: translateY(-50px); opacity: 0; }
    }
    .drawing-guide {
      position: fixed;
      bottom: 10px;
      left: 10px;
      color: white;
      background: rgba(0,0,0,0.7);
      padding: 10px;
      border-radius: 5px;
    }
  </style>
</head>
<body>
  <div class="controls">
    <button onclick="addRobot()">Add Random Robot (+5 pts)</button>
    <button onclick="togglePause()">Pause/Play</button>
  </div>
  <div class="info-panel">
    <div class="score">Score: <span id="scoreDisplay">0</span></div>
    <div>Robots: <span id="robotCount">0</span></div>
  </div>
  <div class="drawing-guide">
    Click and drag to draw new robots (+5 pts). Collisions give +5 pts and growth!
  </div>
  <svg id="gameArea" width="800" height="600" style="background: #000;">
    <rect width="800" height="600" fill="none" stroke="#333" stroke-width="4"/>
    <line id="drawingLine" stroke="white" stroke-width="2" stroke-dasharray="5,5" visibility="hidden"/>
  </svg>

  <script>
    const svg = document.getElementById('gameArea');
    const drawingLine = document.getElementById('drawingLine');
    const robots = [];
    let paused = false;
    let isDrawing = false;
    let startX, startY;
    let score = 0;
    
    function updateScore(points, x, y) {
      score += points;
      document.getElementById('scoreDisplay').textContent = score;
      document.getElementById('robotCount').textContent = robots.length;
      
      // Create and animate points popup
      const popup = document.createElement('div');
      popup.className = 'points-popup';
      popup.textContent = `+${points}`;
      popup.style.left = `${x}px`;
      popup.style.top = `${y}px`;
      document.body.appendChild(popup);
      
      // Remove popup after animation
      setTimeout(() => popup.remove(), 1000);
    }
    
    class Robot {
      constructor(x, y, speed, direction, length) {
        this.segments = [];
        this.speed = speed || 2 + Math.random() * 2;
        this.direction = direction || Math.random() * Math.PI * 2;
        this.length = length || 5 + Math.floor(Math.random() * 5);
        this.color = `hsl(${Math.random() * 360}, 80%, 50%)`;
        this.x = x || Math.random() * 700 + 50;
        this.y = y || Math.random() * 500 + 50;
        
        this.createSegments();
      }
      
      createSegments() {
        // Remove old segments if they exist
        this.segments.forEach(seg => seg.element.remove());
        this.segments = [];
        
        // Create new segments
        for (let i = 0; i < this.length; i++) {
          const segment = document.createElementNS("http://www.w3.org/2000/svg", "circle");
          segment.setAttribute("r", "8");
          segment.setAttribute("fill", this.color);
          svg.appendChild(segment);
          this.segments.push({
            element: segment,
            x: this.x,
            y: this.y
          });
        }
      }
      
      grow() {
        this.length += 1;
        this.createSegments();
      }
      
      update() {
        // Update head position
        this.x += Math.cos(this.direction) * this.speed;
        this.y += Math.sin(this.direction) * this.speed;
        
        // Bounce off walls
        if (this.x < 10 || this.x > 790) {
          this.direction = Math.PI - this.direction;
          this.mutate();
        }
        if (this.y < 10 || this.y > 590) {
          this.direction = -this.direction;
          this.mutate();
        }
        
        // Update segments
        for (let i = this.segments.length - 1; i > 0; i--) {
          this.segments[i].x = this.segments[i - 1].x;
          this.segments[i].y = this.segments[i - 1].y;
        }
        this.segments[0].x = this.x;
        this.segments[0].y = this.y;
        
        // Update SVG elements
        this.segments.forEach(segment => {
          segment.element.setAttribute("cx", segment.x);
          segment.element.setAttribute("cy", segment.y);
        });
      }
      
      mutate() {
        // Random mutation when bouncing
        this.speed = Math.max(1, Math.min(6, this.speed + (Math.random() - 0.5)));
        this.color = `hsl(${Math.random() * 360}, 80%, 50%)`;
        this.segments.forEach(segment => {
          segment.element.setAttribute("fill", this.color);
        });
      }
      
      checkCollision(other) {
        const dx = this.x - other.x;
        const dy = this.y - other.y;
        const distance = Math.sqrt(dx * dx + dy * dy);
        
        if (distance < 16) {
          // Bounce off each other
          const angle = Math.atan2(dy, dx);
          this.direction = angle + Math.PI / 2;
          other.direction = angle - Math.PI / 2;
          
          // Grow and mutate both robots
          this.grow();
          other.grow();
          this.mutate();
          other.mutate();
          
          // Add points and show popup at collision location
          const svgRect = svg.getBoundingClientRect();
          const popupX = svgRect.left + this.x;
          const popupY = svgRect.top + this.y;
          updateScore(5, popupX, popupY);
        }
      }
    }
    
    // Mouse event handlers
    svg.addEventListener('mousedown', (e) => {
      const rect = svg.getBoundingClientRect();
      startX = e.clientX - rect.left;
      startY = e.clientY - rect.top;
      isDrawing = true;
      
      drawingLine.setAttribute('x1', startX);
      drawingLine.setAttribute('y1', startY);
      drawingLine.setAttribute('x2', startX);
      drawingLine.setAttribute('y2', startY);
      drawingLine.setAttribute('visibility', 'visible');
    });
    
    svg.addEventListener('mousemove', (e) => {
      if (isDrawing) {
        const rect = svg.getBoundingClientRect();
        const currentX = e.clientX - rect.left;
        const currentY = e.clientY - rect.top;
        
        drawingLine.setAttribute('x2', currentX);
        drawingLine.setAttribute('y2', currentY);
      }
    });
    
    svg.addEventListener('mouseup', (e) => {
      if (isDrawing) {
        const rect = svg.getBoundingClientRect();
        const endX = e.clientX - rect.left;
        const endY = e.clientY - rect.top;
        
        const dx = endX - startX;
        const dy = endY - startY;
        const distance = Math.sqrt(dx * dx + dy * dy);
        const direction = Math.atan2(dy, dx);
        
        const speed = Math.min(6, distance / 50);
        const length = Math.max(3, Math.min(12, Math.floor(distance / 30)));
        robots.push(new Robot(startX, startY, speed, direction, length));
        
        // Add points for new robot
        updateScore(5, e.clientX, e.clientY);
        
        drawingLine.setAttribute('visibility', 'hidden');
        isDrawing = false;
      }
    });
    
    svg.addEventListener('mouseleave', () => {
      if (isDrawing) {
        drawingLine.setAttribute('visibility', 'hidden');
        isDrawing = false;
      }
    });
    
    function addRobot() {
      const robot = new Robot();
      robots.push(robot);
      // Add points for new random robot
      const svgRect = svg.getBoundingClientRect();
      updateScore(5, svgRect.left + robot.x, svgRect.top + robot.y);
    }
    
    function togglePause() {
      paused = !paused;
    }
    
    function update() {
      if (!paused) {
        robots.forEach(robot => robot.update());
        
        for (let i = 0; i < robots.length; i++) {
          for (let j = i + 1; j < robots.length; j++) {
            robots[i].checkCollision(robots[j]);
          }
        }
      }
      requestAnimationFrame(update);
    }
    
    // Start with 3 robots
    for (let i = 0; i < 3; i++) {
      addRobot();
    }
    
    update();
  </script>
</body>
</html>