-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex18.py
More file actions
43 lines (34 loc) · 1.32 KB
/
Copy pathex18.py
File metadata and controls
43 lines (34 loc) · 1.32 KB
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
import math
import matplotlib.pyplot as plt
import random
class Ex18:
def __init__(self):
self.message = "Exercice 18 initialized"
self.balls = [ball(0, 0, random.uniform(10, 20), math.radians(random.uniform(0, 360))) for _ in range(20)] # Initial position (50,50), velocity 20 units/s, direction 45 degrees
print(self.message)
def render(self, time):
plt.clf()
plt.xlim(-50, 50)
plt.ylim(-50, 50)
plt.gca().set_aspect('equal', adjustable='box')
# Draw the balls
for ball in self.balls:
circle = plt.Circle((ball.x, ball.y), 2, color='blue')
plt.gca().add_artist(circle)
plt.title(f"Time: {time:.2f}s")
plt.pause(0.01)
def run(self):
print("Running Exercice 18...")
for i in range(1000):
self.render(i * 0.01) # Render every 0.01 seconds
for ball in self.balls:
ball.update(0.01) # Update ball position after 0.01 seconds
class ball:
def __init__(self, x, y, velocity, direction):
self.x = x
self.y = y
self.velocity = velocity
self.direction = direction # radians
def update(self, dt):
self.x += self.velocity * dt * math.cos(self.direction)
self.y += self.velocity * dt * math.sin(self.direction)