Links und Funktionen
Sprachumschaltung

Navigationspfad


Inhaltsbereich

SpaceTraveler

Kompletter Code in einer Datei

SpaceTraveler.py — text/python-source, 19 KB (19673 bytes)

Dateiinhalt

from math import *
from random import random
from visual import *
from math import *
from random import *
from time import *
from time import *
from threading import *

from visual import *
import math

class MovingObject(object):
    def __init__ (self, position=vector((0,0,0)), speed=vector((0,0,0)), col_radius=1, focus = False ):
        self.position     = position
        self.speed        = speed
        self.acceleration = vector((0,0,0))
        self.col_radius   = col_radius
        self.focus        = focus

        self.collision    = False
        self.collisions   = []

        #self.adjustFrameToDir = True
        self.rahmen       = frame(pos=self.position)
        
        #if self.adjustFrameToDir:
            #self.rahmen.rotate(angle = math.atan2((-1)*speed[2], (-1)*speed[0]), axis = (0,1,0))
            #i = math.sqrt(speed[0]**2 + speed[2]**2)
            #self.rahmen.rotate(angle = (-1) * math.atan2(speed[1], i), axis = (1,0,0))

        self.i = 0
        

    def collisionWith(self,x):
        self.collision = True
        self.collisions.append(x)

    def getCollision(self):
        y = self.collisions.pop()
        if self.collisions == []:
            self.collision = False
        return y
    
    def step(self,dtime):
        self.eventInteraction()
        self.speed += self.acceleration
        self.position   += self.speed*dtime

        self.rahmen.pos = self.position
        self.rahmen.axis = self.speed
        #print self.position, "\t", self.speed, "\t", self.i
        self.i += 1
        if self.focus:
            scene.center = self.position
            scene.range=(18,18,18)
            scene.forward = self.speed+vector((0,-10,0))
        
    def eventInteraction(self):
        # abstract Method
        pass

