My first aquarium inhabitant.
With thanks to Frederik for helping me out with the nice organic movement.
# Script based on the animation example "parade.py"size(800,600)speed(60)fromrandomimport seed
frommathimport sin, cos, tan, atan, acos, asin
# define an oval that draws from the centredefcircle(x,y,size):
oval(x-size/2, y-size/2, size, size)defclamp(v, minv, maxv):
returnmin(max(v, minv), maxv)class Worm:
# Initialize a worm, set all the values to their defaults.def__init__(self):
self.x, self.y = random(20,WIDTH-20), random(20,HEIGHT-20)self.vx, self.vy = random(-1.0, 1.0), random(-1.0, 1.0)self.size = 19self.dx = self.dy = self.ds = 0.0self.color = 0self.path = []# a list for remembering x,y posistionsself.fill = (1,1,1,0.5)self.strokewidth = 5self.stroke = (1,1,1,0.3)self.d = 0.5self.maxspeed=5.0# Update the worm defupdate(self):
self.vx += random(-self.d, self.d)self.vy += random(-self.d, self.d)# Clamping keeps the speed in controleself.vx = clamp(self.vx,-self.maxspeed,self.maxspeed)self.vy = clamp(self.vy,-self.maxspeed,self.maxspeed)self.x += self.vxself.y += self.vy# This keeps the worm in the screenifself.x > WIDTH+20orself.x < -20:
self.vx = -self.vxself.x += self.vxifself.y > HEIGHT+20orself.y < -20:
self.vy = -self.vyself.y += self.vy# Updating the size for a nice sin movement self.size +=sin(FRAME/3.0)/5# This keeps a list in the path listself.pt = [self.x,self.y]self.path.append(self.pt)# Nodebox only needs to remember the 20 last positions# so this deletes the lists "history".self.path = self.path[-20:]defdraw(self, lvl=20):
background(0,0,1)
q= 19
ds= 0push()# Every circle follows the one before him, we have 20 circles# so we start level=20 and for every time we reloop, the level# goes down with 1 and a smaller circle is drawn.while lvl>1:
fill(self.fill)strokewidth(self.strokewidth)stroke(self.stroke)scale(0.9)
lvl-=1# In the beginning we don't have 19 positions, to avoid# an error report, we use this small trick.try:
a=self.path[q]exceptIndexError:
a=self.path[-1]
x=a[0]# calls for the x position for the circle before.
y=a[1]# calls for the y position for the circle before.
q-=1
circle(x, y, self.size)pop()defsetup():
global worms
worms = []
n=1# n is the number of worms we want.for i inrange(n):
worms.append(Worm())defdraw():
global worms
for worm in worms:
worm.update()
worm.draw()
Worm
Posted by FredericAlbers on Jun 11, 2007My first aquarium inhabitant.
With thanks to Frederik for helping me out with the nice organic movement.