Links und Funktionen
Sprachumschaltung

Navigationspfad
Sie sind hier: Startseite / Probestudium / 2010 / WS I: 3D-Programmierung / Gruppe Humanoid / roboter2.py


Inhaltsbereich

roboter2.py

roboter2.py — text/python-source, 14 KB (14538 bytes)

Dateiinhalt

# -*- coding: cp1252 -*-

from visual import*
from math import*
from random import *
#import Image

scene.userzoom = False
scene.range = (8,8,8)
scene.title="Guenther"

"""name = "sky2"
width, height = 512, 512
im = Image.open(name+".tga")
materials.saveTGA(name,im)
skytex = materials.texture(data=im, mapping="spherical")"""

class Umgebung(object):
    def __init__ (self):
        scene.background = (0.1,0.4,0.8)
        self.boden=box(size=(40,1,22),pos=(0,-6.5,0), color=(0,1,0))
        self.wand1=box(size=(1,20,22),pos=(20,4,0), color=(1,1,0))
        self.wand2=box(size=(1,20,22),pos=(-20,4,0), color=(1,1,0))
        self.schwarz=box(size=(40,10,22),pos=(0,-12,0), color=(0,1,0))
        #self.skybox=sphere(radius=200,material=skytex,pos=(0,-8,0))
        #self.skybox=box(size=(50,25,22),pos=(0,4,0), material=skytex)


