wurfl
neuer1.py — Python Source, 17 KB (18136 bytes)
Dateiinhalt
# -*- coding: utf-8 -*- from visual import * import random import time from threading import Thread PORT = 50200 #scene.title = "Spieler1" scene.background=color.white class Spielbrett (frame): def __init__(self): frame.__init__(self) Spielfeld = box (height=0.1, length=15, width=15, material =materials.wood, color=color.black, pos=(1.25,0,6.25)) #Grundfläche für Spielfeld feld1=Feld((0,0,0),1) feld2=Feld((1.25,0,0),2) feld3=Feld((2.5,0,0),3) feld4=Feld((2.5,0,1.25),4) feld5=Feld((2.5,0,2.5),5) feld6=Feld((2.5,0,3.75),6) feld7=Feld((2.5,0,5),7) feld8=Feld((3.75,0,5),8) feld9=Feld((5,0,5),9) feld10=Feld((6.25,0,5),10) feld11=Feld((7.5,0,5),11) feld12=Feld((7.5,0,6.25),12) feld13=Feld((7.5,0,7.5),13) feld14=Feld((6.25,0,7.5),14) feld15=Feld((5,0,7.5),15) feld16=Feld((3.75,0,7.5),16) feld17=Feld((2.5,0,7.5),17) feld18=Feld((2.5,0,8.75),18) feld19=Feld((2.5,0,10),19) feld20=Feld((2.5,0,11.25),20) feld21=Feld((2.5,0,12.5),21) feld22=Feld((1.25,0,12.5),22) feld23=Feld((0,0,12.5),23) feld24=Feld((0,0,11.25),24) feld25=Feld((0,0,10),25) feld26=Feld((0,0,8.75),26) feld27=Feld((0,0,7.5),27) feld28=Feld((-1.25,0,7.5),28) feld29=Feld((-2.5,0,7.5),29) feld30=Feld((-3.75,0,7.5),30) feld31=Feld((-5,0,7.5),31) feld32=Feld((-5,0,6.25),32) feld33=Feld((-5,0,5),33) feld34=Feld((-3.75,0,5),34) feld35=Feld((-2.5,0,5),35) feld36=Feld((-1.25,0,5),36) feld37=Feld((0,0,5),37) feld38=Feld((0,0,3.75),38) feld39=Feld((0,0,2.5),39) feld40=Feld((0,0,1.25),40) garage1=Feld((1.25,0,1.25),41) garage2=Feld((1.25,0,2.5),42) garage3=Feld((1.25,0,3.75),43) garage4=Feld((1.25,0,5),44) garage5=Feld((-3.75,0,6.25),45) garage6=Feld((-2.5,0,6.25),46) garage7=Feld((-1.25,0,6.25),47) garage8=Feld((0,0,6.25),48) garage9=Feld((2.5,0,6.25),49) garage10=Feld((3.75,0,6.25),50) garage11=Feld((5,0,6.25),51) garage12=Feld((6.25,0,6.25),52) garage13=Feld((1.25,0,7.5),53) garage14=Feld((1.25,0,8.75),54) garage15=Feld((1.25,0,10),55) garage16=Feld((1.25,0,11.25),56) anfang1=Feld((6.25,0,11.25),57) anfang2=Feld((7.5,0,11.25),58) anfang3=Feld((6.25,0,12.5),59) anfang4=Feld((7.5,0,12.5),60) anfang5=Feld((6.25,0,0),61) anfang6=Feld((7.5,0,0),62) anfang7=Feld((6.25,0,1.25),63) anfang8=Feld((7.5,0,1.25),64) anfang9=Feld((-5,0,0),65) anfang10=Feld((-3.75,0,0),66) anfang11=Feld((-5,0,1.25),67) anfang12=Feld((-3.75,0,1.25),68) anfang13=Feld((-5,0,11.25),69) anfang14=Feld((-3.75,0,11.25),70) anfang15=Feld((-5,0,12.5),71) anfang16=Feld((-3.75,0,12.5),72) anfang9.feld.color=color.yellow anfang10.feld.color=color.yellow anfang11.feld.color=color.yellow anfang12.feld.color=color.yellow feld33.feld.color=color.yellow garage5.feld.color=color.yellow garage6.feld.color=color.yellow garage7.feld.color=color.yellow garage8.feld.color=color.yellow anfang5.feld.color=color.green anfang6.feld.color=color.green anfang7.feld.color=color.green anfang8.feld.color=color.green feld3.feld.color=color.green garage1.feld.color=color.green garage2.feld.color=color.green garage3.feld.color=color.green garage4.feld.color=color.green anfang1.feld.color=color.red anfang2.feld.color=color.red anfang3.feld.color=color.red anfang4.feld.color=color.red feld13.feld.color=color.red garage9.feld.color=color.red garage10.feld.color=color.red garage11.feld.color=color.red garage12.feld.color=color.red anfang13.feld.color=color.blue anfang14.feld.color=color.blue anfang15.feld.color=color.blue anfang16.feld.color=color.blue feld23.feld.color=color.blue garage13.feld.color=color.blue garage14.feld.color=color.blue garage15.feld.color=color.blue garage16.feld.color=color.blue self.liste = [feld1,feld2,feld3,feld4,feld5,feld6,feld7,feld8,feld9,feld10,feld11,feld12,feld13,feld14,feld15,feld16,feld17,feld18,feld19,feld20,feld21,feld22,feld23,feld24,feld25,feld26,feld27,feld28,feld29,feld30,feld31,feld32,feld33,feld34,feld35,feld36,feld37,feld38,feld39,feld40,garage1,garage2,garage3,garage4,garage5,garage6,garage7,garage8,garage9,garage10,garage11,garage12,garage13,garage14,garage15,garage16,anfang1,anfang2,anfang3,anfang4,anfang5,anfang6,anfang7,anfang8,anfang9,anfang10,anfang11,anfang12,anfang13,anfang14,anfang15,anfang16] s1 = Spielfigur(groesse = 0.5, farbe = color.red) s2 = Spielfigur(groesse = 0.5, farbe = color.red) s3 = Spielfigur(groesse = 0.5, farbe = color.red) s4 = Spielfigur(groesse = 0.5, farbe = color.red) s5 = Spielfigur(groesse = 0.5, farbe = color.green) s6 = Spielfigur(groesse = 0.5, farbe = color.green) s7 = Spielfigur(groesse = 0.5, farbe = color.green) s8 = Spielfigur(groesse = 0.5, farbe = color.green) s9 = Spielfigur(groesse = 0.5, farbe = color.yellow) s10 = Spielfigur(groesse = 0.5, farbe = color.yellow) s11 = Spielfigur(groesse = 0.5, farbe = color.yellow) s12 = Spielfigur(groesse = 0.5, farbe = color.yellow) s13 = Spielfigur(groesse = 0.5, farbe = color.blue) s14 = Spielfigur(groesse = 0.5, farbe = color.blue) s15 = Spielfigur(groesse = 0.5, farbe = color.blue) s16 = Spielfigur(groesse = 0.5, farbe = color.blue) self.liste1 = [s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16] self.figurensetzen(56,0) self.figurensetzen(57,1) self.figurensetzen(58,2) self.figurensetzen(59,3) self.figurensetzen(60,4) self.figurensetzen(61,5) self.figurensetzen(62,6) self.figurensetzen(63,7) self.figurensetzen(64,8) self.figurensetzen(65,9) self.figurensetzen(66,10) self.figurensetzen(67,11) self.figurensetzen(68,12) self.figurensetzen(69,13) self.figurensetzen(70,14) self.figurensetzen(71,15) def figurensetzen(self,feld,figurnummer): x = self.liste1[figurnummer] y = self.liste[feld] x.pos = y.pos ## ##""" ## def run(self): ## radius = 0.5 ## s1 = Spielfigur(groesse = 0.5, farbe = color.red) ## pick = None ## while True: ## if scene.mouse.events: ## m1 = scene.mouse.getevent() ## if m1.drag and m1.pick in s1.objects: ## pick = m1.pick ## elif m1.drop: ## if pick != None: ## #print(pick.pos) ## for n in self.liste: ## #print(mag(pick.pos - n.pos)) ## if mag(pick.pos - vector(n.pos)) <= radius: ## ## #print(n.pos) ## break ## else: ## pass ## ## pick = None ## if pick: ## s1.Bewegen(scene.mouse.pos) ## new_pos = scene.mouse.project(normal=(0,1,0)) ## # if new_pos != m1.pick.pos: ## # pick.pos += new_pos - m1.pick.pos ## # m1.pick.pos = new_pos ##""" def run(self): pick = None #das angeklickte Objekt - am Anfang keines while True: if scene.mouse.events: m1 = scene.mouse.getevent() # get event if m1.drag: for f in self.liste1: if m1.pick in f.objects: drag_pos = m1.pickpos # where on the ball pick = f # pick now true (not None) break elif m1.drop: # released at end of drag pick = None # end dragging (None is false) feldindex = 0 for n in range (len(self.liste)): position = self.liste[n].pos if mag(position - f.pos) < mag((self.liste[feldindex].pos - f.pos)): feldindex = n f.pos = (self.liste[feldindex].pos) if pick: pick.Bewegen(scene.mouse.pos) class Feld (frame): def __init__(self, ort, nummer): frame.__init__(self,pos=ort) self.nummer=nummer self.feld=cylinder(radius=0.5,axis=(0,0.1,0), pos=ort) class Figur: def __init__(self, farbe = color.white, groesse = 1): figur = frame() radius = groesse hoehe = 3*radius self.fuss = cone(frame = figur, radius = radius, pos = (0,0,0), axis = (0,1,0), length = hoehe, color = farbe) self.kopf = sphere(frame = figur, radius = radius, pos = (0,hoehe,0), color = farbe) class Spielfigur(frame): def __init__(self, farbe = color.white, groesse = 1): frame.__init__(self) radius = groesse hoehe = 3*radius self.fuss = cone(frame = self, radius = radius, pos = (0,0,0), axis = (0,1,0), length = hoehe, color = farbe) self.kopf = sphere(frame = self, radius = radius, pos = (0,hoehe,0), color = farbe) def Bewegen(self, position): x = position[0] y = position[1] z = position[2] test = scene.mouse.project(normal=(0,1,0)) # tuple1 = (x,0,z) # tuple2 = (x,y,z) tuple3 = scene.mouse.project(normal=(0,1,0)) if y <= 0: self.pos = (tuple3) elif y > 0: self.pos = (tuple3) """ def Bewegen2(self): pick = None #das angeklickte Objekt - am Anfang keines while True: if scene.mouse.events: m1 = scene.mouse.getevent() # get event if m1.drag and m1.pick in s1.objects: # if touched drag_pos = m1.pickpos # where on the ball pick = m1.pick # pick now true (not None) elif m1.drop: # released at end of drag pick = None # end dragging (None is false) if pick: self.Bewegen(scene.mouse.pos) """ class Wuerfel(frame): def __init__(self, groesse = 1, pos=(0,0,0)): frame.__init__(self,pos=pos) wuerfel = box(pos = (1,1,1), length = groesse, width = groesse, height = groesse, color = color.white, frame=self) p1 = cylinder(pos = (1.41,1,1),axis=(0.1,0,0),radius=0.1,color = color.black,frame=self) p2 = cylinder(pos = (0.49,1.25,1.25),axis=(0.1,0,0),radius=0.1,color = color.black,frame=self) p3 = cylinder(pos = (0.49,1,1.25),axis=(0.1,0,0),radius=0.1,color = color.black ,frame=self) p4 = cylinder(pos = (0.49,0.75,1.25),axis=(0.1,0,0),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (0.49,1.25,0.75),axis=(0.1,0,0),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (0.49,1,0.75),axis=(0.1,0,0),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (0.49,0.75,0.75),axis=(0.1,0,0),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (1,1.41,1),axis=(0,0.1,0),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (0.75,1.41,0.75),axis=(0,0.1,0),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (1.25,1.41,0.75),axis=(0,0.1,0),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (0.75,1.41,1.25),axis=(0,0.1,0),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (1.25,1.41,1.25),axis=(0,0.1,0),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (1.25,0.49,1.25),axis=(0,0.1,0),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (0.75,0.49,0.75),axis=(0,0.1,0),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (1.25,1.25,0.49),axis=(0,0,0.1),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (1.25,0.75,0.49),axis=(0,0,0.1),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (0.75,1.25,0.49),axis=(0,0,0.1),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (0.75,0.75,0.49),axis=(0,0,0.1),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (0.75,0.75,1.41),axis=(0,0,0.1),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (1.25,1.25,1.41),axis=(0,0,0.1),radius=0.1,color = color.black,frame=self) p4 = cylinder(pos = (1,1,1.41),axis=(0,0,0.1),radius=0.1,color = color.black,frame=self) class Zufallsgenerator: def __init__(self): zahl=1 self.b=label(pos=(1,7,1), color=color.black) def wuerfeln(self): zahl = random.choice([1,2,3,4,5,6]) self.b.text = 'Du hast eine ' + str(zahl) + ' gewuerfelt!' time.sleep(3) self.b.visible=False class Steuerung(Thread): def __init__(self, spiel, port=50200): Thread.__init__(self) self.view=spiel # UDP-Client, der den Server als Subprozess startet import subprocess # Modul zum Starten von Subprozessen import socket # Modul fuer Netzwerkkommunikation self.PORT = port #Anlegen eines Socket self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # AF_INET = IPv4, SOCK_DGRAM = UDP self.s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1) # Broadcast ermoeglichen #Testen, ob es schon einen Server auf diesem Port gibt, notfalls selber starten try: self.s.sendto("print 'wer hoert mich?'", ('255.255.255.255', self.PORT)) # sende Nachricht 'an alle' self.s.settimeout(2) # warte 1/2 Sekunde.. print (self.s.recvfrom(64)[0]) # gibt Servermeldung aus #ein Problem, wenn timeout einmal gesetzt ist... self.s.settimeout(3000) except: # reagiert auf Zeitueberschreitung print ("Kein Server vorhanden - starte Server") # Rueckmeldung subprocess.Popen(['python','server_class.py',str(self.PORT)]) # Server starten self.s.setblocking(1) # nicht-blockierend self.s.sendto("print 'hallo'", ('255.255.255.255', self.PORT)) self.start() #Endlosmethode zum Abhoeren des Ports auf Nachrichten def run(self): while True: nachricht = self.s.recvfrom(128)[0] self.auswerten(nachricht) #Methode zum Auswerten der eingegangenen Nachricht def auswerten(self,nachricht): exec (nachricht) #Methode zum Weiterleiten, wird von Spiel genutzt def weiterleiten(self, nachricht): # HIER CODE EINFUEGEN, DER AUSGEFUEHRT WERDEN SOLL teile=nachricht.split(",") nachricht = "self.view.figur_setzen("+teile[0]+","+teile[1]+","+teile[3]+")" self.s.sendto(nachricht, ('255.255.255.255', self.PORT)) # versenden der Nachricht an Tupel(IP, PORT) """class Logik (): def pruefen (): if zahl == 6: anfangsfigur/ feldfigur/garagenfigur if pos(Spieler anderer Farbe)==neue eigene pos: eigene pos==frühere posindex + zahl pos(Spieler anderer Farbe)==anfang label (text = 'Du hast Spieler [] geschmissen.) label an Spieler []: text 'Du wurdest von Spieler {} geschmissen.' else: eigene pos==frühere posindex + zahl else: nur feldfigur/garagenfigur if pos(Spieler anderer Farbe)==neue eigene pos: eigene pos==frühere posindex + zahl pos(Spieler anderer Farbe)==anfang label (text = 'Du hast Spieler [] geschmissen.) label an Spieler []: text 'Du wurdest von Spieler {} geschmissen.' else: eigene pos==frühere posindex + zahl """ scene.stereodepth = 20 spiel=Spielbrett() w=Wuerfel(pos=(0.25,-0.5,5.25)) Steuerung(Spielbrett) z=Zufallsgenerator() z.wuerfeln() spiel.run() """ from math import sin, cos scene.userzoom = False # User darf zoomen? scene.userspin = False # User darf rotieren? scene.forward = (-1,-1,-1) # Kameraposition scene.autoscale = True # Autoskalierung aktiv? scene.range = 120 # Kameraabstand von scene.center scene.center = (0,0,0) # Kamerablickpunkt: norm. (0,0,0) scene.up = (0,1,0) # Rotationsachse Kamera: norm. (0,1,0) scene.background = color.white # Hintergrundfarbe scene.fov = 1 # Kamerawinkeloeffnung: norm. 1 scene.fullscreen = False # Vollbild? norm. False scene.title = "Spieler1" # Fenstertitel scene.width = 650 # Fensterbreite: norm. 200 scene.height = 650 # Fensterhoehe: norm 200 scene.x = 0 # x-Koordinate d. Fensters: norm. 0 scene.y = 0 # y-Koordinate d. Fensters: norm. 0 i=0.000001 while i<8: rate(50) scene.center=(sin(i)*300, 100, cos(i)*200) scene.forward = (sin(i)*300-300, -300.0/i, cos(i)*150-300) i=i+0.01 if __name__ == "__main__" : """