Dame2
dame2.py — Python Source, 5 KB (5763 bytes)
Dateiinhalt
from visual import * import time import math from threading import Thread class Spielbrettstein: def __init__ (self,farbe = 1,position = (0,0,0),isSet = None): self.stein = box(pos = position,height = 0.2,length = 1,width = 1) self.farbe = farbe if farbe == 1: self.stein.color = color.black else: self.stein.color = color.white self.pObject = isSet class Spielbrett: def __init__(self): self.brett = [] for i in range(0,64): self.brett.append(Spielbrettstein(farbe = (i+(i/8))%2,position = (i%8,0,-(i/8)))) #time.sleep(1) def FeldSetzen(self,x,y,Object): self.brett[int(x+abs(y)*8)].pObject = Object #print(brett[x+abs(y)*8] class Spielstein(frame): def __init__(self,farbe = 1,position = (0,0,0)): frame.__init__(self) self.stein = cylinder(frame = self,pos = position,radius = 0.5,length = 0.4,axis = (0,0.4,0),material = materials.wood) self.farbe = farbe self.lastPos = position if farbe == 1: self.stein.color = (1,1,1) else: self.stein.color = (0.35,0.35,0.35) self.istDame = False def checkifValid(self, spiel): newpos = (round(self.stein.pos[0],0),0,round(self.stein.pos[2],0)) if newpos[0]<0 or newpos[0] > 7 or newpos[2]>0 or newpos[2] < -7 or ((newpos[0]+ newpos[2])%2 == 0): newpos = self.lastPos else: if not self.istDame: if self.farbe == 1: if self.lastPos[2] <= newpos[2]: newpos = self.lastPos else: if self.lastPos[2]>=newpos[2]: newpos = self.lastPos if abs(newpos[0] - self.lastPos[0]) > 2 or abs(newpos[2] - self.lastPos[2]) > 2: newpos = self.lastPos elif (abs(newpos[0] - self.lastPos[0]) == 2 and abs(newpos[2] - self.lastPos[2]) == 0) or (abs(newpos[2] - self.lastPos[2]) == 2 and abs(newpos[0] - self.lastPos[0]) == 0): newpos = self.lastPos elif abs(newpos[0] - self.lastPos[0]) == 2 or abs(newpos[2] - self.lastPos[2]) == 2: if spiel.brett.brett[int(newpos[0]+abs(newpos[2])*8)].pObject: newpos = self.lastPos elif spiel.brett.brett[int((newpos[0]+(self.lastPos[0] - newpos[0])/2)+abs((newpos[2]+(self.lastPos[2] - newpos[2])/2)*8))].pObject: steinImWeg = spiel.brett.brett[int((newpos[0]+(self.lastPos[0] - newpos[0])/2)+abs((newpos[2]+(self.lastPos[2] - newpos[2])/2)*8))].pObject if steinImWeg.farbe == self.farbe: newpos = self.lastPos else: steinImWeg.Wegnehmen() spiel.brett.FeldSetzen((newpos[0]+(self.lastPos[0] - newpos[0])/2),(newpos[2]+(self.lastPos[2] - newpos[2])/2), None) spiel.brett.brett[int(newpos[0]+abs(newpos[2])*8)].pObject = self spiel.brett.FeldSetzen(self.lastPos[0], self.lastPos[2], None) #eventuell Zug weitergeben else: #print(newpos) if spiel.brett.brett[int(newpos[0]+abs(newpos[2])*8)].pObject: newpos = self.lastPos else: spiel.brett.brett[int(newpos[0]+abs(newpos[2])*8)].pObject = self spiel.brett.FeldSetzen(self.lastPos[0], self.lastPos[2], None) self.stein.pos = newpos self.lastPos = newpos def ZurDameMachen(self): if self.istDame == False: self.stein.length = self.stein.length * 2 self.istDame = True def Wegnehmen(self): self.stein.visible = False class Spiel: def __init__(self): scene.background = (0.2,0.5,0.7) scene.center = (3.5,0,0) #scene.stereo = "active" self.brett = Spielbrett() self.steine = [] for i in range(12): temp = Spielstein(farbe = 1,position = (((i*2)+i/4+1)%8,0,-(i/4))) self.steine.append(temp) self.brett.FeldSetzen(((i*2)+i/4+1)%8,-(i/4),temp) #time.sleep(1) #watch it!!!!! for i in range(12): temp = Spielstein(farbe = 0,position = (((i*2)+i/4)%8,0,(i/4)-7)) self.steine.append(temp) self.brett.FeldSetzen(((i*2)+i/4)%8,(i/4)-7,temp) #time.sleep(1) def IstStein(self,Object): for i in self.steine: if Object == i.stein: return i return None class Spiellogik(Thread): def __init__(self): Thread.__init__(self) self.spiel = None def run(self): self.spiel = Spiel() pick = None while True: if scene.mouse.events: currentevent = scene.mouse.getevent() if currentevent.drag and self.spiel.IstStein(currentevent.pick): drag_pos = currentevent.pickpos pick = currentevent.pick elif currentevent.drop: if self.spiel.IstStein(pick): self.spiel.IstStein(pick).checkifValid(self.spiel) pick = None if pick: new_pos = scene.mouse.project(normal=(0,1,0)) if new_pos != drag_pos: pick.pos += new_pos - (drag_pos[0],0,drag_pos[2]) drag_pos = new_pos time.sleep(0.02) x = Spiellogik() x.start()