class Roboter(object):
    def __init__ (self):
        self.x=0
        self.y=0.01
        
        self.gruppebein1=frame()
        self.gruppefuss1=frame()
        self.gruppebein2=frame()
        self.gruppefuss2=frame()

        self.gruppearm1=frame()
        self.gruppearm2=frame()
        self.gruppehand1=frame()
        self.gruppehand2=frame()

        self.gruppekopf=frame()
        
        self.kopf=sphere (radius=1, pos=(0,1.25,0), frame=self.gruppekopf, color=(1,0.8,0.6))
        self.auge1=sphere(radius=0.2, pos=(0.8,1.5,0.4), frame=self.gruppekopf, color=(1,0,0), material=materials.emissive)
        self.auge2=sphere(radius=0.2, pos=(0.8,1.5,-0.4), frame=self.gruppekopf, color=(1,0,0), material=materials.emissive)
        self.haare=sphere(radius=1.1,pos=(-0.2,1.35,0), frame=self.gruppekopf, color=(0.5,0.3,0.1))
        
        self.oberkoerper=cylinder (pos=(0,0.25,0), axis=(0,-2.75,0), radius=1, color=(0.8,0,0))
        self.hals=cylinder(pos=(0,1.25,0), axis=(0,-3,0), radius=0.5, color=(0.8,0,0))

        self.schulter1=sphere(radius=0.5, pos=(0,-0.25,1), color=(0.8,0,0), frame=self.gruppearm1)
        self.oberarm1=cylinder(pos=(0,-0.25,1), axis=(0,-1.5,0), radius=0.5, color=(0.8,0,0), frame=self.gruppearm1)
        self.armgelenk1=sphere (pos=(0,-1.75,1), radius=0.5, color=(0.8,0,0), frame=self.gruppehand1)
        self.unterarm1=cylinder(pos=(0,-1.75,1), axis=(0,-1.5,0), radius=0.5, color=(0.8,0,0), frame=self.gruppehand1)
        self.hand1=sphere(radius=0.5, pos=(0,-3.4,1), color=(1,0.8,0.6), frame=self.gruppehand1)

        self.schulter2=sphere(radius=0.5, pos=(0,-0.25,-1), color=(0.8,0,0), frame=self.gruppearm2)
        self.oberarm2=cylinder(pos=(0,-0.25,-1), axis=(0,-1.5,0), radius=0.5, color=(0.8,0,0), frame=self.gruppearm2)
        self.armgelenk2=sphere (pos=(0,-1.75,-1), radius=0.5, color=(0.8,0,0), frame=self.gruppehand2)
        self.unterarm2=c6=cylinder(pos=(0,-1.75,-1), axis=(0,-1.5,0), radius=0.5, color=(0.8,0,0), frame=self.gruppehand2)
        self.hand2=sphere(radius=0.5, pos=(0,-3.4,-1), color=(1,0.8,0.6), frame=self.gruppehand2)

        self.oberschenkelgelenk1=sphere(radius=0.5, pos=(0,-2.5,0.5), color=color.blue, frame=self.gruppebein1)
        self.oberschenkel1=cylinder(pos=(0,-2.5,0.5), axis=(0,-1.5,0), radius=0.5, color=color.blue, frame=self.gruppebein1)
        self.kniegelenk1=sphere(radius=0.5, pos=(0,-4,0.5), color=color.blue, frame=self.gruppefuss1)
        self.unterschenkel1=cylinder(pos=(0,-4,0.5), axis=(0,-1.5,0), radius=0.5, color=color.blue, frame=self.gruppefuss1)
        self.schuh1=box(size=(1.5,0.5,1), pos=(0.3,-5.5,0.5), color=(0,0,0), frame=self.gruppefuss1)

        self.oberschenkelgelenk2=sphere(radius=0.5, pos=(0,-2.5,-0.5), color=color.blue, frame=self.gruppebein2)
        self.oberschenkel2=cylinder(pos=(0,-2.5,-0.5), axis=(0,-1.5,0), radius=0.5, color=color.blue, frame=self.gruppebein2)
        self.kniegelenk2=sphere(radius=0.5, color=color.blue, pos=(0,-4,-0.5), frame=self.gruppefuss2)
        self.unterschenkel2=cylinder(pos=(0,-4,-0.5), axis=(0,-1.5,0), radius=0.5, color=color.blue, frame=self.gruppefuss2)
        self.schuh2=box(size=(1.5,0.5,1), pos=(0.3,-5.5,-0.5), color=(0,0,0), frame=self.gruppefuss2)

        self.gruppefuss1.frame=self.gruppebein1
        self.gruppefuss2.frame=self.gruppebein2
        self.gruppehand1.frame=self.gruppearm1
        self.gruppehand2.frame=self.gruppearm2

        


    def move(self):
        "Bewegt den Roboter"
        #Die einzelnen Zyklen:
        self.x+=self.y
        if 0 < self.x <= pi/4.:
            #Beine bewegen
            self.gruppebein1.rotate(angle=self.y, axis=(0,0,1),origin=(0,-2.5,0.5))
            self.gruppebein2.rotate(angle=0, axis=(0,0,1),origin=(0,-2.5,-0.5))
            self.gruppefuss1.rotate(angle=-1.5*self.y, axis=(0,0,1),origin=(0,-4,0.5))
            self.gruppefuss2.rotate(angle=-0.5*self.y, axis=(0,0,1),origin=(0,-4,-0.5))
            #Arme bewegen
            self.gruppearm1.rotate(angle=-0.5*self.y, axis=(0,0,1), origin=(0,0,1))
            self.gruppearm2.rotate(angle=0.25*self.y, axis=(0,0,1), origin=(0,0,-1))
            self.gruppehand1.rotate(angle=0, axis=(0,0,1), origin=(0,-1.75,1))
            self.gruppehand2.rotate(angle=1.5*self.y, axis=(0,0,1), origin=(0,-1.75,-1))
            #Kopf bewegen
            self.gruppekopf.rotate(angle=-self.y, axis=(0,1,0), origin=(0,1.25,0))
        elif self.x > pi/4. or self.x < -pi/4.:
            self.y*=-1
        elif -pi/4. <= self.x < 0:
            #Beine bewegen
            self.gruppebein1.rotate(angle=0, axis=(0,0,1),origin=(0,-2.5,0.5))
            self.gruppebein2.rotate(angle=-self.y, axis=(0,0,1),origin=(0,-2.5,-0.5))
            self.gruppefuss1.rotate(angle=0.5*self.y, axis=(0,0,1),origin=(0,-4,0.5))
            self.gruppefuss2.rotate(angle=1.5*self.y, axis=(0,0,1),origin=(0,-4,-0.5))
             #Arme bewegen
            self.gruppearm1.rotate(angle=-0.25*self.y, axis=(0,0,1), origin=(0,0,1))
            self.gruppearm2.rotate(angle=0.5*self.y, axis=(0,0,1), origin=(0,0,-1))
            self.gruppehand1.rotate(angle=-1.5*self.y, axis=(0,0,1), origin=(0,-1.75,1))
            self.gruppehand2.rotate(angle=0, axis=(0,0,1), origin=(0,-1.75,-1))
            #Kopf bewegen
            self.gruppekopf.rotate(angle=-self.y, axis=(0,1,0), origin=(0,1.25,0))


class Tanne (object):
    def __init__ (self, b):
        self.f=frame()
        self.stamm=cylinder(radius=0.5, pos=(0, -6, -5), axis=(0,3.5,0), color=(0.5,0.25,0.1), frame=self.f)
        self.blatt1=cone(radius=2, pos=(0,-2.5,-5), axis=(0,6,0), color=(0.1,0.5,0.1), frame=self.f)
        self.blatt2=cone(radius=2, pos=(0,-0.5,-5), axis=(0,6,0), color=(0.1,0.5,0.1), frame=self.f)
        self.blatt3=cone(radius=2, pos=(0,1.5,-5), axis=(0,6,0), color=(0.1,0.5,0.1), frame=self.f)
        self.f.x=22
        if b==True:
            self.f.z=0
            self.f.axis=(-1,0,0)

    def move(self):
        self.f.x-=0.01


class Baum (object):
    def __init__ (self, b):
        self.f=frame()
        self.stamm=cylinder(radius=0.5, pos=(0, -6, -5), axis=(0,6.5,0), color=(0.5,0.25,0.1), frame=self.f)
        self.laub=sphere(radius=3, pos=(0,3,-5), color=(0.1,0.5,0.1), frame=self.f)
        self.f.x=22
        if b==True:
            self.f.z=0
            self.f.axis=(-1,0,0)

    def move(self):
        self.f.x-=0.01


class Grab (object):
    def __init__ (self, b):
        self.f=frame()
        self.erde=cylinder(radius=4, pos=(0, -9, -10), axis=(0,0,8), color=(0.5,0.25,0.1), frame=self.f)
        self.kreuz1=box(size=(0.3,5,0.3), pos=(0,-3.5,-10), color=(0.9,0.9,0.9), frame=self.f)
        self.kreuz2=box(size=(3,0.3,0.3), pos=(0,-3,-10), color=(0.9,0.9,0.9), frame=self.f)
        self.f.x=22
        if b==True:
            self.f.z=0
            self.f.axis=(-1,0,0)

    def move(self):
        self.f.x-=0.01


class Eimer (object):
    def __init__ (self, b):
        self.f=frame()
        self.eimer1=ring(radius=1,thicknes=0.1,pos=(0,-6,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer2=ring(radius=1,thicknes=0.1,pos=(0,-5.8,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer3=ring(radius=1,thicknes=0.1,pos=(0,-5.6,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer4=ring(radius=1,thicknes=0.1,pos=(0,-5.4,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer5=ring(radius=1,thicknes=0.1,pos=(0,-5.2,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer6=ring(radius=1,thicknes=0.1,pos=(0,-5.0,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer7=ring(radius=1,thicknes=0.1,pos=(0,-4.8,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer8=ring(radius=1,thicknes=0.1,pos=(0,-4.6,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer9=ring(radius=1,thicknes=0.1,pos=(0,-4.4,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer10=ring(radius=1,thicknes=0.1,pos=(0,-4.2,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer11=ring(radius=1,thicknes=0.1,pos=(0,-4.0,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer12=ring(radius=1,thicknes=0.1,pos=(0,-3.8,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer13=ring(radius=1,thicknes=0.1,pos=(0,-3.6,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer14=ring(radius=1,thicknes=0.1,pos=(0,-3.4,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer15=ring(radius=1,thicknes=0.1,pos=(0,-3.2,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.eimer16=ring(radius=1,thicknes=0.1,pos=(0,-3.0,-5),color=(0.7,0.7,0.7),axis=(0,10,0),frame=self.f)
        self.boden=cylinder(radius=1, pos=(0,-6,-5), axis=(0,1,0), color=(0.7,0.7,0.7),frame=self.f)
        self.muell=sphere(radius=0.9, pos=(0,-3.6,-5), color=(0.5,0.5,0),frame=self.f)
        self.rand=ring(radius=1.05,thicknes=0.1,pos=(0,-3,-5),color=(0,0,0),axis=(0,10,0),frame=self.f)
        self.f.x=22
        if b==True:
            self.f.z=0
            self.f.axis=(-1,0,0)

    def move(self):
        self.f.x-=0.01


class Lampe (object):
    def __init__ (self, b):
        self.f=frame()
        self.stange=cylinder(radius=0.25, pos=(0, -6, -5), axis=(0,9,0), color=(0.3,0.3,0.3), frame=self.f)
        self.lampe=sphere(radius=1.5,pos=(0,4.5,-5),opacity=0.2,frame=self.f)
        self.ring=ring(radius=0.25,thickness=0.1,axis=(0,1,0),pos=(0,0,-5),color=(0.3,0.3,0.3), frame=self.f)
        self.basis=cone(radius=0.4, pos=(0,-6,-5), axis=(0,3,0), color=(0.3,0.3,0.3), frame=self.f)
        self.f.x=22
        if b==True:
            self.f.z=0
            self.f.axis=(-1,0,0)

    def move(self):
        self.f.x-=0.01


class Bank (object):
    def __init__ (self, b):
        self.f=frame()
        self.sitz=box(pos=(0,-4.5,-5), size=(9,0.75,3),color=(0.8,0.8,0.8), frame=self.f)
        self.bein1=box(pos=(-3,-6,-5), size=(0.75,3,3), color=(0.8,0.8,0.8), frame=self.f)
        self.bein2=box(pos=(3,-6,-5), size=(0.75,3,3), color=(0.8,0.8,0.8), frame=self.f)
        self.lehne=box(pos=(0,-3,-6.4), size=(9,2.25,0.2), color=(0.8,0.8,0.8), frame=self.f)
        self.f.x=23
        if b==True:
            self.f.z=0
            self.f.axis=(-1,0,0)

    def move(self):
        self.f.x-=0.01


class Wegweiser (object):
    def __init__ (self, b):
        self.f=frame()
        self.stange=cylinder(radius=0.2,pos=(0,-6,-5), axis=(0,7,0), color=(0.45,0.2,0.05), frame=self.f)
        self.schilder=box(size=(4,1,0.25),pos=(0,0.5,-5), color=(0.45,0.2,0.05), frame=self.f)
        self.spitze1=pyramid(axis=(1,0,0),size=(1,1,0.25), pos=(2,0.5,-5), color=(0.45,0.2,0.05), frame=self.f)
        self.spitze2=pyramid(axis=(-1,0,0),size=(1,1,0.25), pos=(-2,0.5,-5), color=(0.45,0.2,0.05), frame=self.f)
        self.f.x=22
        if b==True:
            self.f.z=0
            self.f.axis=(-1,0,0)

    def move(self):
        self.f.x-=0.01
        

        
#Hauptprogramm startet
#Umgebung+G�nther erzeugen und ein paar Variablen f�r sie Objekte definieren
u=Umgebung()                       
ro=Roboter()
listelinks=[]
listerechts=[]
abstandrechts=0.1
abstandlinks=0.1

#Endlosschleife der Bewegung:
while(1):
    rate(150) #kein Sprint
    r=1000 #beliebige, gro�e Zahl
    l=1000

    #Bei Bedarf neue Objekte erzeugen:
    if abstandrechts>0.0:
        if int(random()*200)==22: #kommt �berhaupt ein neues? Dient zur Variierung des Abstandes
            r=int(random()*100+1) #Und wenn ja, welches denn?
    if abstandlinks>0.0:
        if int(random()*200)==22:
            l=int(random()*100+1)

    #entsprechend der Zahl nun jetzt ein Objekt erzeugen (Die unterschiedlichen Bereiche dienen einer Variation der Auftrittswahrscheinlichkeiten
    if r<25:
        temp=Tanne(False)
        listerechts.append(temp)
        abstandrechts=-6.0
    elif r<50:
        temp=Baum(False)
        listerechts.append(temp)
        abstandrechts=-6.0
    elif r<55:
        temp=Grab(False)
        listerechts.append(temp)
        abstandrechts=-7.0
    elif r<70:
        temp=Eimer(False)
        listerechts.append(temp)
        abstandrechts=-4.0
    elif r<87:
        temp=Lampe(False)
        listerechts.append(temp)
        abstandrechts=-4.0
    elif r<97:
        temp=Bank(False)
        listerechts.append(temp)
        abstandrechts=-10.0
    elif r<101:
        temp=Wegweiser(False)
        listerechts.append(temp)
        abstandrechts=-6.0


    if l<25:
        temp=Tanne(True)
        listelinks.append(temp)
        abstandlinks=-6.0
    elif l<50:
        temp=Baum(True)
        listelinks.append(temp)
        abstandlinks=-6.0
    elif l<55:
        temp=Grab(True)
        listelinks.append(temp)
        abstandlinks=-7.0
    elif l<70:
        temp=Eimer(True)
        listelinks.append(temp)
        abstandlinks=-4.0
    elif l<87:
        temp=Lampe(True)
        listelinks.append(temp)
        abstandlinks=-4.0
    elif l<98:
        temp=Bank(True)
        listelinks.append(temp)
        abstandlinks=-10.0
    elif l<101:
        temp=Wegweiser(True)
        listelinks.append(temp)
        abstandlinks=-6.0


    #Alles bewegen
    abstandrechts+=0.01
    abstandlinks+=0.01
    
    ro.move()
    
    for i in listerechts:
        i.move()
        if listerechts[0].f.x < -23:
            i.f.visible=False
            del i
            listerechts.remove(listerechts[0])
        
    for i in listelinks:
        i.move()
        if listelinks[0].f.x < -23:
            i.f.visible=False
            del i
            listelinks.remove(listelinks[0])


    
    
    

Funktionsleiste