Advertisement
here2share

# tk_Ball_To_Ball_Collisions.py

Apr 12th, 2023
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. # tk_Ball_To_Ball_Collisions.py
  2.  
  3. from tkinter import *
  4. import math
  5. import random
  6.  
  7. # Define constants
  8. NUM_POINTS = 30
  9. RADIUS = 10
  10. WIDTH = 500
  11. HEIGHT = 500
  12.  
  13. # Initialize tkinter window
  14. root = Tk()
  15. root.title("Ball-To-Ball Collisions")
  16. canvas = Canvas(root, width=WIDTH, height=HEIGHT)
  17. canvas.pack()
  18.  
  19. # Define function for calculating distance between two points
  20. def distance(x1, y1, x2, y2):
  21.     return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
  22.  
  23. # Create list of points with random locations and velocities
  24. points = []
  25. for i in range(NUM_POINTS):
  26.     x = random.randint(RADIUS, WIDTH-RADIUS)
  27.     y = random.randint(RADIUS, HEIGHT-RADIUS)
  28.     angle = 2 * math.pi * random.random()
  29.     speed = random.randint(1,5) # each ball has different speed (random between 1 to 5)
  30.     vx = speed * math.cos(angle)
  31.     vy = speed * math.sin(angle)
  32.     points.append({'x': x, 'y': y, 'vx': vx, 'vy': vy, 'speed': speed})
  33.  
  34. # Define function for updating the positions of the points
  35. def update_points():
  36.     for i, point in enumerate(points):
  37.         # Move point
  38.         point['x'] = (point['x'] + point['vx'])
  39.         point['y'] = (point['y'] + point['vy'])
  40.  
  41.         # Check for wall collisions and adjust position
  42.         if point['x'] - RADIUS <= 0:
  43.             point['x'] = RADIUS
  44.             point['vx'] = -point['vx']
  45.  
  46.         if point['x'] + RADIUS >= WIDTH:
  47.             point['x'] = WIDTH - RADIUS
  48.             point['vx'] = -point['vx']
  49.  
  50.         if point['y'] - RADIUS <= 0:
  51.             point['y'] = RADIUS
  52.             point['vy'] = -point['vy']
  53.  
  54.         if point['y'] + RADIUS >= HEIGHT:
  55.             point['y'] = HEIGHT - RADIUS
  56.             point['vy'] = -point['vy']
  57.  
  58.         # Check for collisions with other points
  59.         for j in range(i+1, len(points)):
  60.             other = points[j]
  61.             d = distance(point['x'], point['y'], other['x'], other['y'])
  62.             if d < 2*RADIUS:
  63.                 # Calculate new velocities to avoid collision
  64.                 angle = math.atan2(other['y'] - point['y'], other['x'] - point['x'])
  65.                 new_vx = -point['speed'] * math.cos(angle)
  66.                 new_vy = -point['speed'] * math.sin(angle)
  67.                
  68.                 # Update velocities of both points
  69.                 point['vx'], point['vy'] = new_vx, new_vy
  70.                 other['vx'], other['vy'] = -new_vx, -new_vy
  71.                
  72.         # Redraw point on canvas
  73.         canvas.coords(point['circle'], point['x']-RADIUS, point['y']-RADIUS, point['x']+RADIUS, point['y']+RADIUS)
  74.  
  75.     # Call this function again after a delay
  76.     root.after(15, update_points)
  77.  
  78. # Create circles on canvas for each point
  79. for point in points:
  80.     point['circle'] = canvas.create_oval(point['x']-RADIUS, point['y']-RADIUS, point['x']+RADIUS, point['y']+RADIUS, fill='yellow')
  81.  
  82. # Start animation loop
  83. update_points()
  84.  
  85. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement