Einzelspieler_Mehrschuss
Umgebung.py — text/python-source, 11 KB (11345 bytes)
Dateiinhalt
# -*- coding: utf-8 -*- # Ein Koordinatensystem mit visual # Einbinden des 3D-Moduls import socket from visual import * from threading import Thread from random import random from time import sleep # Szene-Einstellungen scene.userzoom = False # User darf zoomen? scene.userspin = False # User darf rotieren? #scene.forward = (-1,-1,-1) # Kameraposition scene.autoscale = False # Autoskalierung aktiv? scene.range = (1,1,1) # Kameraabstand von scene.center scene.center = (0,5,50) # Kamerablickpunkt: norm. (0,0,0) scene.up = (0,1,0) # Rotationsachse Kamera: norm. (0,1,0) scene.background = color.black # Hintergrundfarbe scene.fov = 2 # Kamerawinkeloeffnung: norm. 1 scene.fullscreen = False # Vollbild? norm. False scene.title = "Koordinaten" # Fenstertitel scene.width = 640 # Fensterbreite: norm. 200 scene.height = 1000 # Fensterhoehe: norm 200 scene.x = 0 # x-Koordinate d. Fensters: norm. 0 scene.y = 0 # y-Koordinate d. Fensters: norm. 0 FremdPORT=8000 # Port des Fremdservers HOST="141.84.220.104" # Adresse des Gegenueber EigenPORT=8001 # Port des eigenen Servers lastaction = "up" # -------------- Thread Umgebung ------------ class Umgebung(Thread): def __init__(self): Thread.__init__(self) def run(self): global zufall for i in range (haus_anzahl): zufall.append(random()) zufall.append(random()) saeule.append(box(pos=(zufall[i-1]*100-50, 6,zufall[i]*100-50),\ width=5+zufall[i-1]*5, height=12, length=7+zufall[i]*3, \ material=materials.bricks)) saeulentreffer.append(0) # -------------- Thread Spieler ------------ class Spieler(Thread): """Schnelle Verbindung zu Server""" def __init__(self, host=HOST, fport=FremdPORT, eport=EigenPORT): Thread.__init__(self) if self.__class__.__name__ == "Spieler": self.eport = eport # UDP-Socket fuer Eigenserver self.e = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.e.bind(("",eport)) self.host = host self.fport = fport self.f = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) def run(self): # interne Startmethode des Threads if self.__class__.__name__ == "Spieler": while True: # Endlosschleife der Serverausgabe (50 Zeichen) try: # Versuch, die Nachricht vom Gegenueber umzusetzen info = self.e.recvfrom(100)[0] print info exec(info) except: print "Fehler beim Ausfuehren von Serveranweisung" def action(self,num): # startet den Client, indem self.start() global lastaction global saeule u = Umgebung() u.start() if num == 0: objekte[num].visible=False # eigene Sphere fuer sich selber unsichtbar objekte[num+1].visible=True # gegnerische Sphere sichtbar else: objekte[num].visible=False # eigene Sphere fuer sich selber unsichtbar objekte[num-1].visible=True # gegnerische Sphere sichtbar while True: rate(50) if scene.kb.keys: # event waiting to be processed? s = scene.kb.getkey() # get keyboard info print s if s == 'up': # vorwaerts scene.center = scene.center+scene.forward*mag(scene.center-scene.mouse.camera) objekte[num].pos=scene.center lastaction = "up" elif s == 'down': # rueckwaerts scene.center = scene.center-scene.forward*mag(scene.center-scene.mouse.camera) objekte[num].pos=scene.center lastaction = "down" elif s == 'left': # links drehen newforward = rotate(scene.forward, axis=scene.up, angle=radians(5)) scene.center = scene.mouse.camera+newforward*mag(scene.center-scene.mouse.camera) scene.forward = newforward objekte[num].pos=scene.center elif s == 'right': # rechts drehen newforward = rotate(scene.forward, axis=scene.up, angle=radians(-5)) scene.center = scene.mouse.camera+newforward*mag(scene.center-scene.mouse.camera) scene.forward = newforward objekte[num].pos=scene.center elif s == 'k': # schiessen geschosse.append(Kugel()) # neuen Thread Kugel in geschosse setzen geschosse[-1].start() # letzte Kugel starten elif s == 'l': for i in range(10): scene.center = scene.center+scene.forward*mag(scene.center-scene.mouse.camera) scene.center.y = scene.center.y+0.5 objekte[num].pos=scene.center sleep(0.01) for i in range(10): scene.center = scene.center+scene.forward*mag(scene.center-scene.mouse.camera) scene.center.y = scene.center.y-0.5 objekte[num].pos=scene.center sleep(0.01) elif s == ' ': for i in range(10): scene.center.y = scene.center.y+0.5 objekte[num].pos=scene.center sleep(0.01) sleep(0.1) for i in range(10): scene.center.y = scene.center.y-0.5 objekte[num].pos=scene.center sleep(0.01) self.f.sendto("objekte[%s].pos=(%s,%s,%s)"\ %(num,objekte[num].x,objekte[num].y,objekte[num].z),\ (self.host, self.fport)) # Versand der Anweisung an Gegenueber for y in range(len(saeule)): if mag(objekte[num].pos-saeule[y].pos) < 7: if lastaction == "up": scene.center = scene.center-scene.forward*mag(scene.center-scene.mouse.camera) objekte[num].pos=scene.center elif lastaction == "down": scene.center = scene.center+scene.forward*mag(scene.center-scene.mouse.camera) objekte[num].pos=scene.center if aussenwand[0].pos[2]-objekte[num].pos[2] < 3 or \ aussenwand[1].pos[2]-objekte[num].pos[2]+3 > 1 or \ aussenwand[2].pos[0]-objekte[num].pos[0] < 3 or \ aussenwand[3].pos[0]-objekte[num].pos[0]+3 > 1: if lastaction == "up": scene.center = scene.center-scene.forward*mag(scene.center-scene.mouse.camera) objekte[num].pos=scene.center elif lastaction == "down": scene.center = scene.center+scene.forward*mag(scene.center-scene.mouse.camera) objekte[num].pos=scene.center # -------------- Thread Kugel ------------ class Kugel(Spieler): def __init__(self): Spieler.__init__(self) def run(self): global saeule global sauelentreffer global num global geschosse geschosse.append(sphere(pos=scene.center)) b = len(geschosse) -1 print b self.f.sendto("geschosse[%s]=sphere(pos=(%s,%s,%s)))" %(b, scene.center.x, scene.center.y, scene.center.z), (self.host, self.fport)) direction = scene.forward.astuple() stop = False for x in range(0,100,1): if stop: break rate(50) geschosse[b].pos += vector(direction)*2.1 self.f.sendto("geschosse[%s].pos=(%s,%s,%s)"\ %(b, geschosse[b].x, geschosse[b].y,geschosse[b].z),\ (self.host, self.fport)) """if num == 0: if mag(self.geschoss.pos - objekte[num+1].pos) < 5: print "Got him!" self.geschoss.visible = False del self.geschoss sleep(5) break if mag(self.geschoss.pos - objekte[num].pos) < 5: print "HIT" self.geschoss.visible = False del self.geschoss sleep(5) break elif num == 1: print "eins!" if mag(self.geschoss.pos - objekte[num-1].pos) < 5: print "Got him!" self.geschoss.visible = False del self.geschoss sleep(5) break if mag(self.geschoss.pos - objekte[num].pos) < 5: print"HIT" self.geschoss.visible = False del self.geschoss sleep(5) break """ for y in range(len(saeule)): kaputt = False if stop: break position = saeule[y].pos.astuple() position = vector(position) position[1] = 5 if mag(saeule[y].pos - geschosse[b].pos) < 5: geschosse[b].visible=False if saeulentreffer[y] == 0: saeule[y].material = materials.wood elif saeulentreffer[y] == 1: saeule[y].opacity = 0.5 else: saeule[y].visible=False del saeule[y] kaputt = True del saeulentreffer[y] if kaputt != True: saeulentreffer[y] = saeulentreffer[y] +1 stop = True break geschosse[b].visible=False haus_anzahl=40 floor = box(pos=(0,0,0), width=120, height=1, length=120, color=(0.4,0.4,0.4), material=materials.marble) ceiling = box(pos=(0,12,0), width=120, height=1, length=120, color=(0.4,0.4,0.4), material=materials.marble) aussenwand = [] aussenwand.append(box(pos=(0,6,60), length=120, width=1, height=12, material=materials.bricks)) aussenwand.append(box(pos=(0,6,-60), length=120, width=1, height=12, material=materials.bricks)) aussenwand.append(box(pos=(60,6,0), length=1, width=120, height=12, material=materials.bricks)) aussenwand.append(box(pos=(-60,6,0), length=1, width=120, height=12, material=materials.bricks)) saeule = [] saeulentreffer = [] objekte = [sphere(radius=2, color=color.green, pos=scene.center),\ sphere(radius=2, color=color.red, pos=scene.center)] geschosse = [] zufall = [] num = 0 if __name__ == "__main__": a=Spieler() a.action(1)