Links und Funktionen
Sprachumschaltung

Navigationspfad
Sie sind hier: Startseite / Probestudium / 2012 / WS I: 3D-Programmierung / Arbeitsgruppen / Schach / multiplayer


Inhaltsbereich

multiplayer

schach_multiplayer.py — text/python-source, 8 KB (8388 bytes)

Dateiinhalt

# -*- coding: utf-8 -*-
from visual import *
import socket
from Figuren import *
from threading import Thread

scene.userzoom=False
scene.userspin=False
scene.forward=(0,-1,-1)
scene.autoscale = False
scene.range = 62
scene.center = (40,0,40)
scene.up = (0,1,0)
scene.background = color.black
scene.fov = 1
scene.fullscreen = False
scene.title = "Koordinaten"
scene.width = 610
scene.height = 700
scene.x = 0
scene.y = 0
scene.lights = [distant_light(direction=(0.22, 0.44, 0.88), color=color.gray(0.8)), distant_light(direction=(-0.88, -0.22, -0.44), color=color.gray(0.3))]


pick=None
BauerW=[]
BauerS=[]
TurmW=[]
TurmS=[]
SpringerW=[]
SpringerS=[]
LaeuferW=[]
LaeuferS=[]
DameW=[]
DameS=[]
KoenigW=[]
KoenigS=[]
Sboxen=[]


pick=None

server=True #Für 2. Spieler auf False setzen
PORT = 8500 # EPORT und PORT für 2.Spieler tauschen
HOST = '141.84.220.101' # 141.84.220.90
EPORT = 8501

if server==False:
    scene.forward=(0,-1,1)
    scene.lights = [distant_light(direction=(0.22, 0.44, -0.88), color=color.gray(0.8)), distant_light(direction=(-0.88, -0.22, 0.44), color=color.gray(0.3))]

service= socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
service.bind(("", EPORT))
sender=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
print "Starte Server auf Port", EPORT


#FeldListe
class Spielfeld(frame):
    def __init__(self, pos=(0,0,0)):
        self.feld=0
        frame.__init__(self, pos=pos)
        self.feld = []
        self.groesse=10
        i=0
        farbe=True
        while i<8:
            e=0
            while e<8:
                if farbe:
                    color=(.8,.6,.5)
                    farbe=False
                else:
                    color=(.6,.4,.3)
                    farbe=True
                g = self.groesse + 0.0
                self.feld.append(box( pos= (g/2 +g*e,0,g/2 + g*i), height=.1, width=g, length=g, color=color, material=materials.wood, frame=self))
                e=e+1
            i=i+1
            if farbe:
                    farbe=False
            else:
                    farbe=True
    def get_pos(self, x, y): 
        return self.feld[x+y*8].pos + (0,.01,0)
    def gfeld(self,x,y):
        return self.feld[x+y*8]
    def get_x(self, objekt):
        f=0
        for i in self.feld:
            if objekt.pos==i.pos:
                a=f
                a=a%8
                return a
            f=f+1
    def get_y(self,objekt):
        f=0
        for i in self.feld:
            if objekt.pos==i.pos:
                a= f - self.get_x(objekt)
                a= a/8
                return a
            f=f+1
    def r_color(self, objekt):
        Farbe=False
        f=0
        for i in self.feld:
            if Farbe:
                Farbe=False
            else:
                Farbe = True
            if i==objekt:
                if Farbe:
                    objekt.color=(.8,.6,.5)
                else:
                    objekt.color=(.6,.4,.3)
            if (f+1)%8==0:
                if Farbe:
                    Farbe=False
                else:
                    Farbe = True
            f=f+1
    def check(self, objekt):
        a=True
        for i in BauerW:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        for i in BauerS:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        for i in TurmW:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        for i in TurmS:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        for i in LaeuferW:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        for i in LaeuferS:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        for i in SpringerW:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        for i in SpringerS:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        for i in DameW:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        for i in DameS:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        for i in KoenigW:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        for i in KoenigS:
            if i.pos==objekt.pos + (0,.01,0):
                a=False
                return i
        if a:
            return None
        
        

def spielfiguren(sfeld):
    i=0
         
    while i<8:
        BauerW.append(Bauer(pos=sfeld.get_pos(i,1)))
        i = i+1
    i=0
    while i<8:
        BauerS.append(Bauer2(pos=sfeld.get_pos(i,6)))
        i = i+1

    #Turm
    TurmW.append(Turm(pos=sfeld.get_pos(0,0)))
    TurmW.append(Turm(pos=sfeld.get_pos(7,0)))
    TurmS.append(Turm2(pos=sfeld.get_pos(0,7)))
    TurmS.append(Turm2(pos=sfeld.get_pos(7,7)))

    #Springer

    SpringerW.append(Pferdchen(pos=sfeld.get_pos(1,0)))
    SpringerW.append(Pferdchen(pos=sfeld.get_pos(6,0)))
    SpringerS.append(Pferdchen2(pos=sfeld.get_pos(1,7)))
    SpringerS.append(Pferdchen2(pos=sfeld.get_pos(6,7)))

    #Läufer

    LaeuferW.append(Laeufer(pos=sfeld.get_pos(2,0)))
    LaeuferW.append(Laeufer(pos=sfeld.get_pos(5,0)))
    LaeuferS.append(Laeufer2(pos=sfeld.get_pos(2,7)))
    LaeuferS.append(Laeufer2(pos=sfeld.get_pos(5,7)))

    #Dame

    DameW.append(Dame(pos=sfeld.get_pos(4,0)))
    DameS.append(Dame2(pos=sfeld.get_pos(4,7)))

    #Koenig

    KoenigW.append(Koenig(pos=sfeld.get_pos(3,0)))
    KoenigS.append(Koenig2(pos=sfeld.get_pos(3,7)))
                

def collisionSphere(obj1,obj2):
    m=mag(obj1.pos-obj2.pos)
    r=obj1.radius+obj2.radius
    if m<=r:
        return true
    else:
        return false

class Active(Thread):
    "Das Aktive Objekt handeln)"
    def __init__(self, pick):
        Thread.__init__(self)
        self.pick=pick
        self.active=None

        Thread.start(self)
    def run(self):
        while true:
            if self.active == None:
                self.active = self.pick
            else:
                self.active.color = color.white
                c = s.check(self.active)
                if c != None and self.pick != self.active and self.pick!=None:
                    if s.check(self.pick):
                        s.check(self.pick).pos=(-1,0,-1)
                    
                    sender.sendto(str(s.get_x(self.active)) + '-' + str(s.get_y(self.active))+'-'+str(s.get_x(self.pick))+'-'+str(s.get_y(self.pick)), (HOST,PORT))
                    print str(s.get_x(self.active)) + '-' + str(s.get_y(self.active))+'-'+str(s.get_x(self.pick))+'-'+str(s.get_y(self.pick)), (HOST,PORT)
                    c.pos=s.get_pos(s.get_x(self.pick),s.get_y(self.pick))
                    s.r_color(self.active)
                    self.pick=None
                if c==None:
                    self.pick=None
                if self.pick == None:
                    s.r_color(self.active)
                    self.active=self.pick

class empf(Thread):
    "Server-Client"
    def __init__(self):

        Thread.__init__(self)
        Thread.start(self)

    def run(self):
        while True:
            data, self.client = service.recvfrom(30)
            print "Verbindung von", self.client, "schreibt:", data
            text= data.split('-')
            i1 = int(text[0])
            i2 = int(text[1])
            i3 = int(text[2])
            i4 = int(text[3])
            if s.check(s.gfeld(i3,i4)):
                s.check(s.gfeld(i3,i4)).pos=(-1,0,-1)
            obj = s.gfeld(i1,i2)
            s.check(obj).pos=s.get_pos(i3,i4)

if __name__ == "__main__":
    s=Spielfeld()
    spielfiguren(s)
    thread = Active(pick)
    e = empf()

    
    
    while True:
        rate(50)
        if scene.mouse.events:
            m1 = scene.mouse.getevent()
            if m1.click=='left':
                a=True
                for i in s.feld:
                    if m1.pick == i:
                        thread.pick=i
                        a=False
                        print pick
                if a:
                    pick=None
                    print pick
        

                        

Funktionsleiste