class Raumschiff(MovingObject):
    
    def __init__ (self,pos,speed,col_radius=3,focus=False):
        MovingObject.__init__(self,pos,speed,col_radius, focus)
        #self.rahmen = frame(pos=self.pos)

        self.rumpf_1 =  pyramid(frame=self.rahmen,pos=(-0,0,-0)    ,axis=(1,0,0)  ,size=(4,0.25,2),color=(1,0,0))
        self.rumpf_2 =  pyramid(frame=self.rahmen,pos=(-2,0,-0)    ,axis=(1,0,0)  ,size=(4,1,6),color=(1,0,0))
        self.heck_1  =  pyramid(frame=self.rahmen,pos=(-3.5,0,-0)  ,axis=(1,0,0)  ,size=(3,1.5,4),color=(0.7,0,0))
        self.heck_2  =  pyramid(frame=self.rahmen,pos=(-3.49,0,-0) ,axis=(1,0,0)  ,size=(1.5,3,0.5),color=(0.2,0,0))
        self.duese_1 = cylinder(frame=self.rahmen,pos=(-3.6,0,1)   ,axis=(0.2,0,0),radius=0.2, color=(0,0,0))
        self.duese_2 = cylinder(frame=self.rahmen,pos=(-3.6,0,-1)  ,axis=(0.2,0,0),radius=0.2, color=(0,0,0))
        self.cockpit =  pyramid(frame=self.rahmen,pos=(2,0.05,-0)  ,axis=(1,0,0)  ,size=(1.5,0.25,0.5),color=(0,0,0))

        self.merk1 = vector((0,0,0))
        self.merk2 = vector((0,0,0))
        self.a=1.0
    
    def eventInteraction(self):
        if scene.kb.keys:
            while scene.kb.keys:
                s=scene.kb.getkey()
                if s == 'up':
                    self.acceleration= self.a/sqrt(self.speed[0]**2+self.speed[2]**2)*(self.speed[1]*self.speed/sqrt(self.speed[0]**2+self.speed[1]**2+self.speed[2]**2)-sqrt(self.speed[0]**2+self.speed[1]**2+self.speed[2]**2)*vector((0,1,0)))
                    try:
                        if self.merk1[0]/abs(self.merk1[0])==-self.acceleration[0]/abs(self.acceleration[0]) and self.merk1[2]/abs(self.merk1[2])==-self.acceleration[2]/abs(self.acceleration[2]) and (norm(self.speed)[1]<-0.5 or norm(self.speed)[1]>0.5):
                            self.a=-1*self.a
                            self.acceleration= self.a/sqrt(self.speed[0]**2+self.speed[2]**2)*(self.speed[1]*self.speed/sqrt(self.speed[0]**2+self.speed[1]**2+self.speed[2]**2)-sqrt(self.speed[0]**2+self.speed[1]**2+self.speed[2]**2)*vector((0,1,0))) 
                    except:
                        pass
                    self.merk1=self.acceleration
                elif s == 'down':
                    self.acceleration=-1*self.a/sqrt(self.speed[0]**2+self.speed[2]**2)*(self.speed[1]*self.speed/sqrt(self.speed[0]**2+self.speed[1]**2+self.speed[2]**2)-sqrt(self.speed[0]**2+self.speed[1]**2+self.speed[2]**2)*vector((0,1,0)))            
                    try:
                        if self.merk2[0]/abs(self.merk2[0])==-self.acceleration[0]/abs(self.acceleration[0]) and self.merk2[2]/abs(self.merk2[2])==-self.acceleration[2]/abs(self.acceleration[2]) and (norm(self.speed)[1]<-0.5 or norm(self.speed)[1]>0.5):
                            self.a=-1*self.a
                            self.acceleration=-1*self.a/sqrt(self.speed[0]**2+self.speed[2]**2)*(self.speed[1]*self.speed/sqrt(self.speed[0]**2+self.speed[1]**2+self.speed[2]**2)-sqrt(self.speed[0]**2+self.speed[1]**2+self.speed[2]**2)*vector((0,1,0))) 
                    except:
                        pass
                    self.merk2=self.acceleration

                if s == 'left':
                    self.acceleration= 1.0*(self.speed[2]/sqrt(self.speed[0]**2+self.speed[2]**2)*vector((1,0,0))-self.speed[0]/sqrt(self.speed[0]**2+self.speed[2]**2)*vector((0,0,1)))
                elif s == 'right':
                    self.acceleration=-1.0*(self.speed[2]/sqrt(self.speed[0]**2+self.speed[2]**2)*vector((1,0,0))-self.speed[0]/sqrt(self.speed[0]**2+self.speed[2]**2)*vector((0,0,1)))

                if s== 'w':
                    self.acceleration= 1.0*norm(self.speed)
                elif s== 's':
                    self.acceleration=-1.0*norm(self.speed)

                if s== 'f':
                    scene.forward=self.speed+vector((0,-10,0))
                    
                if s== 'q':
                    self.focus=False
                    scene.userzoom=1
                    scene.userspin=1
                    
                if s== 't':
                    self.focus=True
                    scene.userzoom=1
                    scene.userspin=1

        else: self.acceleration=vector((0,0,0))

        # collisions
        while self.collision:
            y = self.getCollision()
            if y.__class__.__name__ == "Planet":
                self.engine.interrupted=True
                self.rahmen.visible=0
                label(pos=self.rahmen.pos,text="GAME OVER!!!",height=50)

                Teilchen = []
                Teilchengeschwindigkeit = []
                for i in range(100):
                    Teilchen.append(sphere(pos=(self.rahmen.pos+(2*random()-1,2*random()-1,2*random()-1)),radius=0.5,color=(1,0,0),material=materials.emissive))
                for i in range(100):
                    Teilchengeschwindigkeit.append(vector((0.2*random()-0.1,0.2*random()-0.1,0.2*random()-0.1)))
                while True:
                    for i in range(100):
                        Teilchen[i].pos += Teilchengeschwindigkeit[i]
                        Teilchengeschwindigkeit[i] *= 0.999
                    sleep(0.01)
        if abs(self.position) > 1000:
            self.engine.interrupted=True
            self.rahmen.visible=0
            label(pos=self.rahmen.pos,text="GAME OVER!!!",height=50)

            Teilchen = []
            Teilchengeschwindigkeit = []
            for i in range(100):
                Teilchen.append(sphere(pos=(self.rahmen.pos+(2*random()-1,2*random()-1,2*random()-1)),radius=0.5,color=(1,0,0),material=materials.emissive))
            for i in range(100):
                Teilchengeschwindigkeit.append(vector((0.2*random()-0.1,0.2*random()-0.1,0.2*random()-0.1)))
            while True:
                for i in range(100):
                    Teilchen[i].pos += Teilchengeschwindigkeit[i]
                    Teilchengeschwindigkeit[i] *= 0.999
                sleep(0.01)


from visual import *        
from threading import *     
from math import *      
import time 
from random import random

# Sonne (km,km,kg):
s_radius=16
s_masse=30
# Merkur (km,d,d):
mer_radius=20
mer_umkreiszeit=40
mer_rotzeit=10
s_abstand_merkur=200


