3dpong_0.2_src
3dpong src, all rights reserved
3dpong.py — text/python-source, 2 KB (2948 bytes)
Dateiinhalt
import visual as vP import random class PlayerError(Exception): pass class Vector: def __init__(self,x,y=None,z=None): if y == None: y = x if z == None: z = y self.x = x; self.y = y; self.z = z def __setattr__(self,name,value): self.__dict__[name] = float(value) def __add__(self,other): return Vector(self.x+other.x,self.y+other.y,self.z+other.z) def __sub__(self,other): return Vector(self.x-other.x,self.y-other.y,self.z-other.z) def __mul__(self,other): return Vector(self.x*other.x,self.y*other.y,self.z*other.z) def __truediv__(self,other): return Vector(self.x/other.x,self.y/other.y,self.z/other.z) def get(self): return (self.x,self.y,self.z) class WAND: def __init__(self,seite,spieler=None): self.seite = seite self.spieler = spieler def SpielerSetzen(self,spieler): if not isinstance(spieler,SPIELER): raise PlayerError, "overload a player please" self.spieler = spieler class SPIELER: def __init__(self): pass class SPIELFELD: def __VPVecToTuple(self,vec): """ Converts a vPython Vector to a tuple""" return vec.x,vec.y,vec.z def __init__(self,anzahl): if 0 > anzahl > 5: raise PlayerError, "min 0 and max 6 players allowed" self.anzahl = anzahl self.size = Vector(200) self.symbol = vP.box(pos=(0,0,0),opacity=0.2,width=200,height=200,length=200) self.waende = {} for i in range(anzahl): self.waende[i] = WAND(i,SPIELER()) for i in range(anzahl,6): self.waende[i] = WAND(i) self.ball = vP.sphere(radius=10,pos=(0,0,0)) self.ball_geschw= Vector(*[random.choice([-5,5,-1,1]) for i in range(3)]) def run(self): while True: vP.rate(50) x,y,z = (self.ball_geschw+Vector(*self.__VPVecToTuple(self.ball.pos))).get() vx,vy,vz = self.ball_geschw.get() beg = (-100,100) pos = 100 neg = -100 if x < beg[0]-5: vx *= -1 x = beg[0] elif x > beg[1]+5: vx *= -1 x = beg[1] if y < beg[0]-5: vy *= -1 y = beg[0] elif y > beg[1]+5: vy *= -1 y = beg[1] if z < beg[0]-5: vz *= -1 z = beg[0] elif z > beg[1]+5: vz *= -1 z = beg[1] self.ball_geschw = Vector(vx,vy,vz) self.ball.pos = (x+2,y+2,z+2) def main(): feld = SPIELFELD(2) feld.run() main()