Browse Source

added collision detection

master
Fr3deric 7 years ago
parent
commit
b73f05b294
  1. 78
      generators/unrath.py

78
generators/unrath.py

@ -29,20 +29,9 @@ def get_random_color():
return colorsys.hsv_to_rgb(random.random(), s, v) return colorsys.hsv_to_rgb(random.random(), s, v)
def cap(x):
return min(1, max(0, x))
def cyc(x):
while x < 0:
x += 1
while x > 1:
x -= 1
return x
class Unrath: class Unrath:
def __init__(self): def __init__(self, world):
self.world = world
self.shape = set([(0, 0)]) self.shape = set([(0, 0)])
shapesize = random.randint(2, 5) shapesize = random.randint(2, 5)
while len(self.shape) < shapesize: while len(self.shape) < shapesize:
@ -63,25 +52,59 @@ class Unrath:
self.speed = d * random.randint(2, WIDTH/2) self.speed = d * random.randint(2, WIDTH/2)
self.color = get_random_color() self.color = get_random_color()
def update(self, dt): def update(self, dt, nocheck=False):
if (self.pos[0] + self.shapewidth < 0 and self.speed < 0) or \ if (self.pos[0] + self.shapewidth < 0 and self.speed < 0) or \
(self.pos[0] > WIDTH and self.speed > 0): (self.pos[0] > WIDTH and self.speed > 0):
self.finished = True self.finished = True
return
newpos = (self.pos[0] + dt*self.speed, self.pos[1])
newpoints = self.get_points(newpos)
collision = False
for unrath in self.world.unraethe - {self}:
if len(unrath.get_points().intersection(newpoints)) > 0:
collision = True
if abs(unrath.speed) > abs(self.speed):
fast = unrath
slow = self
else: else:
self.pos = (self.pos[0] + dt*self.speed, self.pos[1]) slow = unrath
fast = self
slow.speed = fast.speed * 1.1
slow.update(dt, True)
if not collision:
self.pos = newpos
def get_points(self, pos=None):
if pos is None:
pos = self.pos
return {(int(pos[0] + s[0]), int(pos[1] + s[1])) for s in self.shape}
def draw(self, frame): def draw(self, frame):
for p in self.shape: for p in self.get_points():
try: try:
frame.setPixel( frame.setPixel(p[0], p[1], convert_color(self.color))
int(self.pos[0] + p[0]),
int(self.pos[1] + p[1]),
convert_color(self.color)
)
except ValueError: except ValueError:
pass pass
class World:
def __init__(self, num_unraethe):
self.num_unraethe = num_unraethe
self.unraethe = {}
def update(self, dt):
self.unraethe = set(filter(lambda u: not u.finished, self.unraethe))
if len(self.unraethe) < self.num_unraethe:
self.unraethe.add(Unrath(self))
for u in self.unraethe:
u.update(dt)
def draw(self, frame):
for u in self.unraethe:
u.draw(frame)
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate Unrath animations') parser = argparse.ArgumentParser(description='Generate Unrath animations')
parser.add_argument('-d', '--delay', type=int, default=50) parser.add_argument('-d', '--delay', type=int, default=50)
@ -95,19 +118,12 @@ if __name__ == '__main__':
anim.tags['description'] = ' '.join(sys.argv) anim.tags['description'] = ' '.join(sys.argv)
t = 0 t = 0
unraethe = [] w = World(args.num_unraethe)
while t < args.time: while t < args.time:
t += args.delay / 1000 t += args.delay / 1000
frame = blup.animation.AnimationFrame(dim, args.delay) frame = blup.animation.AnimationFrame(dim, args.delay)
w.update(args.delay / 1000)
unraethe = list(filter(lambda u: not u.finished, unraethe)) w.draw(frame)
if len(unraethe) < args.num_unraethe:
unraethe.append(Unrath())
for u in unraethe:
u.update(args.delay / 1000)
u.draw(frame)
anim.addFrame(frame) anim.addFrame(frame)
blup.writebml.writeBml(anim, args.output_file) blup.writebml.writeBml(anim, args.output_file)

Loading…
Cancel
Save