cookertron

Stacking Circles - Python Pygame

May 10th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. import pygame
  2. import pygame.gfxdraw
  3. from random import uniform as uRnd
  4. from random import randint as rnd
  5. import math, time
  6.  
  7. FPS = 120
  8. W, H = 1280, 720
  9. HW, HH = int(W / 2), int(H / 2)
  10.  
  11. WHITE = [255, 255, 255]
  12. BLACK = [  0,   0,   0]
  13.  
  14. # game time
  15. class gTime:
  16.     def __init__(s, fps): # input = frames per second
  17.         s.fps = fps
  18.         s.tpf = 1 / fps * 1000000000
  19.         s.time = 0
  20.         s.timeStamp = time.time_ns()
  21.  
  22.     def tick(s):
  23.         while time.time_ns() - s.timeStamp < s.tpf:
  24.             pass
  25.         s.timeStamp = time.time_ns()
  26.         s.time += 1
  27.  
  28. pygame.init()
  29. DS = pygame.display.set_mode((W, H))
  30. TIME = gTime(FPS)
  31.  
  32. GRAVITY = 0.1
  33.  
  34. BARRIER_X1 = HW - 50
  35. BARRIER_X2 = HW + 50
  36.  
  37. class circles:
  38.     class circle:
  39.         def __init__(s, pos):
  40.             s.x = pos[0]
  41.             s.y = pos[1]
  42.             s.dy = 0
  43.             s.updated = False
  44.  
  45.     def __init__(s):
  46.         s.circles = []
  47.         s.bx = [BARRIER_X1, BARRIER_X2]
  48.  
  49.     def add(s):
  50.         s.circles += [s.circle([HW - uRnd(-3, 3), 0])]
  51.  
  52.     def draw(s):
  53.         for c in s.circles:
  54.             pygame.draw.circle(DS, WHITE, (int(c.x), int(c.y)), 10)
  55.  
  56.     def update(s):
  57.         for c1 in s.circles:
  58.             x1 = c1.x
  59.             y1 = c1.y
  60.  
  61.             for c2 in s.circles:
  62.                 if c2 == c1: continue
  63.  
  64.                 x2 = c2.x
  65.                 y2 = c2.y
  66.                
  67.                 x = x2 - x1
  68.                 y = y2 - y1
  69.  
  70.                 d = math.hypot(x, y)
  71.  
  72.                 if d <= 20:
  73.                     c2.x += x / d * 2
  74.                     c2.y += y / d * 2
  75.                     g = c2.dy / d
  76.                     c2.dy -= g
  77.            
  78.             c1.y += c1.dy
  79.             c1.dy += GRAVITY
  80.  
  81.             if c1.y >= H - 10:
  82.                 c1.y = H - 10
  83.                 c1.dy = 0
  84.             if c1.x < s.bx[0] + 10:
  85.                 c1.x = s.bx[0] + 10
  86.             if c1.x > s.bx[1] - 10:
  87.                 c1.x = s.bx[1] - 10
  88. C = circles()
  89.  
  90. ADD_CIRCLE_TIME = FPS
  91. timer = ADD_CIRCLE_TIME
  92.  
  93. while True:
  94.     e = pygame.event.get()
  95.     if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
  96.  
  97.     DS.fill(BLACK)
  98.  
  99.     if timer:
  100.         timer -= 1
  101.         if not timer:
  102.             timer = ADD_CIRCLE_TIME
  103.             C.add()
  104.  
  105.     C.draw()
  106.     C.update()
  107.     if len(C.circles) > 100:
  108.         C.bx = [0, W]
  109.     else:
  110.         pygame.draw.line(DS, WHITE, [C.bx[0], 0], [C.bx[0], H])
  111.         pygame.draw.line(DS, WHITE, [C.bx[1], 0], [C.bx[1], H])
  112.  
  113.     pygame.display.update()
  114.     TIME.tick()
  115.  
  116. pygame.quit()
Add Comment
Please, Sign In to add comment