Links und Funktionen
Sprachumschaltung

Navigationspfad


Inhaltsbereich

Multi_RN

Python Source icon multi.py — Python Source, 9 KB (10152 bytes)

Dateiinhalt

# -*- coding: utf-8 -*-
# Client und Server gleichzeitig
import socket                   # Modul für Netzwerkverbindung
from threading import Thread    # Modul für Nebenläufigkeit
from visual import *            # 3D-Modul
from random import *

# Portnummer und Hostadresse des Servers: bei Bedarf aendern
FremdPORT=8001
HOST="141.84.220.97"
EigenPORT=8000

a = 100
b = 100

scene.title = "Robin City" # Fenstertitel
scene.x = 300	                    # Fensterpos v. links
scene.y = 400	                    # Fensterpos v. oben
##scene.height = 320              # Fensterhöhe
##scene.width = 700               # Fensterbreite
scene.range = (5,10,10)           # Abstand
#scene.scale = (1,0.1,100)
#scene.center = (0,10,2)         # Wohin schaut die Kamera - wo ist die Mitte des Fensters? Normalerweise (0,0,0)
                                # kann sich aber auch mit einem Objekt mitbewegenFestlegung Blickziel
scene.userspin = False          # Blickwechsel abgeschaltet
scene.userzoom = False          # Benutzerzoom abgeschaltet
scene.fov = 2                   # Kamerawinkel
##scene.background = (0.8,0.85,1) # Farbe Hintergrund
scene.stereodepth = 1           # Stereotiefe
scene.show_rendertime = False    # Anzeige der Berechnungszeiten
#scene.fullscreen = True         # Vollbildmodus
hausAnzahl = 30

#Inhalte des Fensters

scene.userspin = False           # Kann der Benutzer die Kamera bewegen?


class Feld(frame):
    def __init__(self):
        frame.__init__(self)
        self.Platte = box(frame = self, pos = (0,0,0), length = a, height = 0.3, width = b, color = color.white, material = materials.silver)

class Rahmen(frame):
    def __init__(self):
        frame.__init__(self)
        self.RandUnten  =   box(frame = self, pos = (0,1.35,b/2), length = a, height = 3, width = 0.3, color = color.white, material = materials.bricks)
        self.RandOben   =   box(frame = self, pos = (0,1.35,-b/2), length = a, height = 3, width = 0.3, color = color.white, material = materials.bricks)
        self.RandRechts =   box(frame = self, pos = (a/2,1.35,0), length = 0.3, height = 3, width = b, color = color.white, material = materials.bricks)
        self.RandLinks  =   box(frame = self, pos = (-a/2,1.35,0), length = 0.3, height = 3, width = b, color = color.white, material = materials.bricks)
        
class Spielfigur(frame):
    def __init__(self,farbe):
        frame.__init__(self)
        self.kegel = cone( frame = self, pos=(0,0.1,0), axis=(0,1,0), radius = 0.5, color = farbe)
        self.kugel = sphere( frame = self, pos=(0,1.2,0), radius = 0.4, color = farbe)
        self.pfeil = arrow( frame = self, pos= (0,1,0), axis = (1,0,0), color = color.yellow)
        self.rotate(frame = self, angle = pi/2, axis = (0,1,0))

        

##class haus():
##    def __init__(self):
##        for i in range(hausAnzahl): # Häuser durch Zufall generieren
##            zufall = random()
##            box(pos=(random()*100-50, (10+zufall*5)/2,random()*100-50),\
##                width=5+random()*5, height=10+zufall*5, length=7+random()*3, \
##                color=color.hsv_to_rgb(((float(i)/hausAnzahl),1,1)))


class city(frame):
    def __init__(self, farbe):
        frame.__init__(self)

        #Moeglichkeit 2: Material aus einem *Bild* erzeugen
        #(Hoehe und Breite des Bilds im unkomprimierten tga/targa-Format muessen 2er-Potenzen sein, zB 1024x2048
        tex3 = materials.texture(data=materials.loadTGA("haus.tga"), mapping = "spherical")
        #tex3 = materials.texture(data=Image.open("Bethlehem.tga"), mapping = "spherical")
        self.haus1 = box(frame = self, pos =(40,3.8,34), length = 13, height = 7, width = 9, color = farbe, material = tex3)
        self.haus2 = box(frame = self, pos =(7,12.8,20), length = 13, height = 25, width = 9, color = farbe, material = tex3)
        self.haus3 = box(frame = self, pos =(46,7.8,40), length = 13, height = 15, width = 9, color = farbe, material = tex3)
        self.haus4 = box(frame = self, pos =(25,2.8,40), length = 13, height = 5, width = 9, color = farbe, material = tex3)
        self.haus5 = box(frame = self, pos =(35,3.3,30), length = 13, height = 6, width = 9, color = farbe, material = tex3)
        self.haus6 = box(frame = self, pos =(40,4.3,25), length = 13, height = 8, width = 9, color = farbe, material = tex3)
        self.haus7 = box(frame = self, pos =(37,6.3,10), length = 13, height = 12, width = 9, color = farbe, material = tex3)
        self.haus8 = box(frame = self, pos =(10,3.8,37), length = 13, height = 7, width = 9, color = farbe, material = tex3)
        self.haus9 = box(frame = self, pos =(18,4.3,9), length = 13, height = 8, width = 9, color = farbe, material = tex3)
        self.haus10= box(frame = self, pos =(2,3.8,12), length = 13, height = 7, width = 9, color = farbe, material = tex3)
        self.Haus1 = box(frame = self, pos =(-10,0,38), length = 13, height = 20, width = 9, material = tex3)
        self.Haus2 = box(frame = self, pos =(-37,0,8), length =13, height = 16, width = 9,  material = tex3)
        self.Haus3 = box(frame = self, pos =(-22,0,-40), length = 13, height =17, width = 9,  material = tex3)
        self.Haus4 = box(frame = self, pos =(-45,0,26), length = 13, height =20, width = 9,  material = tex3)
        self.Haus5 = box(frame = self, pos =(-2,0,-17), length =13, height = 15, width = 9,  material = tex3)
        self.Haus6 = box(frame = self, pos =(0,0,7), length = 13, height = 16, width = 9,  material = tex3)
        self.Haus7 = box(frame = self, pos =(-18,0,25), length = 13, height = 19, width = 9, material = tex3)
        self.Haus8 = box(frame = self, pos =(-38,0,-16), length=13, height =20, width = 9, material = tex3)
        self.Haus9 = box(frame = self, pos =(-20,0,45), length =13, height =20, width =9,  material = tex3)
        self.Haus10= box(frame = self, pos =(-9,0,14), length = 13, height = 14, width = 9, material = tex3)



##class Haus(frame):
##    
##    def __init__(self, farbe):
##        frame.__init__(self)
##        self.haus = box( frame = self, pos=(0,3.3,0), length = 10, height = 6, width = 6, color = farbe)
##       # self.dach = pyramid( frame = self,color= farbe, pos=(30,10,20), size=(8,15,10),axis = (0,1,0))


Feld = Feld()
Rand = Rahmen()
objekte = [Spielfigur(color.blue),\
           Spielfigur(color.red)]
#haus = haus()
stadt = city(color.white)
#Haus = Haus(color.cyan)

class Client(Thread):
    """Schnelle Verbindung zu Server"""
    def __init__(self, host=HOST, fport=FremdPORT, eport=EigenPORT):
        Thread.__init__(self)
        self.host = host
        self.fport = fport
        self.eport = eport
        # UDP-Socket für Clientfunktion
        self.f = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        # UDP-Socket für Eigenserver
        self.e = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.e.bind(("",eport))
        
    def run(self):              # interne Startmethode des Threads
        while 1:                # Endlosschleife der Serverausgabe (50 Zeichen)
            try:                # Info vom Server hören und umsetzen
                #print self.f.recvfrom(50)[0]
                info = self.e.recvfrom(50)[0]
                exec(info)
            except:
                print "Fehler beim Ausführen von Serveranweisung"
                
    def action(self,obj):           # startet den Client, indem
        self.start()                # der Thread aufgerufen wird
        while True:                 # Endlosschleife bis Nachricht versandt
            rate(50)
            if scene.kb.keys:
                s = scene.kb.getkey()
                print s

                ## Collision_Detection: Falls ein Objekt berührt wird springe um n*axis zurück
                
                if objekte[obj].x <= 7.5 and objekte[obj].z <= 11.5 and objekte[obj].z >= 2.5 and objekte[obj].x >= -7.5 or \
                   objekte[obj].x <= 7.5 and objekte[obj].z <= 11.5 and objekte[obj].z >= 2.5 and objekte[obj].x >= -7.5 or \
                   objekte[obj].x <= 7.5 and objekte[obj].z <= 11.5 and objekte[obj].z >= 2.5 and objekte[obj].x >= -7.5 or \
                   objekte[obj].x <= 7.5 and objekte[obj].z <= 11.5 and objekte[obj].z >= 2.5 and objekte[obj].x >= -7.5 or \
                   objekte[obj].x <= 7.5 and objekte[obj].z <= 11.5 and objekte[obj].z >= 2.5 and objekte[obj].x >= -7.5 or \
                   objekte[obj].x <= 7.5 and objekte[obj].z <= 11.5 and objekte[obj].z >= 2.5 and objekte[obj].x >= -7.5 :
                    objekte[obj].pos = objekte[obj].pos - 3*objekte[obj].axis

                
                    
                elif s == 'left':   # links
                    objekte[obj].rotate (angle = pi/2, axis = (0,1,0))
                elif s == 'right':  # right
                    objekte[obj].rotate (angle = -pi/2, axis = (0,1,0))
                elif s == 'up':     # vor
                    objekte[obj].pos = objekte[obj].pos + 0.7*objekte[obj].axis
                    
                elif s == 'down':   # zurück
                    objekte[obj].pos = objekte[obj].pos - 0.8*objekte[obj].axis
                elif s == '0':      # zurück auf Ursprung setzen; Geschw. null; Kamera ran
                    objekte[obj].pos = (0,0,0)
                self.f.sendto("objekte[%s].pos=(%s,%s,%s)"\
                              %(obj,objekte[obj].x,objekte[obj].y,objekte[obj].z),\
                              (self.host, self.fport))
                scene.center = objekte[obj].pos + objekte[obj].axis #- vector(0,0.5,0)
                scene.forward = objekte[obj].axis + vector(0,-0.5,0)
            


##            ## Collision_Detection: Falls ein Objekt berührt wird springe um n*axis zurück        
##                if objekte[obj].x <= 7.5 and objekte[obj].z <= 11.5 and objekte[obj].z >= 2.5 and objekte[obj].x >= -7.5:
##                    objekte[obj].pos = objekte[obj].pos - 3*objekte[obj].axis



# Hier Hostadresse, FremdPort, Eigenport dem Konstruktor übergeben
c = Client("141.84.220.97", 8000, 8001)
# 0 = Blaues Hütchen, 1 = Rot
#for i in range (6):
c.action(1)

#"141.84.220.97", 8000, 8001

Funktionsleiste