Links und Funktionen
Sprachumschaltung

Navigationspfad


Inhaltsbereich

Raumschiff

steuerbares Objekt

Raumschiff.py — text/python-source, 6 KB (6255 bytes)

Dateiinhalt

from visual import *
from Objects import *
from math import *
from random import *
from time import *

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)

if __name__ == "__main__":
        r = Raumschiff((0,0,0),(0,0,0))

Funktionsleiste