Advertisement
cookertron

Python Pygame - Seek + Approach and Flee (WHITE)

Feb 10th, 2020
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 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. CIRCLE_RADIUS = 8
  29. CIRCLE_DIAMETER = CIRCLE_RADIUS * 2
  30. CIRCLE_MAX_SPEED = 4
  31. CIRCLE_MAX_FORCE = 0.1
  32. CIRCLE_APPROACH_RADIUS = 100
  33. CIRCLE_FLEE_RADIUS = 100
  34. CIRCLE_COLOR_1 = [248, 173, 173]
  35. CIRCLE_COLOR_2 = [172, 40, 42]
  36.  
  37. MATRIX_SIZE = 30 * CIRCLE_DIAMETER
  38.  
  39. class circles:
  40.     class circle:
  41.         def __init__(s, x, y, color):
  42.             global CIRCLE_MAX_SPEED
  43.             global W, H
  44.  
  45.             s.pos = pygame.math.Vector2(rnd(0, W), rnd(0, H))
  46.             s.target = pygame.math.Vector2(x, y)
  47.             s.vel = pygame.math.Vector2()
  48.             s.acc = pygame.math.Vector2()
  49.  
  50.             s.color = color
  51.  
  52.         def draw(s):
  53.             global CIRCLE_RADIUS
  54.             global WHITE
  55.             global DS
  56.  
  57.             pygame.draw.circle(DS, s.color, (int(s.pos.x), int(s.pos.y)), CIRCLE_RADIUS)
  58.  
  59.         def behaviour(s):
  60.             mp = pygame.mouse.get_pos()
  61.            
  62.             flee = s.flee(mp)
  63.             arrive = s.arrive(s.target)
  64.            
  65.             flee *= 1.5
  66.  
  67.             s.applyForce(arrive)
  68.             s.applyForce(flee)
  69.  
  70.         def arrive(s, target):
  71.             global CIRCLE_MAX_SPEED
  72.            
  73.             desired = (target - s.pos)
  74.             distance = desired.length()
  75.             desired.normalize_ip()
  76.            
  77.             if distance < CIRCLE_APPROACH_RADIUS:
  78.                 desired *= distance / CIRCLE_APPROACH_RADIUS * CIRCLE_MAX_SPEED
  79.             else:
  80.                 desired *= CIRCLE_MAX_SPEED
  81.  
  82.             steer = (desired - s.vel)
  83.             if steer.length() > CIRCLE_MAX_FORCE:
  84.                 steer.scale_to_length(CIRCLE_MAX_FORCE)            
  85.             return steer
  86.  
  87.         def flee(s, target):
  88.             global CIRCLE_MAX_SPEED, CIRCLE_MAX_FORCE, CIRCLE_FLEE_RADIUS
  89.             desired = (s.pos - target)
  90.  
  91.             if desired.length() < CIRCLE_FLEE_RADIUS:
  92.                 desired.normalize() * CIRCLE_MAX_SPEED
  93.                 return (desired - s.vel).normalize() * CIRCLE_MAX_FORCE
  94.             else:
  95.                 return pygame.math.Vector2()
  96.  
  97.         def applyForce(s, force):
  98.             s.acc += force
  99.        
  100.         def update(s):
  101.             s.pos += s.vel
  102.             s.vel += s.acc
  103.             s.acc.update(0)
  104.  
  105.     def __init__(s):
  106.         global CIRCLE_DIAMETER
  107.         global CIRCLE_COLOR_1, CIRCLE_COLOR_2, WHITE
  108.         global MATRIX_SIZE
  109.         global HW, HH
  110.  
  111.         mx = HW - int(MATRIX_SIZE / 2)
  112.         my = HH - int(MATRIX_SIZE / 2)
  113.        
  114.         s.container = []
  115.         for x in range(mx, mx + MATRIX_SIZE, CIRCLE_DIAMETER):
  116.             pay = 0
  117.             for y in range(my, my + MATRIX_SIZE, CIRCLE_DIAMETER):
  118.                 s.container.append(s.circle(x, y, WHITE))
  119.  
  120.     def draw(s):
  121.         for c in s.container:
  122.             c.draw()
  123.  
  124.     def updates(s):
  125.         for c in s.container:
  126.             c.behaviour()
  127.             c.update()
  128.  
  129.     def do(s):
  130.         s.draw()
  131.         s.updates()
  132.  
  133. pygame.init()
  134. DS = pygame.display.set_mode((W, H))
  135. TIME = gTime(FPS)
  136.  
  137. C = circles()
  138.  
  139. while True:
  140.     e = pygame.event.get()
  141.     if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
  142.  
  143.     DS.fill(BLACK)
  144.  
  145.     C.do()
  146.  
  147.     pygame.display.update()
  148.     TIME.tick()
  149.  
  150. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement