Advertisement
cookertron

Python Pygame - Following The Mouse

Feb 8th, 2020
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. import pygame
  2. import pygame.gfxdraw
  3. import time
  4. import random
  5. import math
  6.  
  7. W, H = 1280, 720
  8. HW, HH = int(W / 2), int(H / 2)
  9.  
  10. FPS = 120
  11.  
  12. TIME_PER_PIXEL = (2 * FPS) / H
  13.  
  14. class gTime:
  15.     def __init__(s, fps): # input = frames per second
  16.         s.fps = fps
  17.         s.tpf = 1 / fps * 1000000000
  18.         s.time = 0
  19.         s.timeStamp = time.time_ns()
  20.  
  21.     def tick(s):
  22.         while time.time_ns() - s.timeStamp < s.tpf:
  23.             pass
  24.         s.timeStamp = time.time_ns()
  25.         s.time += 1
  26.  
  27. class ease:
  28.     def __init__(s, height, frames):
  29.         if frames < 2 or not height:
  30.             s.gravity = 0
  31.             s.velocity = 0
  32.         else:
  33.             s.gravity = 2 * height / (frames * (frames - 1))
  34.             s.velocity = s.gravity * (frames - 1)
  35.         s.iFrames = int(frames)
  36.         s.frames = frames
  37.  
  38.     def pv(s, index):
  39.         return s.gravity * index
  40.  
  41.     def nv(s, index):
  42.         return s.velocity - s.gravity * index
  43.  
  44. BLOCK_RADIUS = 10
  45.  
  46. class blocks:
  47.     class block:
  48.         def __init__(s):
  49.             s.reset()
  50.  
  51.         def draw(s):
  52.             global BLOCK_SIZE # dim
  53.             global DS
  54.  
  55.             if s.attacking:
  56.                 pygame.draw.circle(DS, [128, 0, 0], [s.mx, s.my], 4)
  57.             pygame.gfxdraw.filled_circle(DS, int(s.x), int(s.y1), BLOCK_RADIUS, [255, 255, 255] + [s.opacity])
  58.  
  59.         def do(s):
  60.             s.MODE()
  61.  
  62.         def reset(s):
  63.             global BLOCK_RADIUS # dimensions
  64.             global TIME_PER_PIXEL # time
  65.             global TIME
  66.             global H
  67.  
  68.             s.x = random.randint(0, W - BLOCK_RADIUS)
  69.             s.y1 = -BLOCK_RADIUS
  70.             s.y2 = random.randint(0, H - BLOCK_RADIUS * 2)
  71.             s.e = ease(s.y2 + BLOCK_RADIUS, TIME_PER_PIXEL * s.y2)
  72.  
  73.             s.timeStamp = TIME.time
  74.             s.opacity = 255
  75.             s.attacking = False
  76.             s.dead = False
  77.  
  78.             s.MODE = s.drop
  79.  
  80.         def drop(s):
  81.             global TIME
  82.  
  83.             f = TIME.time - s.timeStamp
  84.             s.y1 += s.e.nv(f - 1)
  85.             if int(s.y1) >= s.y2 - 1: s.MODE = s.fade
  86.  
  87.         def fade(s):
  88.             s.opacity -= 1
  89.             if s.opacity == 64: s.MODE = s.prepareForAttack
  90.  
  91.         def prepareForAttack(s):
  92.             global TIME, TIME_PER_PIXEL
  93.  
  94.             s.mx, s.my = pygame.mouse.get_pos()
  95.  
  96.             s.dx, s.dy = s.mx - s.x, s.my - s.y1
  97.             s.d = math.sqrt((s.dx * s.dx) + (s.dy * s.dy))
  98.             if int(s.d) == 0:
  99.                 s.dead = True
  100.                 return
  101.  
  102.             s.dx /= s.d
  103.             s.dy /= s.d
  104.            
  105.             s.e = ease(s.d, TIME_PER_PIXEL * s.d)
  106.  
  107.             s.attacking = True
  108.             s.MODE = s.attack
  109.             s.timeStamp = TIME.time
  110.  
  111.         def attack(s):
  112.             global TIME
  113.  
  114.             elapsed = TIME.time - s.timeStamp - 1
  115.             if elapsed == s.e.iFrames:
  116.                 s.x = s.mx
  117.                 s.y1 = s.my
  118.                 s.timeStamp = TIME.time
  119.                 s.delayFrames = random.randint(0, 10)
  120.                 s.MODE = s.delay
  121.                 return            
  122.             v = s.e.pv(elapsed)
  123.             s.x += s.dx * v
  124.             s.y1 += s.dy * v
  125.  
  126.         # this stops the blocks bunching together
  127.         def delay(s):
  128.             global TIME
  129.             if TIME.time - s.timeStamp >= s.delayFrames:
  130.                 s.timeStamp = TIME.time
  131.                 s.MODE = s.prepareForAttack
  132.  
  133.            
  134.     def __init__(s, n):
  135.         s.container = [s.block() for index in range(n)]
  136.  
  137.     def do(s):
  138.         for b in s.container:
  139.             b.draw()
  140.        
  141.         deadBlocks = []
  142.         for b in s.container:
  143.             b.do()
  144.             if b.dead: deadBlocks.append(b)
  145.         for db in deadBlocks:
  146.             s.container.remove(db)
  147.  
  148. pygame.init()
  149. DS = pygame.display.set_mode((W, H))
  150. TIME = gTime(FPS)
  151.  
  152. B = blocks(100)
  153.  
  154. while True:
  155.     e = pygame.event.get()
  156.     if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
  157.  
  158.     DS.fill([0, 0, 0])
  159.    
  160.     B.do()
  161.  
  162.     pygame.display.update()
  163.     TIME.tick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement