Mulitplayer 0.1 Spieler 1
nur Spieler 1, mit Clear, 5Senden
Projekt3D4G-MultiplayerS1-0.1.py — text/python-source, 21 KB (21888 bytes)
Dateiinhalt
# -*- coding: utf-8 -*-
from visual import *
from random import random
from threading import Thread # Modul f. Nebenlaeufigkeit
import socket
scene.width=600
scene.height=600
scene.title="3D 4 in a row"
# Koordinatenachsen in Form von Pfeilen
#xAchse = arrow(axis=(10,0,0), shaftwidth=0.05, color=color.red)
#yAchse = arrow(axis=(0,10,0), shaftwidth=0.05, color=(0,0,1))
#zAchse = arrow(axis=(0,0,10), shaftwidth=0.05, color=color.green)
# Beschriftungen
#xLabel = label(pos=(10,0,0), text="x-Achse")
#yLabel = label(pos=(0,10,0), text="y-Achse")
#zLabel = label(pos=(0,0,10), text="z-Achse")
# Portnummer und Hostadresse des Fremdservers: bei Bedarf aendern
FremdPORT=9000 # Port des Fremdservers
HOST="141.84.220.82" # Adresse des Gegenueber
EigenPORT=9001 # Port des eigenen Servers
#Button
f=frame()
cbox = box (pos=(0,7,0),color=color.white)
unbox = box (frame=f,pos=(0,5,0))
cLabel = label(pos=cbox.pos, text="Clear")
unLabel = label(frame=f,pos=unbox.pos, text="NoClear")
for obj in f.objects:
obj.visible=False
#Spieler1
rwin = label(pos=(2.5,2.5,2.5),text="Rot Gewinnt",color=color.red,height=100)
rwin.visible=False
#Spieler2
bwin = label(pos=(2.5,2.5,2.5),text="Blau Gewinnt",color=color.blue,height=100)
bwin.visible=False
matrix = [[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],
[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],
[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],
[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]]
#Blöcke werden generiert
block = []
kugel = []
for i in range(4):
for j in range(4):
for k in range(4):
block.append(box(pos=(i,j,k),opacity=0.05))
if i>2:
block[-1].material=materials.earth
block[-1].opacity=0.2
#Spieler 1 fängt an
p = 1
wi=True
#Umrechnung von i in Koordinate Z
def getZ(i):
z=0
for r in range(i):
if z==3:
z=0
else:
z=z+1
return z
#Umrechnung von i in Koordinate Y
def getY(i):
y=0
s=0
for r in range(i):
if s==3:
s=0
if y==3:
y=0
else:
y=y+1
else:
s=s+1
return y
#Abfangen der vollen Matrix
def max(s):
if s>3:
return False
elif s<0:
return False
else:
return True
#Gewinn
def winn(x,y,z,p):
g=True
r=p
w=1
#1nach "hinten"
if (max(x-1)):
if matrix[x-1][y][z]==r:
w=w+1
if (max(x-2)):
if matrix[x-2][y][z]==r:
w=w+1
if (max(x-3)):
if matrix[x-3][y][z]==r:
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#2nach oben/unten
w=1
if(max(y-1)):
if matrix[x][y-1][z]==r:
w=w+1
if (max(y-2)):
if matrix[x][y-2][z]==r:
w=w+1
if (max(y-3)):
if matrix[x][y-3][z]==r:
w=w+1
if(max(y+1)):
if matrix[x][y+1][z]==r:
w=w+1
if (max(y+2)):
if matrix[x][y+2][z]==r:
w=w+1
if (max(y+3)):
if matrix[x][y+3][z]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#3nach links/rechts
w=1
if(max(z-1)):
if matrix[x][y][z-1]==r:
w=w+1
if (max(z-2)):
if matrix[x][y][z-2]==r:
w=w+1
if (max(z-3)):
if matrix[x][y][z-3]==r:
w=w+1
if(max(z+1)):
if matrix[x][y][z+1]==r:
w=w+1
if (max(z+2)):
if matrix[x][y][z+2]==r:
w=w+1
if (max(z+3)):
if matrix[x][y][z+3]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#4von hinten links nach vorne rechts
w=1
if(max(x+1))and (max(z-1)):
if matrix[x+1][y][z-1]==r:
w=w+1
if (max(x+2))and (max(z-2)):
if matrix[x+2][y][z-2]==r:
w=w+1
if (max(x+3))and (max(z-3)):
if matrix[x+3][y][z-3]==r:
w=w+1
if(max(x-1))and (max(z+1)):
if matrix[x-1][y][z+1]==r:
w=w+1
if (max(x-2))and (max(z+2)):
if matrix[x-2][y][z+2]==r:
w=w+1
if (max(x-3))and (max(z+3)):
if matrix[x-3][y][z+3]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#5von hinten links nach vorne rechts
w=1
if(max(x+1))and (max(z-1)):
if matrix[x+1][y][z-1]==r:
w=w+1
if (max(x+2))and (max(z-2)):
if matrix[x+2][y][z-2]==r:
w=w+1
if (max(x+3))and (max(z-3)):
if matrix[x+3][y][z-3]==r:
w=w+1
if(max(x-1))and (max(z+1)):
if matrix[x-1][y][z+1]==r:
w=w+1
if (max(x-2))and (max(z+2)):
if matrix[x-2][y][z+2]==r:
w=w+1
if (max(x-3))and (max(z+3)):
if matrix[x-3][y][z+3]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#6von hinten rechts nach vorne links
w=1
if(max(x+1))and (max(z+1)):
if matrix[x+1][y][z+1]==r:
w=w+1
if (max(x+2))and (max(z+2)):
if matrix[x+2][y][z+2]==r:
w=w+1
if (max(x+3))and (max(z+3)):
if matrix[x+3][y][z+3]==r:
w=w+1
if(max(x-1))and (max(z-1)):
if matrix[x-1][y][z-1]==r:
w=w+1
if (max(x-2))and (max(z-2)):
if matrix[x-2][y][z-2]==r:
w=w+1
if (max(x-3))and (max(z-3)):
if matrix[x-3][y][z-3]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#7von links unten nach recht oben
w=1
if(max(z-1))and (max(y+1)):
if matrix[x][y+1][z-1]==r:
w=w+1
if (max(z-2))and (max(y+2)):
if matrix[x][y+2][z-2]==r:
w=w+1
if (max(z-3))and (max(y+3)):
if matrix[x][y+3][z-3]==r:
w=w+1
if(max(z+1))and (max(y-1)):
if matrix[x][y-1][z+1]==r:
w=w+1
if (max(z+2))and (max(y-2)):
if matrix[x][y-2][z+2]==r:
w=w+1
if (max(z+3))and (max(y-3)):
if matrix[x][y-3][z+3]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#8von hinten unten nach vorne oben
w=1
if(max(x+1))and (max(y+1)):
if matrix[x+1][y+1][z]==r:
w=w+1
if (max(x+2))and (max(y+2)):
if matrix[x+2][y+2][z]==r:
w=w+1
if (max(x+3))and (max(y+3)):
if matrix[x+3][y+3][z]==r:
w=w+1
if(max(x-1))and (max(y-1)):
if matrix[x-1][y-1][z]==r:
w=w+1
if (max(x-2))and (max(y-2)):
if matrix[x-2][y-2][z]==r:
w=w+1
if (max(x-3))and (max(y-3)):
if matrix[x-3][y-3][z]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#9von hinten oben nach vorne unten
w=1
if(max(x-1))and (max(y+1)):
if matrix[x-1][y+1][z]==r:
w=w+1
if (max(x-2))and (max(y+2)):
if matrix[x-2][y+2][z]==r:
w=w+1
if (max(x-3))and (max(y+3)):
if matrix[x-3][y+3][z]==r:
w=w+1
if(max(x+1))and (max(y-1)):
if matrix[x+1][y-1][z]==r:
w=w+1
if (max(x+2))and (max(y-2)):
if matrix[x+2][y-2][z]==r:
w=w+1
if (max(x+3))and (max(y-3)):
if matrix[x+3][y-3][z]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#10von hinten rechts unten nach vorne links oben
w=1
if(max(x+1))and (max(y+1)) and(max(z+1)):
if matrix[x+1][y+1][z+1]==r:
w=w+1
if (max(x+2))and (max(y+2))and(max(z+2)):
if matrix[x+2][y+2][z+2]==r:
w=w+1
if (max(x+3))and (max(y+3))and(max(z+3)):
if matrix[x++3][y+3][z+3]==r:
w=w+1
if(max(x-1))and (max(y-1))and(max(z-1)):
if matrix[x-1][y-1][z-1]==r:
w=w+1
if (max(x-2))and (max(y-2))and(max(z-2)):
if matrix[x-2][y-2][z-2]==r:
w=w+1
if (max(x-3))and (max(y-3))and(max(z-3)):
if matrix[x-3][y-3][z-3]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#11von hinten links unten nach vorne rechts oben
w=1
if(max(x+1))and (max(y+1)) and(max(z-1)):
if matrix[x+1][y+1][z-1]==r:
w=w+1
if (max(x+2))and (max(y+2))and(max(z-2)):
if matrix[x+2][y+2][z-2]==r:
w=w+1
if (max(x+3))and (max(y+3))and(max(z-3)):
if matrix[x+3][y+3][z-3]==r:
w=w+1
if(max(x-1))and (max(y-1))and(max(z+1)):
if matrix[x-1][y-1][z+1]==r:
w=w+1
if (max(x-2))and (max(y-2))and(max(z+2)):
if matrix[x-2][y-2][z+2]==r:
w=w+1
if (max(x-3))and (max(y-3))and(max(z+3)):
if matrix[x-3][y-3][z+3]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#12von hinten links oben nach vorne rechts unten
w=1
if(max(x+1))and (max(y-1)) and(max(z-1)):
if matrix[x+1][y-1][z-1]==r:
w=w+1
if (max(x+2))and (max(y-2))and(max(z-2)):
if matrix[x+2][y-2][z-2]==r:
w=w+1
if (max(x+3))and (max(y-3))and(max(z-3)):
if matrix[x+3][y-3][z-3]==r:
w=w+1
if(max(x-1))and (max(y+1))and(max(z+1)):
if matrix[x-1][y+1][z+1]==r:
w=w+1
if (max(x-2))and (max(y+2))and(max(z+2)):
if matrix[x-2][y+2][z+2]==r:
w=w+1
if (max(x-3))and (max(y+3))and(max(z+3)):
if matrix[x-3][y+3][z+3]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
#13von hinten rechts oben nach vorne links unten
w=1
if(max(x+1))and (max(y-1)) and(max(z+1)):
if matrix[x+1][y-1][z+1]==r:
w=w+1
if (max(x+2))and (max(y-2))and(max(z+2)):
if matrix[x+2][y-2][z+2]==r:
w=w+1
if (max(x+3))and (max(y-3))and(max(z+3)):
if matrix[x+3][y-3][z+3]==r:
w=w+1
if(max(x-1))and (max(y+1))and(max(z-1)):
if matrix[x-1][y+1][z-1]==r:
w=w+1
if (max(x-2))and (max(y+2))and(max(z-2)):
if matrix[x-2][y+2][z-2]==r:
w=w+1
if (max(x-3))and (max(y+3))and(max(z-3)):
if matrix[x-3][y+3][z-3]==r:
w=w+1
if(w==4):
print "Spieler",r,"hat gewonnen"
if r==1:
rwin.visible=True
elif r==2:
bwin.visible=True
else:
print "Spiernummerfehler"
g=False
return g
def anKlick():
#bereits angeklickt
while len(kugel):
kugel[-1].visible=False
del(kugel[-1])
for i in range(4):
for j in range(4):
for k in range(4):
matrix[i][j][k]=0
unbox.visible=False
unLabel.visible=False
cbox.color=color.white
cLabel.text="Clear"
rwin.visible=False
bwin.visible=False
return False
p=1
def noneKlick():
#noch nicht angeklickt
cbox.color=color.red
cLabel.text="Are you sure?"
unbox.visible=True
unLabel.visible=True
return True
def abKlick():
#Abbruch Clear
cbox.color=color.white
cLabel.text="Clear"
print "Clear undone"
unbox.visible=False
unLabel.visible=False
return True
def clear(sme):
sme=scene.mouse.getclick()
if sme.pick==cbox:
if cbox.color==color.red:
anKlick()
f.sendto("anKlick()",(HOST, FremdPORT))
else:
noneKlick()
f.sendto("noneKlick()",(HOST, FremdPORT))
elif sme.pick==unbox:
abKlick
f.sendto("abKlick()",(HOST, FremdPORT))
else:
return True
#Client/Server
# UDP-Socket fuer Clientfunktion
f = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# UDP-Socket fuer Eigenserver
e = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
e.bind(("",EigenPORT))
def do(wi,p):
while wi:
wi=True
rate(50)
try: # Versuch, die Nachricht vom Gegenueber umzusetzen
info = e.recvfrom(100)[0]
exec(info)
if l:
for x in range (3):
l.pop
print "x"
except:
print "Fehler beim Ausfuehren von Serveranweisung"
if scene.mouse.events:
sme=scene.mouse.getclick()
if sme.pick==cbox or sme.pick==unbox:
clear(sme)
for i in range(16):
if sme.pick==block[i+48]:
if p==1:
#Spieler 1
if matrix[0][getY(i)][getZ(i)]==0:
kugel.append(sphere(pos=(block[i].pos),radius=0.5,color=color.red))
matrix[0][getY(i)][getZ(i)]=1
wi=winn(0,getY(i),getZ(i),p)
a= "kugel.append(sphere(pos=(block[%s].pos),radius=0.5,color=color.red))"%i # Versand der Anweisung an Gegenueber
b= "matrix[0][getY(%s)][getZ(%s)]=1"%(i,i)
c= "wi=winn(0,getY(%s),getZ(%s),%s)"%(i,i,p)
elif matrix[1][getY(i)][getZ(i)]==0:
kugel.append(sphere(pos= (block[i+16].pos),radius=0.5,color=color.red))
matrix[1][getY(i)][getZ(i)]=1
wi=winn(1,getY(i),getZ(i),p)
a= "kugel.append(sphere(pos=(block[%s].pos),radius=0.5,color=color.red))"%i # Versand der Anweisung an Gegenueber
b= "matrix[1][getY(%s)][getZ(%s)]=1"%(i,i)
c= "wi=winn(1,getY(%s),getZ(%s),%s)"%(i,i,p)
elif matrix[2][getY(i)][getZ(i)]==0:
kugel.append(sphere(pos= (block[i+32].pos),radius=0.5,color=color.red))
matrix[2][getY(i)][getZ(i)]=1
wi=winn(2,getY(i),getZ(i),p)
a= "kugel.append(sphere(pos=(block[%s].pos),radius=0.5,color=color.red))"%i # Versand der Anweisung an Gegenueber
b= "matrix[2][getY(%s)][getZ(%s)]=1"%(i,i)
c= "wi=winn(2,getY(%s),getZ(%s),%s)"%(i,i,p)
elif matrix[3][getY(i)][getZ(i)]==0:
kugel.append(sphere(pos= (block[i+48].pos),radius=0.5,color=color.red))
matrix[3][getY(i)][getZ(i)]=1
wi=winn(3,getY(i),getZ(i),p)
a= "kugel.append(sphere(pos=(block[%s].pos),radius=0.5,color=color.red))"%i # Versand der Anweisung an Gegenueber
b= "matrix[3][getY(%s)][getZ(%s)]=1"%(i,i)
c= "wi=winn(3,getY(%s),getZ(%s),%s)"%(i,i,p)
elif matrix[3][getY(i)][getZ(i)]!=0:
print "voll"
p=5
if wi==True:
if p!=5:
p=2
print "Spieler",p,"ist dran"
else:
print "bitte anders setzen"
p=1
try:
f.sendto("l["+c+","+b+","+a+"]",(HOST, FremdPORT))
# a,(HOST, FremdPORT)
# b,(HOST, FremdPORT)
# c,(HOST, FremdPORT) # Versand der Anweisung an Gegenueber
except:
f.sendto("print 1")
else:
#Spieler 2
if matrix[0][getY(i)][getZ(i)]==0:
kugel.append(sphere(pos= (block[i].pos),radius=0.5,color=color.blue))
matrix[0][getY(i)][getZ(i)]=2
wi=winn(0,getY(i),getZ(i),p)
elif matrix[1][getY(i)][getZ(i)]==0:
kugel.append(sphere(pos= (block[i+16].pos),radius=0.5,color=color.blue))
matrix[1][getY(i)][getZ(i)]=2
wi=winn(1,getY(i),getZ(i),p)
elif matrix[2][getY(i)][getZ(i)]==0:
kugel.append(sphere(pos= (block[i+32].pos),radius=0.5,color=color.blue))
matrix[2][getY(i)][getZ(i)]=2
wi=winn(2,getY(i),getZ(i),p)
elif matrix[3][getY(i)][getZ(i)]==0:
kugel.append(sphere(pos= (block[i+48].pos),radius=0.5,color=color.blue))
matrix[3][getY(i)][getZ(i)]=2
wi=winn(3,getY(i),getZ(i),p)
elif matrix[3][getY(i)][getZ(i)]!=0:
print "voll"
p=5
if wi==True:
if p!=5:
#nicht voll
p=1
print "Spieler",p,"ist dran"
else:
#voll
print "bitte anders setzen"
p=2
f.sendto("print 5",(HOST, FremdPORT))
#o = e.recvfrom(100)[0]
#exec(o)
do(wi,p)
while True:
rate(1)
if scene.mouse.events:
sme=scene.mouse.getclick()
if clear(sme)==True:
wi=False
else:
wi=True
do(wi,p)