Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import pygame.gfxdraw
- import time
- import random
- import math
- W, H = 1280, 720
- HW, HH = int(W / 2), int(H / 2)
- FPS = 120
- TIME_PER_PIXEL = (2 * FPS) / H
- 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
- class ease:
- def __init__(s, height, frames):
- if frames < 2 or not height:
- s.gravity = 0
- s.velocity = 0
- else:
- s.gravity = 2 * height / (frames * (frames - 1))
- s.velocity = s.gravity * (frames - 1)
- s.iFrames = int(frames)
- s.frames = frames
- def pv(s, index):
- return s.gravity * index
- def nv(s, index):
- return s.velocity - s.gravity * index
- BLOCK_RADIUS = 10
- class blocks:
- class block:
- def __init__(s):
- s.reset()
- def draw(s):
- global BLOCK_SIZE # dim
- global DS
- if s.attacking:
- pygame.draw.circle(DS, [128, 0, 0], [s.mx, s.my], 4)
- pygame.gfxdraw.filled_circle(DS, int(s.x), int(s.y1), BLOCK_RADIUS, [255, 255, 255] + [s.opacity])
- def do(s):
- s.MODE()
- def reset(s):
- global BLOCK_RADIUS # dimensions
- global TIME_PER_PIXEL # time
- global TIME
- global H
- s.x = random.randint(0, W - BLOCK_RADIUS)
- s.y1 = -BLOCK_RADIUS
- s.y2 = random.randint(0, H - BLOCK_RADIUS * 2)
- s.e = ease(s.y2 + BLOCK_RADIUS, TIME_PER_PIXEL * s.y2)
- s.timeStamp = TIME.time
- s.opacity = 255
- s.attacking = False
- s.dead = False
- s.MODE = s.drop
- def drop(s):
- global TIME
- f = TIME.time - s.timeStamp
- s.y1 += s.e.nv(f - 1)
- if int(s.y1) >= s.y2 - 1: s.MODE = s.fade
- def fade(s):
- s.opacity -= 1
- if s.opacity == 64: s.MODE = s.prepareForAttack
- def prepareForAttack(s):
- global TIME, TIME_PER_PIXEL
- s.mx, s.my = pygame.mouse.get_pos()
- s.dx, s.dy = s.mx - s.x, s.my - s.y1
- s.d = math.sqrt((s.dx * s.dx) + (s.dy * s.dy))
- if int(s.d) == 0:
- s.dead = True
- return
- s.dx /= s.d
- s.dy /= s.d
- s.e = ease(s.d, TIME_PER_PIXEL * s.d)
- s.attacking = True
- s.MODE = s.attack
- s.timeStamp = TIME.time
- def attack(s):
- global TIME
- elapsed = TIME.time - s.timeStamp - 1
- if elapsed == s.e.iFrames:
- s.x = s.mx
- s.y1 = s.my
- s.timeStamp = TIME.time
- s.delayFrames = random.randint(0, 10)
- s.MODE = s.delay
- return
- v = s.e.pv(elapsed)
- s.x += s.dx * v
- s.y1 += s.dy * v
- # this stops the blocks bunching together
- def delay(s):
- global TIME
- if TIME.time - s.timeStamp >= s.delayFrames:
- s.timeStamp = TIME.time
- s.MODE = s.prepareForAttack
- def __init__(s, n):
- s.container = [s.block() for index in range(n)]
- def do(s):
- for b in s.container:
- b.draw()
- deadBlocks = []
- for b in s.container:
- b.do()
- if b.dead: deadBlocks.append(b)
- for db in deadBlocks:
- s.container.remove(db)
- pygame.init()
- DS = pygame.display.set_mode((W, H))
- TIME = gTime(FPS)
- B = blocks(100)
- while True:
- e = pygame.event.get()
- if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
- DS.fill([0, 0, 0])
- B.do()
- pygame.display.update()
- TIME.tick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement