Advertisement
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
- CIRCLE_RADIUS = 8
- CIRCLE_DIAMETER = CIRCLE_RADIUS * 2
- CIRCLE_MAX_SPEED = 4
- CIRCLE_MAX_FORCE = 0.1
- CIRCLE_APPROACH_RADIUS = 100
- CIRCLE_FLEE_RADIUS = 100
- CIRCLE_COLOR_1 = [248, 173, 173]
- CIRCLE_COLOR_2 = [172, 40, 42]
- MATRIX_SIZE = 30 * CIRCLE_DIAMETER
- class circles:
- class circle:
- def __init__(s, x, y, color):
- global CIRCLE_MAX_SPEED
- global W, H
- s.pos = pygame.math.Vector2(rnd(0, W), rnd(0, H))
- s.target = pygame.math.Vector2(x, y)
- s.vel = pygame.math.Vector2()
- s.acc = pygame.math.Vector2()
- s.color = color
- def draw(s):
- global CIRCLE_RADIUS
- global WHITE
- global DS
- pygame.draw.circle(DS, s.color, (int(s.pos.x), int(s.pos.y)), CIRCLE_RADIUS)
- def behaviour(s):
- mp = pygame.mouse.get_pos()
- flee = s.flee(mp)
- arrive = s.arrive(s.target)
- flee *= 1.5
- s.applyForce(arrive)
- s.applyForce(flee)
- def arrive(s, target):
- global CIRCLE_MAX_SPEED
- desired = (target - s.pos)
- distance = desired.length()
- desired.normalize_ip()
- if distance < CIRCLE_APPROACH_RADIUS:
- desired *= distance / CIRCLE_APPROACH_RADIUS * CIRCLE_MAX_SPEED
- else:
- desired *= CIRCLE_MAX_SPEED
- steer = (desired - s.vel)
- if steer.length() > CIRCLE_MAX_FORCE:
- steer.scale_to_length(CIRCLE_MAX_FORCE)
- return steer
- def flee(s, target):
- global CIRCLE_MAX_SPEED, CIRCLE_MAX_FORCE, CIRCLE_FLEE_RADIUS
- desired = (s.pos - target)
- if desired.length() < CIRCLE_FLEE_RADIUS:
- desired.normalize() * CIRCLE_MAX_SPEED
- return (desired - s.vel).normalize() * CIRCLE_MAX_FORCE
- else:
- return pygame.math.Vector2()
- def applyForce(s, force):
- s.acc += force
- def update(s):
- s.pos += s.vel
- s.vel += s.acc
- s.acc.update(0)
- def __init__(s):
- global CIRCLE_DIAMETER
- global CIRCLE_COLOR_1, CIRCLE_COLOR_2
- global MATRIX_SIZE
- global HW, HH
- p = pygame.image.load("p.png").convert()
- pa = pygame.PixelArray(p)
- pax = 0
- pay = 0
- mx = HW - int(MATRIX_SIZE / 2)
- my = HH - int(MATRIX_SIZE / 2)
- s.container = []
- for x in range(mx, mx + MATRIX_SIZE, CIRCLE_DIAMETER):
- pay = 0
- for y in range(my, my + MATRIX_SIZE, CIRCLE_DIAMETER):
- if pa[pax][pay] == 0:
- s.container.append(s.circle(x, y, CIRCLE_COLOR_1))
- else:
- s.container.append(s.circle(x, y, CIRCLE_COLOR_2))
- pay += 1
- pax += 1
- pa.close()
- del pa
- def draw(s):
- for c in s.container:
- c.draw()
- def updates(s):
- for c in s.container:
- c.behaviour()
- c.update()
- def do(s):
- s.draw()
- s.updates()
- pygame.init()
- DS = pygame.display.set_mode((W, H))
- TIME = gTime(FPS)
- C = circles()
- while True:
- e = pygame.event.get()
- if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
- DS.fill(BLACK)
- C.do()
- pygame.display.update()
- TIME.tick()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement