Advertisement
cookertron

Python Pygame - Colliding Balls

Nov 24th, 2021
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. import pygame
  2. from pygame import Rect
  3. from pygame import Vector2
  4. from random import randint, uniform
  5.  
  6. class ball:
  7.     def __init__(s, balls):
  8.         global PDR, BALL_RADIUS
  9.  
  10.         # find a space for the balls
  11.         found_space = False
  12.  
  13.         if balls:
  14.             while not found_space:
  15.                 v = Vector2(randint(0, PDR.w), randint(0, PDR.h))
  16.                 for b in balls:
  17.                     if b.v.distance_to(v) <= BALL_RADIUS * 2:
  18.                         continue
  19.                     found_space = True
  20.                     break
  21.         else:
  22.             v = Vector2(randint(0, PDR.w), randint(0, PDR.h))
  23.         s.v = v
  24.         # direction
  25.         s.d = Vector2(0, 1).rotate(randint(0, 359))
  26.  
  27. class balls:
  28.     def __init__(s, n):
  29.         s.table = []
  30.         for _ in range(n):
  31.             s.table.append(ball(s.table))
  32.    
  33.     def update(s):
  34.         global PDS, PDR, WHITE, BALL_RADIUS
  35.  
  36.         for b1 in s.table:
  37.             # check for collision with sides of PDS
  38.             if b1.v.x - BALL_RADIUS <= 0:
  39.                 b1.d.x = -b1.d.x
  40.                 b1.v.x = BALL_RADIUS
  41.             if b1.v.x + BALL_RADIUS >= PDR.w:
  42.                 b1.d.x = -b1.d.x
  43.                 b1.v.x = PDR.w - BALL_RADIUS
  44.             if b1.v.y - BALL_RADIUS <= 0:
  45.                 b1.d.y = -b1.d.y
  46.                 b1.v.y = BALL_RADIUS
  47.             if b1.v.y + BALL_RADIUS >= PDR.h:
  48.                 b1.d.y = -b1.d.y
  49.                 b1.v.y = PDR.h - BALL_RADIUS
  50.  
  51.             # check for collision with other balls
  52.             for b2 in s.table:
  53.                 if b1 == b2:
  54.                     continue
  55.                 if b1.v.distance_to(b2.v) <= BALL_RADIUS * 2:
  56.                     b1.d = b1.d.reflect(b2.v - b1.v)
  57.  
  58.             # draw the ball to the PDS
  59.             pygame.draw.circle(PDS, WHITE, b1.v, BALL_RADIUS)
  60.             # apply velocity (_.d) to ball
  61.             b1.v += b1.d
  62.  
  63. pygame.init()
  64. PDR = Rect(0, 0, 1280, 720)
  65. PDS = pygame.display.set_mode(PDR.size)
  66.  
  67. BLACK = (0, 0, 0)
  68. WHITE = (255, 255, 255)
  69.  
  70. BALL_RADIUS = 20
  71.  
  72. T = balls(10)
  73.  
  74. quit_demo = False
  75. while not quit_demo:
  76.     for e in pygame.event.get():
  77.         if e.type == pygame.KEYUP:
  78.             if e.key == pygame.K_ESCAPE:
  79.                 quit_demo = True
  80.    
  81.     PDS.fill(BLACK)
  82.  
  83.     T.update()
  84.  
  85.     pygame.display.update()
  86.     pygame.time.Clock().tick(120)
  87.  
  88. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement