Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # fireworks
- # by @eigenbom
- import tdl
- import math
- import random
- from copy import deepcopy
- class Game:
- WIDTH, HEIGHT = 30, 25
- def __init__(self):
- tdl.set_font("curses_square_24x24.png")
- self.console = tdl.init(Game.WIDTH, Game.HEIGHT, title = "Test")
- tdl.set_fps(24)
- def __del__(self):
- del self.console
- game = None
- class Particle:
- def __init__(self):
- self.x = 0
- self.y = 0
- self.vx = 0
- self.vy = 0
- self.colour = None
- self.age = 0
- self.alive = True
- class ParticleSystem(object):
- def __init__(self, x, y):
- self.x = float(x)
- self.y = float(y)
- self.particles = []
- self.t = 0
- self.duration = 0.15
- self.particle_duration = 1.3
- self.particle_spawn_rate = 0.005
- self.particle_spawn_t = 0
- self.finished = False
- def update(self, dt):
- if self.finished: return
- self.t += dt
- # update existing
- num_active = 0
- for p in self.particles:
- if not p.alive: continue
- num_active = num_active + 1
- p.x += p.vx * dt
- p.y += p.vy * dt
- p.vy += dt * 58
- p.age += dt
- if p.age > self.particle_duration:
- p.alive = False
- # flag if we're finished
- if self.t > self.duration and num_active == 0:
- self.finished = True
- # create new
- if self.t < self.duration:
- self.particle_spawn_t += dt
- while self.particle_spawn_t > 0:
- self.particle_spawn_t -= self.particle_spawn_rate
- p = Particle()
- p.x = self.x # + random.uniform(-1,1)
- p.y = self.y # + random.uniform(-1,1)
- p.vx = random.uniform(-2.0, 2.0)
- p.vy = random.uniform(-4.2, -2)
- l = math.sqrt(p.vx*p.vx + p.vy*p.vy)
- if l==0:
- p.vx, p.vy = 0, 1
- else:
- p.vx, p.vy = p.vx / l, p.vy / l
- speed = 25 * random.uniform(0.8, 1.2)
- p.vx = p.vx * speed
- p.vy = p.vy * speed
- p.colour = (255,255,255)
- self.particles.append(p)
- def sign(x, precision=0):
- if x>precision: return 1
- elif x<-precision: return -1
- else: return 0
- def main():
- global game
- game = Game()
- con = game.console
- cells = [[(0,0) for i in range(con.width)] for j in range(con.height)]
- mouse_down = False
- particle_systems = []
- launchers = []
- while 1:
- for event in tdl.event.get():
- if event.type == 'QUIT':
- raise SystemExit()
- elif event.type == 'MOUSEDOWN':
- mouse_down = True
- launchers.append((event.cell[0], event.cell[1], 0.0))
- elif event.type == 'MOUSEUP':
- mouse_down = False
- # update
- for i, l in enumerate(launchers):
- x, y, a = l
- a += 1/24
- if a > 0.6:
- particle_systems.append(ParticleSystem(int(x), int(y)))
- launchers[i] = None
- else:
- launchers[i] = x, y - 20 * 1/24, a
- for i, ps in enumerate(particle_systems):
- ps.update(1/24)
- if ps.finished:
- particle_systems[i] = None
- # draw
- game.console.clear()
- particle_systems = [p for p in particle_systems if p is not None]
- launchers = [l for l in launchers if l is not None]
- for x, y, a in launchers:
- game.console.draw_char(int(x), int(y), '|', fg=(125,125,125))
- for ps in particle_systems:
- for p in ps.particles:
- if p.alive:
- pos = int(p.x), int(p.y)
- if 0 > pos[1] or pos[1] > con.height-1: continue
- if 0 > pos[0] or pos[0] > con.width -1: continue
- ch = '*'
- speed = math.sqrt(p.vx*p.vx + p.vy*p.vy)
- dvx = abs(p.vx) - abs(p.vy)
- dage = p.age / ps.particle_duration
- p2 = pos[0] + sign(p.vx, 9.5), pos[1] + sign(p.vy, 9.5)
- if speed < 0.1:
- ch = '.'
- elif speed > 0.1 and abs(dvx)<0.9:
- if p.vx*p.vy > 0:
- ch = '\\'
- else:
- ch = '/'
- p2 = pos[0] + sign(p.vx), pos[1] + sign(p.vy)
- elif dvx > 0:
- ch = '-'
- else:
- ch = '|'
- fg = [
- (255, 255, 0),
- (196, 196, 125),
- (125, 125, 125),
- (50, 50, 50),
- (0, 0, 0),
- ][min(max(int(dage * 4), 0), 4)]
- game.console.draw_char(pos[0], pos[1], ch, fg=fg)
- if p2 and dage < 0.8:
- game.console.draw_char(p2[0], p2[1], '\2', fg=fg)
- tdl.flush()
- del game
- if __name__=="__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement