Advertisement
eigenbom

fireworks

Oct 25th, 2016
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.30 KB | None | 0 0
  1. # fireworks
  2. # by @eigenbom
  3.  
  4. import tdl
  5. import math
  6. import random
  7. from copy import deepcopy
  8.  
  9. class Game:
  10.     WIDTH, HEIGHT = 30, 25
  11.  
  12.     def __init__(self):
  13.         tdl.set_font("curses_square_24x24.png")
  14.         self.console = tdl.init(Game.WIDTH, Game.HEIGHT, title = "Test")
  15.         tdl.set_fps(24)
  16.  
  17.     def __del__(self):
  18.         del self.console
  19.  
  20. game = None
  21.  
  22. class Particle:
  23.     def __init__(self):
  24.         self.x = 0
  25.         self.y = 0
  26.         self.vx = 0
  27.         self.vy = 0
  28.         self.colour = None
  29.         self.age = 0
  30.         self.alive = True
  31.  
  32. class ParticleSystem(object):
  33.  
  34.     def __init__(self, x, y):
  35.         self.x = float(x)
  36.         self.y = float(y)
  37.         self.particles = []
  38.         self.t = 0
  39.         self.duration = 0.15
  40.         self.particle_duration = 1.3
  41.         self.particle_spawn_rate = 0.005
  42.         self.particle_spawn_t = 0
  43.         self.finished = False
  44.  
  45.     def update(self, dt):
  46.         if self.finished: return
  47.  
  48.         self.t += dt
  49.  
  50.         # update existing
  51.         num_active = 0
  52.         for p in self.particles:
  53.             if not p.alive: continue
  54.             num_active = num_active + 1
  55.             p.x += p.vx * dt
  56.             p.y += p.vy * dt
  57.             p.vy += dt * 58
  58.             p.age += dt
  59.             if p.age > self.particle_duration:
  60.                 p.alive = False
  61.  
  62.         # flag if we're finished
  63.         if self.t > self.duration and num_active == 0:
  64.             self.finished = True
  65.  
  66.         # create new
  67.         if self.t < self.duration:
  68.             self.particle_spawn_t += dt
  69.             while self.particle_spawn_t > 0:
  70.                 self.particle_spawn_t -= self.particle_spawn_rate
  71.                 p = Particle()
  72.                 p.x = self.x #  + random.uniform(-1,1)
  73.                 p.y = self.y # + random.uniform(-1,1)
  74.                 p.vx = random.uniform(-2.0, 2.0)
  75.                 p.vy = random.uniform(-4.2, -2)
  76.                 l = math.sqrt(p.vx*p.vx + p.vy*p.vy)
  77.                 if l==0:
  78.                     p.vx, p.vy = 0, 1
  79.                 else:
  80.                     p.vx, p.vy = p.vx / l, p.vy / l
  81.                 speed = 25 * random.uniform(0.8, 1.2)
  82.                 p.vx = p.vx * speed
  83.                 p.vy = p.vy * speed
  84.                 p.colour = (255,255,255)
  85.                 self.particles.append(p)
  86.  
  87. def sign(x, precision=0):
  88.     if x>precision: return 1
  89.     elif x<-precision: return -1
  90.     else: return 0
  91.  
  92. def main():
  93.     global game
  94.     game = Game()
  95.     con = game.console
  96.  
  97.     cells = [[(0,0) for i in range(con.width)] for j in range(con.height)]
  98.     mouse_down = False
  99.  
  100.     particle_systems = []
  101.     launchers = []
  102.  
  103.     while 1:
  104.         for event in tdl.event.get():
  105.             if event.type == 'QUIT':
  106.                 raise SystemExit()
  107.             elif event.type == 'MOUSEDOWN':
  108.                 mouse_down = True
  109.                 launchers.append((event.cell[0], event.cell[1], 0.0))
  110.             elif event.type == 'MOUSEUP':
  111.                 mouse_down = False
  112.  
  113.         # update
  114.         for i, l in enumerate(launchers):
  115.             x, y, a = l
  116.             a += 1/24
  117.             if a > 0.6:
  118.                 particle_systems.append(ParticleSystem(int(x), int(y)))
  119.                 launchers[i] = None
  120.             else:
  121.                 launchers[i] = x, y - 20 * 1/24, a
  122.  
  123.         for i, ps in enumerate(particle_systems):
  124.             ps.update(1/24)
  125.             if ps.finished:
  126.                 particle_systems[i] = None
  127.  
  128.         # draw
  129.         game.console.clear()
  130.  
  131.         particle_systems = [p for p in particle_systems if p is not None]
  132.         launchers = [l for l in launchers if l is not None]
  133.         for x, y, a in launchers:
  134.             game.console.draw_char(int(x), int(y), '|', fg=(125,125,125))
  135.  
  136.         for ps in particle_systems:
  137.             for p in ps.particles:
  138.                 if p.alive:
  139.                     pos = int(p.x), int(p.y)
  140.                     if 0 > pos[1] or pos[1] > con.height-1: continue
  141.                     if 0 > pos[0] or pos[0] > con.width -1: continue
  142.                     ch = '*'
  143.                     speed = math.sqrt(p.vx*p.vx + p.vy*p.vy)
  144.                     dvx = abs(p.vx) - abs(p.vy)
  145.                     dage = p.age / ps.particle_duration
  146.                     p2 = pos[0] + sign(p.vx, 9.5), pos[1] + sign(p.vy, 9.5)
  147.                     if speed < 0.1:
  148.                         ch = '.'
  149.                     elif speed > 0.1 and abs(dvx)<0.9:
  150.                         if p.vx*p.vy > 0:
  151.                             ch = '\\'
  152.                         else:
  153.                             ch = '/'
  154.                         p2 = pos[0] + sign(p.vx), pos[1] + sign(p.vy)
  155.                     elif dvx > 0:
  156.                         ch = '-'
  157.                     else:
  158.                         ch = '|'
  159.  
  160.                     fg = [
  161.                         (255, 255, 0),
  162.                         (196, 196, 125),
  163.                         (125, 125, 125),
  164.                         (50, 50, 50),
  165.                         (0, 0, 0),
  166.                     ][min(max(int(dage * 4), 0), 4)]
  167.  
  168.                     game.console.draw_char(pos[0], pos[1], ch, fg=fg)
  169.  
  170.                     if p2 and dage < 0.8:
  171.                         game.console.draw_char(p2[0], p2[1], '\2', fg=fg)
  172.  
  173.         tdl.flush()
  174.  
  175.     del game
  176.  
  177. if __name__=="__main__":
  178.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement