Dienstag, 27. April 2010

Python 046 Artifical Life :)


The eyes move from left to right and back.
And the arm are waving around. (at least in my version)

#artifical intelligence
#shiranutachi = an other form of live :)

from visual import *
import numpy as np
import time
from random import randint

N = 7 #number of shiranu
size = 3*N
Shiranu = []
leg1 = []
leg2 = []
arm1 = []
arm2 = []
eye1 = []
eye2 = []
eye1black = []
eye2black = []
arm1angle = []
arm2angle = []
xx = []
yy = []
zz = []
scene.center = (size/2.0, 0, size/2.0)

colors = [color.green, color.yellow, color.blue, color.red]

#instantiation
for i in range(N):
x = random.randint(0, size)
y = 0
z = random.randint(0, size)
print (x,y)
Shiranu = Shiranu + [sphere(pos=(x,y,z), radius=1, color=colors[i % 4])]
leg1 = leg1 + [curve(pos=[(x-0.5,y,z),(x-0.5,y-1.6,z)], color=colors[i % 4], radius=0.1)]
leg2 = leg2 + [curve(pos=[(x+0.5,y,z),(x+0.5,y-1.6,z)], color=colors[i % 4], radius=0.1)]
arm1 = arm1 + [curve(pos=[(x-0.95,y,z),(x-1.5,y+0.6,z)], color=colors[i % 4], radius=0.1)]
arm2 = arm2 + [curve(pos=[(x+0.95,y,z),(x+1.5,y+0.6,z)], color=colors[i % 4], radius=0.1)]
eye1 = eye1 + [sphere(pos=(x-0.40,y+0.1,z+0.67), radius=0.3, color=color.white)]
eye2 = eye2 + [sphere(pos=(x+0.40,y+0.1,z+0.67), radius=0.3, color=color.white)]
eye1black = eye1black + [sphere(pos=(x-0.45,y+0.1,z+0.90), radius=0.1, color=color.black)]
eye2black = eye2black + [sphere(pos=(x+0.45,y+0.1,z+0.90), radius=0.1, color=color.black)]
xx = xx + [x]
yy = yy + [y]
zz = zz + [z]

#motion
t = 0
dt = 0.1 #better not change
while t<3000:
t += dt
#todo: armmovement isnt ready
if t % 3.1415 > 2.0:
arm1angle = radians(t)
if t % 3.1415 < 1.0:
arm1angle = radians(-t)
else:
pass
print arm1angle
for i in range(N):
eye1black[i].pos.x = eye1black[i].pos.x + cos(t+i/1)/180 #this is for eye-movement
eye2black[i].pos.x = eye2black[i].pos.x + cos(t+i/1)/180
#todo: armmovement isnt ready
arm1[i].pos[0] = (xx[i]-0.95,yy[i],zz[i])
arm1[i].pos[1] = (xx[i]-0.95+cos(arm1angle)*1, yy[i]+sin(arm1angle)*1, zz[i])
if 200 > degrees(arm1angle) > 90:
pass
else:
arm2[i].pos[0] = (xx[i]+0.95,yy[i],zz[i])
arm2[i].pos[1] = (xx[i]+0.95+cos(arm1angle)*1, yy[i]+sin(arm1angle)*1, zz[i])
time.sleep(0.001)

Kommentare:

Jesse Aldridge hat gesagt…

The blog is killing the code indentation. :(

It would be convenient if you could post the .py files as well. Or maybe use pastebin?

kame hat gesagt…

Hello Jesse. Thanks for the comment. I will use pastebin from now!

 
eXTReMe Tracker