Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import pygame.gfxdraw
- from random import uniform as uRnd
- from random import randint as rnd
- import math, time
- FPS = 120
- W, H = 1280, 720
- HW, HH = int(W / 2), int(H / 2)
- WHITE = [255, 255, 255]
- BLACK = [ 0, 0, 0]
- # game time
- class gTime:
- def __init__(s, fps): # input = frames per second
- s.fps = fps
- s.tpf = 1 / fps * 1000000000
- s.time = 0
- s.timeStamp = time.time_ns()
- def tick(s):
- while time.time_ns() - s.timeStamp < s.tpf:
- pass
- s.timeStamp = time.time_ns()
- s.time += 1
- pygame.init()
- DS = pygame.display.set_mode((W, H))
- TIME = gTime(FPS)
- GRAVITY = 0.1
- BARRIER_X1 = HW - 50
- BARRIER_X2 = HW + 50
- class circles:
- class circle:
- def __init__(s, pos):
- s.x = pos[0]
- s.y = pos[1]
- s.dy = 0
- s.updated = False
- def __init__(s):
- s.circles = []
- s.bx = [BARRIER_X1, BARRIER_X2]
- def add(s):
- s.circles += [s.circle([HW - uRnd(-3, 3), 0])]
- def draw(s):
- for c in s.circles:
- pygame.draw.circle(DS, WHITE, (int(c.x), int(c.y)), 10)
- def update(s):
- for c1 in s.circles:
- x1 = c1.x
- y1 = c1.y
- for c2 in s.circles:
- if c2 == c1: continue
- x2 = c2.x
- y2 = c2.y
- x = x2 - x1
- y = y2 - y1
- d = math.hypot(x, y)
- if d <= 20:
- c2.x += x / d * 2
- c2.y += y / d * 2
- g = c2.dy / d
- c2.dy -= g
- c1.y += c1.dy
- c1.dy += GRAVITY
- if c1.y >= H - 10:
- c1.y = H - 10
- c1.dy = 0
- if c1.x < s.bx[0] + 10:
- c1.x = s.bx[0] + 10
- if c1.x > s.bx[1] - 10:
- c1.x = s.bx[1] - 10
- C = circles()
- ADD_CIRCLE_TIME = FPS
- timer = ADD_CIRCLE_TIME
- while True:
- e = pygame.event.get()
- if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
- DS.fill(BLACK)
- if timer:
- timer -= 1
- if not timer:
- timer = ADD_CIRCLE_TIME
- C.add()
- C.draw()
- C.update()
- if len(C.circles) > 100:
- C.bx = [0, W]
- else:
- pygame.draw.line(DS, WHITE, [C.bx[0], 0], [C.bx[0], H])
- pygame.draw.line(DS, WHITE, [C.bx[1], 0], [C.bx[1], H])
- pygame.display.update()
- TIME.tick()
- pygame.quit()
Add Comment
Please, Sign In to add comment