def scale_dist(rohdaten):
    "globale Hilfsfunktion: skaliert Entfernungen"
    return sqrt(rohdaten)

def scale_rad(rohdaten):
    "globale Hilfsfunktion: skaliert Radien"
    return sqrt(rohdaten)

class Planet(MovingObject):
    "Klasse erstellt Planetenobjekte"
    def __init__(self, pos = (0, 0, 0), axis = 0 , radius=mer_radius, color=(1,1,1), umkreiszeit=365, zentrum=None,\
                 abstand_zentrum=0, material=materials.wood):
        MovingObject.__init__(self,vector(0,0,0),vector(0,0,0),col_radius=radius)

        self.axis = axis
        self.position = vector(pos)
        self.planet = sphere(pos = self.position)
        self.f = frame(pos=self.position)
        self.planet.material = material
        self.planet.radius = radius
        self.planet.color = color
        self.umkreiszeit = umkreiszeit
        self.zentrum=zentrum
        self.abstand_zentrum = abstand_zentrum

        self.x = 90.0
        
    def step(self,dtime):
        if self.zentrum:
            '''
            self.planet.pos = (self.zentrum.f.pos.x+self.abstand_zentrum*cos(self.x/self.umkreiszeit)+self.axis, \
                           self.zentrum.f.pos.y + self.abstand_zentrum*cos(self.x/self.umkreiszeit)*cos(self.axis/(2*pi)*360)+self.axis ,\
                           self.zentrum.f.pos.z+self.abstand_zentrum*cos(self.x/self.umkreiszeit)+self.axis)   
            self.position = vector(self.planet.pos)
            '''
            self.planet.pos = (self.f.pos.x  * sin(self.x+self.axis), \
                              self.f.pos.y * sin (self.x + self.axis),\
                              self.f.pos.z * cos(self.x+self.axis))   
            self.position = vector(self.planet.pos)
            self.x += 0.005



#(self.f.pos.y + sin(self.x)*self.abstand_zentrum)*cos(self.axis)


class Engine(Thread):
    def __init__ (self,FPS):
        Thread.__init__(self)
        self.objectList=[]
        self.FPS=FPS
        self.interrupted = False

    def addObject(self,obj):
        self.objectList.append(obj)
        obj.engine = self

    def run(self):
        while not self.interrupted:
            for i in self.objectList:
                i.step(1.0/self.FPS)
                for x in self.objectList:
                    if (i.col_radius + x.col_radius) > abs(i.position - x.position) and i != x:
                        i.collisionWith(x)
            sleep(1.0/self.FPS)
        


#scene.stereo ="redcyan"
import Image
name   = "moon"
width  = 1024 
height = 512 
im = Image.open(name+".tga")
materials.saveTGA(name,im)
mondtextur = materials.texture(data=im, mapping="spherical")

engine = Engine(25)

scene.autoscale=0
r=Raumschiff(vector((100,100,100)),vector((1.1,1.1,-3.1)),3,1 )
engine.addObject(r)
b=box(material=materials.earth)
s = sphere(pos = (0, 0, 0, ), radius = 1000, color = (0, 0, 1), material = mondtextur)


scene.lights=[distant_light(direction=(0.22, 0.44, 0.88), color=color.gray(0.4))] # nur schwaches 'Weltraumlicht'
sonne  = Planet(pos=(0,0,0),radius=75, color = (1,1,0), material=materials.emissive)
sonnenlicht = local_light(pos=(0,0,0), color=color.white)
color1 = (1,0.7,0.3)


asteroid1 = Planet(pos = (-500, -400, -450),    axis =2*pi*random(),radius = 25, \
                   color = color1, material = mondtextur, umkreiszeit=50, zentrum=sonne, abstand_zentrum=sqrt(500**2+400**2+450**2))
asteroid2 = Planet(pos = (400, -540, -650), axis =2*pi*random(),radius = 55, \
                   color = color1, material=mondtextur, umkreiszeit=100, zentrum=sonne, abstand_zentrum=sqrt(400**2+540**2+650**2))
asteroid4 = Planet(pos = (-730, -500, 550),    axis =2*pi*random(),radius = 42, \
                   color = color1, material=mondtextur, umkreiszeit=150, zentrum=sonne, abstand_zentrum=sqrt(730**2+500**2+550**2))
asteroid5 = Planet(pos = (-650, 450, -550),  axis = 2*pi*random(),radius = 30, \
                   color = color1, material=mondtextur, umkreiszeit=90, zentrum=sonne, abstand_zentrum=sqrt(650**2+450**2+550**2))
asteroid6 = Planet(pos = (600, -500, 450),  axis =2*pi*random(),radius = 40, \
                   color = color1, material=mondtextur, umkreiszeit=70, zentrum=sonne, abstand_zentrum=sqrt(600**2+500**2+450**2))
asteroid7 = Planet(pos = (600, 770, -650), axis =2*pi*random(),radius = 35, \
                   color = color1, material=mondtextur, umkreiszeit=60, zentrum=sonne, abstand_zentrum=sqrt(600**2+770**2+650**2))
asteroid8 = Planet(pos = (-450, 450, 500),  axis =2*pi*random(),radius = 46, \
                   color = color1, material=mondtextur,umkreiszeit=66, zentrum=sonne, abstand_zentrum=sqrt(450**2+450**2+500**2))
asteroid9 = Planet(pos = (450, 500, 350),   axis =2*pi*random(),radius = 15, \
                   color = color1, material=mondtextur, umkreiszeit=80, zentrum=sonne, abstand_zentrum=sqrt(450**2+500**2+350**2))
asteroid10 = Planet(pos = (-650,-680, -654), axis =2*pi*random(),radius = 10,\
                    color = color1, material=mondtextur,umkreiszeit=100, zentrum=sonne, abstand_zentrum=sqrt(650**2+680**2+654**2))
asteroid11 = Planet(pos = (-290, -200, 200), axis =2*pi*random(),radius = 16, \
                    color = color1, material=mondtextur, umkreiszeit=110, zentrum=sonne, abstand_zentrum=sqrt(290**2+200**2+200**2))
asteroid12 = Planet(pos = (-420, 300, -500), axis =2*pi*random(),radius = 17, \
                    color = color1, material=mondtextur, umkreiszeit=120, zentrum=sonne, abstand_zentrum=sqrt(420**2+300**2+500**2))
asteroid13 = Planet(pos = (400, -500, -540),axis =2*pi*random(),radius = 20, \
                    color = color1, material=mondtextur, umkreiszeit=50, zentrum=sonne, abstand_zentrum=sqrt(400**2+500**2+540**2))
asteroid14 = Planet(pos = (-450, 590, 600), axis =2*pi*random(),radius = 25, \
                    color = color1, material=mondtextur, umkreiszeit=40, zentrum=sonne, abstand_zentrum=sqrt(450**2+590**2+600**2))
asteroid15 = Planet(pos = (650,660, -500),   axis =2*pi*random(),radius = 26, \
                    color = color1, material=mondtextur, umkreiszeit=30, zentrum=sonne, abstand_zentrum=sqrt(650**2+660**2+500**2))
asteroid16 = Planet(pos = (400, -650, 680),  axis =2*pi*random(),radius = 10, \
                    color = color1, material=mondtextur, umkreiszeit=70, zentrum=sonne, abstand_zentrum=sqrt(400**2+650**2+680**2))
asteroid17 = Planet(pos = (500, 453, 660), axis =2*pi*random(),radius = 45, \
                    color = color1, material=mondtextur, umkreiszeit=90, zentrum=sonne, abstand_zentrum=sqrt(500**2+453**2+660**2))
asteroid18 = Planet(pos = (-650, -600, 540),  axis =2*pi*random(),radius = 24, \
                    color = color1, material=mondtextur,umkreiszeit=80, zentrum=sonne, abstand_zentrum=sqrt(650**2+600**2+540**2))
asteroid19 = Planet(pos = (580, -400, -650),axis =2*pi*random(),radius = 40, \
                    color = color1, material=mondtextur,umkreiszeit=70, zentrum=sonne, abstand_zentrum=sqrt(580**2+400**2+650**2))
asteroid20 = Planet(pos = (-450, 500, -350), axis =2*pi*random(),radius = 30, \
                    color = color1, material=mondtextur,umkreiszeit=60, zentrum=sonne, abstand_zentrum=sqrt(450**2+500**2+350**2))
asteroid21 = Planet(pos = (-250, -450, 250),   axis =2*pi*random(),radius = 34, \
                    color = color1, material=mondtextur,umkreiszeit=60, zentrum=sonne, abstand_zentrum=sqrt(250**2+450**2+250**2))
asteroid22 = Planet(pos = (450, -350, 350),axis =2*pi*random(),radius = 29, \
                    color = color1, material=mondtextur,umkreiszeit=60, zentrum=sonne, abstand_zentrum=sqrt(450**2+350**2+350**2))
asteroid23 = Planet(pos = (-120, 160,250),   axis =2*pi*random(),radius = 50, \
                    color = color1, material=mondtextur,umkreiszeit=60, zentrum=sonne, abstand_zentrum=sqrt(120**2+160**2+250**2))
asteroid24 = Planet(pos = (500, 400, -450),    axis =2*pi*random(),radius = 45, \
                    color = color1, material=mondtextur,umkreiszeit=80, zentrum=sonne, abstand_zentrum=sqrt(500**2+400**2+450**2))
asteroid25= Planet(pos = (180, 190, 210),    axis =2*pi*random(),radius = 40, \
                   color = color1, material=mondtextur,umkreiszeit=120, zentrum=sonne, abstand_zentrum=sqrt(180**2+190**2+210**2))
asteroid26 = Planet(pos = (-230, -170, -250), axis =2*pi*random(),radius = 66, \
                    color = color1, material=mondtextur,umkreiszeit=40, zentrum=sonne, abstand_zentrum=sqrt(230**2+170**2+250**2))
asteroid27 = Planet(pos = (-500, 460, -450), axis =2*pi*random(),radius = 34, \
                    color = color1, material=mondtextur,umkreiszeit=180, zentrum=sonne, abstand_zentrum=sqrt(500**2+460**2+450**2))
asteroid28 = Planet(pos = (-380, -360, 280),  axis =2*pi*random(),radius = 46, \
                    color = color1, material=mondtextur,umkreiszeit=190, zentrum=sonne, abstand_zentrum=sqrt(380**2+360**2+280**2))
asteroid29 = Planet(pos = (380, -510, -450),  axis =2*pi*random(),radius = 22, \
                    color = color1, material=mondtextur,umkreiszeit=170, zentrum=sonne, abstand_zentrum=sqrt(380**2+510**2+450**2))
asteroid30 = Planet(pos = (690, -560, 490),   axis =2*pi*random(),radius = 22, \
                    color = color1, material=mondtextur,umkreiszeit=160, zentrum=sonne, abstand_zentrum=sqrt(690**2+560**2+490**2))
asteroid31 = Planet(pos = (-550, 450, 450),  axis =2*pi*random(),radius = 26, \
                    color = color1, material=mondtextur,umkreiszeit=140, zentrum=sonne, abstand_zentrum=sqrt(550**2+450**2+450**2))
asteroid32 = Planet(pos = (400, 200, -300),   axis =2*pi*random(),radius = 20, \
                    color = color1, material=mondtextur,umkreiszeit=200, zentrum=sonne, abstand_zentrum=sqrt(400**2+200**2+300**2))
asteroid33 = Planet(pos = (350, 400, 350),  axis =2*pi*random(),radius = 15, \
                    color = color1, material=mondtextur,umkreiszeit=150, zentrum=sonne, abstand_zentrum=sqrt(350**2+400**2+350**2))

engine.addObject(sonne)
engine.addObject(asteroid1)
engine.addObject(asteroid2)
engine.addObject(asteroid4)
engine.addObject(asteroid5)
engine.addObject(asteroid6)
engine.addObject(asteroid7)
engine.addObject(asteroid8)
engine.addObject(asteroid9)
engine.addObject(asteroid10)
engine.addObject(asteroid11)
engine.addObject(asteroid12)
engine.addObject(asteroid13)
engine.addObject(asteroid14)
engine.addObject(asteroid15)
engine.addObject(asteroid16)
engine.addObject(asteroid17)
engine.addObject(asteroid18)
engine.addObject(asteroid19)
engine.addObject(asteroid20)
engine.addObject(asteroid21)
engine.addObject(asteroid22)
engine.addObject(asteroid23)
engine.addObject(asteroid24)
engine.addObject(asteroid25)
engine.addObject(asteroid26)
engine.addObject(asteroid27)
engine.addObject(asteroid28)
engine.addObject(asteroid29)
engine.addObject(asteroid30)
engine.addObject(asteroid31)
engine.addObject(asteroid32)
engine.addObject(asteroid33)

engine.start()
scene.ambient = 0.5
scene.userzoom = 0
scene.userspin = 0
a = sphere(pos=(100,200,100),radius= 15, color = (0,0,1))

counter = 0


lab = label(heigth = 50, pos=(0,0,0),text="Punkte")

while 1:
    if 18 > abs(r.position - a.pos):
        a.pos =(random()*500, random()*500, random()*500)
        counter += 1
        lab.text = "Punkte: " + str(counter)
    sleep(0.5)
    
scene.fullscreen=1

Funktionsleiste