def __init__(self,sx,sy, level): self.level=level self.current_state["standing"]=True self.current_state["crouching"]=False self.current_state["walking"]=False self.current_state["jumping"]=False self.current_state["wandswish"]=False self.current_state["falling"]=False self.x=sx self.y=sy self.startx=sx self.starty=sy self.AG_walk=ActionGroup() self.AG_walk.append("wobble",Action(func=self.wobble,max=5,cycle=True,min=-5,reverseloop=True,init_tick=0)) self.AG_walk.append("footR",Action(func=self.footR,max=13,cycle=True,min=0)) self.AG_walk.append("footL",Action(func=self.footL,max=13,cycle=True,min=0,init_tick=6)) self.AG_jump=ActionGroup() self.AG_jump.append("jump_displacement",Action(func=self.jump_displacement,max=10,min=0)) self.AG_jump.action("jump_displacement").callback=self.end_jump self.AG_walk.speed_scale(2) self.A_wandswish=Action(func=self.swish,min=-7,max=-1,cycle=False,reverseloop=False,init_tick=-7)
def __init__(self,x,y): self.x=x self.y=y self.draw_func=self.temp_drawfunc self.AG_move=ActionGroup() self.AG_move.append("x_action",Action(func=self.x_action,max=100,cycle=True,min=0)) self.AG_move.append("y_action",Action(func=self.y_action,max=100,cycle=True,min=0)) self.AG_move.append("z_action",Action(func=self.z_action,max=100,cycle=True,min=0)) self.AG_move.append("xrot_action",Action(func=self.xrot_action,max=360,cycle=True,min=0)) self.AG_move.append("yrot_action",Action(func=self.yrot_action,max=360,cycle=True,min=0)) self.AG_move.append("zrot_action",Action(func=self.zrot_action,max=360,cycle=True,min=0))
def __init__(self, x, y): self.x = x self.y = y self.draw_func = self.temp_drawfunc self.AG_move = ActionGroup() self.AG_move.append( "x_action", Action(func=self.x_action, max=100, cycle=True, min=0)) self.AG_move.append( "y_action", Action(func=self.y_action, max=100, cycle=True, min=0)) self.AG_move.append( "z_action", Action(func=self.z_action, max=100, cycle=True, min=0)) self.AG_move.append( "xrot_action", Action(func=self.xrot_action, max=360, cycle=True, min=0)) self.AG_move.append( "yrot_action", Action(func=self.yrot_action, max=360, cycle=True, min=0)) self.AG_move.append( "zrot_action", Action(func=self.zrot_action, max=360, cycle=True, min=0))
def __init__(self,griddata): griddata.reverse() #self.grid=griddata self.grid=[] for line in griddata: self.grid.append(list(line)) rr=0 for r in self.grid: cc=0 for c in r: if c=="@": self.solomon=Solomon(cc,rr,self) self.grid[rr][cc]="." self.solomon.A_wandswish.callback=self.block_swap elif c=="4": self.door=[cc,rr] self.grid[rr][cc]="." elif c=="k": ns=Sprite(cc,rr+0.5) ns.setDrawFuncToList(lists["green_key"]) ns.collision_action=self.key_detected_something_test self.sprites.append(ns) self.grid[rr][cc]="." elif not c in ["b","B","s"]: self.grid[rr][cc]="." cc+=1 rr+=1 self.AG_twinklers=ActionGroup() self.AG_twinklers.append("twinkle1",Action(func=self.sin_gen_1,max=200,cycle=True,min=0,reverseloop=False,init_tick=0)) self.AG_twinklers.append("twinkle2",Action(func=self.sin_gen_2,max=100,cycle=True,min=0,reverseloop=False,init_tick=10))
def __init__(self,griddata): griddata.reverse() #self.grid=griddata self.grid=[] for line in griddata: self.grid.append(list(line)) rr=0 for r in self.grid: cc=0 for c in r: if c=="@": self.solomon=Solomon(cc,rr,self) self.grid[rr][cc]="." self.solomon.A_wandswish.callback=self.block_swap elif c=="k": ns=Sprite(cc,rr) ns.setDrawFuncToList(lists["green_key"]) ns.collision_action=key_detected_something_test self.sprites.append(ns) self.grid[rr][cc]="." elif not c in ["b","B","s"]: self.grid[rr][cc]="." cc+=1 rr+=1 self.AG_twinklers=ActionGroup() self.AG_twinklers.append("twinkle1",Action(func=self.sin_gen_1,max=200,cycle=True,min=0,reverseloop=False,init_tick=0)) self.AG_twinklers.append("twinkle2",Action(func=self.sin_gen_2,max=100,cycle=True,min=0,reverseloop=False,init_tick=10))
class Level: grid=None baddies=[] solomon=None sprites=[] AG_twinklers=None def sin_gen_1(self,tvsl): t,v,s,l=tvsl v=0.5*(1+sin(2*pi*t/(l))) #print (t,v) return v def sin_gen_2(self,tvsl): t,v,s,l=tvsl v=0.5*(1+sin(4*pi*t/(l))) #print (t,v) return v def __init__(self,griddata): griddata.reverse() #self.grid=griddata self.grid=[] for line in griddata: self.grid.append(list(line)) rr=0 for r in self.grid: cc=0 for c in r: if c=="@": self.solomon=Solomon(cc,rr,self) self.grid[rr][cc]="." self.solomon.A_wandswish.callback=self.block_swap elif c=="k": ns=Sprite(cc,rr) ns.setDrawFuncToList(lists["green_key"]) ns.collision_action=key_detected_something_test self.sprites.append(ns) self.grid[rr][cc]="." elif not c in ["b","B","s"]: self.grid[rr][cc]="." cc+=1 rr+=1 self.AG_twinklers=ActionGroup() self.AG_twinklers.append("twinkle1",Action(func=self.sin_gen_1,max=200,cycle=True,min=0,reverseloop=False,init_tick=0)) self.AG_twinklers.append("twinkle2",Action(func=self.sin_gen_2,max=100,cycle=True,min=0,reverseloop=False,init_tick=10)) def block_swap(self): print("hi"+str(self.solomon)) takeoff_for_crouching=0 if self.solomon.state_test(["crouching"])>0 : takeoff_for_crouching=-0.8 res=self.detect(val(self.solomon.x,self.solomon.facing*1.0),val(self.solomon.y,takeoff_for_crouching),collision_bound=0.5) if res=="OK": return ch,xx,yy,dist=res[0] if ch=="b": #print("break block") #self.grid[yy][xx]="B" print("destroy block") self.grid[yy][xx]="." elif ch=="B": print("destroy block") self.grid[yy][xx]="." elif ch==".": print("create") self.grid[yy][xx]="b" def detect(self,xx,yy,collision_bound=None,callback=None,ignoreDots=False,ignoreTheseSprites=[]): """ return "OK" message in test of a tuple of (character detected,x of char,y of char,distance float """ if collision_bound==None: collision_bound=self.solomon.bound detection=[] for rr in range(int(floor(yy-collision_bound+0.5)),int(ceil(yy+collision_bound+0.5))): #didhave +1 list1="" for cc in range(int(floor(xx-collision_bound+0.5)),int(ceil(xx+collision_bound+0.5))): c=self.grid[rr][cc] if not (c=="." and ignoreDots==True): test=(cc-xx)**2+(rr-yy)**2 list1+=c if test<(collision_bound)**2: detection.append((c,cc,rr,sqrt(test))) #print list1 for s in self.sprites: if not s in ignoreTheseSprites: test=(s.x-xx)**2+(s.y-yy)**2 if test<(collision_bound)**2: detection.append((s,s.x,s.y,sqrt(test))) detection=sorted(detection,key=lambda x: x[3]) if not callback==None: callback(detection) return detection def evaluate(self,joystick,keys): self.solomon.stickers=[] self.solomon.stickers.append([0,0,0,"white"]) """ TODO redo current_state was rubbish anyway """ self.AG_twinklers.do() jumpnow=False walkcheck=False if self.solomon.A_wandswish.overide==False: self.solomon.current_state["wandswish"]=False if joystick.isDown(keys)==True: self.solomon.current_state["crouching"]=True self.solomon.current_state["standing"]=False else: self.solomon.current_state["crouching"]=False if joystick.isRight(keys)==True: self.solomon.facing=1 self.solomon.current_state["walking"]=True self.solomon.current_state["standing"]=False walkcheck=True elif joystick.isLeft(keys)==True: self.solomon.facing=-1 self.solomon.current_state["walking"]=True self.solomon.current_state["standing"]=False walkcheck=True else: self.solomon.current_state["walking"]=False self.solomon.current_state["standing"]=True if walkcheck==True: if joystick.isUp(keys)==True and self.solomon.current_state["jumping"]==False: self.solomon.current_state["jumping"]=True jumpnow=True else: self.solomon.current_state["jumping"]=False xcheck_p=val(self.solomon.x+0.5,self.solomon.step*2*self.solomon.facing) xcheck_m=val(self.solomon.x+0.5,-self.solomon.step*2*self.solomon.facing) #self.solomon.stickers.append([xcheck_p,self.solomon.y-self.solomon.step,0,"white"]) #self.solomon.stickers.append([xcheck_m,self.solomon.y-self.solomon.step,0,"white"]) #self.solomon.stickers.append([xcheck_p,self.solomon.y-self.solomon.step,0,"white"]) #self.solomon.stickers.append([xcheck_m,self.solomon.y-self.solomon.step,0,"white"]) width=1.5 canwalk=False if walkcheck: col="green" if self.solomon.facing==-1: col="yellow" self.solomon.stickers.append([self.solomon.facing*self.solomon.step*width,0,0,col]) result=self.detect(val(self.solomon.x,self.solomon.facing*self.solomon.step*width),self.solomon.y) if (len(result)==0 or result[0][0]==".") and self.solomon.current_state["walking"]==True: #self.solomon.x+=self.solomon.step*self.solomon.facing self.solomon.current_state["standing"]=False self.solomon.current_state["walking"]=True canwalk=True #elif result[0][0] in ["] result1=self.grid[int(self.solomon.y-self.solomon.step)][int(xcheck_p)] result2=self.grid[int(self.solomon.y-self.solomon.step)][int(xcheck_m)] print("fall check" + str((result1,result2,self.solomon.x,self.solomon.y))) if result1=="." and result2==".": self.solomon.y_change(-self.solomon.step) print(("solomon y affected by", -self.solomon.step)) self.solomon.current_state["falling"]=True #canwalk=False else: self.solomon.current_state["falling"]=False if canwalk==True: print(("self.solomon.x before",self.solomon.x)) self.solomon.x_change(self.solomon.step*self.solomon.facing) print(("self.solomon.x after",self.solomon.x)) if joystick.isFire(keys)==True and self.solomon.current_state["wandswish"]==False: self.solomon.A_wandswish.kick() self.solomon.A_wandswish.overide=True self.solomon.current_state["wandswish"]=True if self.solomon.current_state["jumping"]==True: isWalk=0 if self.solomon.current_state["walking"]==True: isWalk=1 result=self.detect(self.solomon.x+(self.solomon.facing*self.solomon.step*5.0)*(isWalk),self.solomon.y) self.solomon.current_state["falling"]=False print(("jumping",result)) self.solomon.AG_jump.do() print("he's jumping") print(str(self.solomon.AG_jump.action("jump_displacement").tick )) print(str(self.solomon.AG_jump.action("jump_displacement").value )) self.solomon.y+=0.2 print(("solomon y affected by", +0.2)) #print "co-ordinates "+str((self.solomon.x,self.solomon.y)) if joystick.isUp(keys)==True and self.solomon.current_state["jumping"]==False: self.solomon.current_state["jumping"]=True self.solomon.AG_jump.kick() if self.solomon.current_state["walking"]==True: self.solomon.AG_walk.do() def draw(self): #global X #glRotate(X,1,0,0) glPushMatrix() glTranslate(8,6.5,-0.55) glScale(15,12,0.1) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["red"]) glutSolidCube(1) glPopMatrix() rr=0 for r in self.grid: cc=0 for c in r: #if True: if rr>0 and rr<13 and cc>0 and cc<16: glPushMatrix() glTranslate(cc,rr,0) if c in ["b","s"]: if c=="b": color = [0.3,0.3,1.0,1.0] elif c=="s": color = [1.0,1.0,0.0,1.0] glMaterialfv(GL_FRONT,GL_DIFFUSE,color) glutWireCube(1) #glutSolidCube(1) elif c in ["d","6"]: glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_SRC_ALPHA); if c=="d": color = [0.8,0.5,0.0, 0.1+0.2*float(self.AG_twinklers.value("twinkle1")) ] elif c=="6": color = [10,0.5,0.0, 0.1+0.2*float(self.AG_twinklers.value("twinkle1")) ] glMaterialfv(GL_FRONT,GL_DIFFUSE,color) glutSolidCube( float(self.AG_twinklers.value("twinkle2"))*0.3+0.7) glBlendFunc(GL_SRC_ALPHA, GL_ONE) glDisable(GL_BLEND) elif c in ["B"]: ##i.e changed to half a block because recieved bash color = [0.3,0.3,1.0,1.0] glMaterialfv(GL_FRONT,GL_DIFFUSE,color) #global lists glCallList(lists["broken brick"]) glPopMatrix() cc+=1 rr+=1 glPushMatrix() self.solomon.draw(self.solomon.stickers) glPopMatrix() for s in self.sprites: glPushMatrix() s.runDetection(self) s.draw() glPopMatrix() s.do()
class Sprite: collision_action=None def __init__(self,x,y): self.x=x self.y=y self.draw_func=self.temp_drawfunc self.AG_move=ActionGroup() self.AG_move.append("x_action",Action(func=self.x_action,max=100,cycle=True,min=0)) self.AG_move.append("y_action",Action(func=self.y_action,max=100,cycle=True,min=0)) self.AG_move.append("z_action",Action(func=self.z_action,max=100,cycle=True,min=0)) self.AG_move.append("xrot_action",Action(func=self.xrot_action,max=360,cycle=True,min=0)) self.AG_move.append("yrot_action",Action(func=self.yrot_action,max=360,cycle=True,min=0)) self.AG_move.append("zrot_action",Action(func=self.zrot_action,max=360,cycle=True,min=0)) def setDrawFuncToList(self,listid): self.listnumber=listid self.draw_func=self.drawList def drawList(self): glCallList(self.listnumber) def runDetection(self,level): level.detect(self.x,self.y,collision_bound=2.0,callback=self.collision_action,ignoreDots=True,ignoreTheseSprites=[self]) def draw(self): #glPushMatrix() #print str((self.x,self.y)) glTranslate(self.x,self.y,0) glTranslate(float(self.AG_move.value("x_action")),float(self.AG_move.value("y_action")),float(self.AG_move.value("z_action"))) glRotate(float(self.AG_move.value("xrot_action")),1,0,0) glRotate(float(self.AG_move.value("yrot_action")),0,1,0) glRotate(float(self.AG_move.value("zrot_action")),0,0,1) #glRotate(15,1,0,0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["white"]) #glutWireCube(1) self.draw_func() #glPopMatrix() def temp_drawfunc(self): glutSolidCube(0.8) def do(self): self.AG_move.do() def x_action(self,tvmm): return 0 def y_action(self,tvmm): return 0 def z_action(self,tvmm): return 0 def xrot_action(self,tvmm): return 0 def yrot_action(self,tvmm): return 0 def zrot_action(self,tvmm): t,v,min,max=tvmm v=t*2 return v
class Solomon: x,y=None,None startx,starty=None,None #st_a=None AG_walk=None AG_jump=None A_wandswish=None current_state={} bound=0.3 #this is his bounding sphere step=0.100 facing=1 #or -1 level=None def x_change(self,amt): print(("changing x",amt)) self.x=val(self.x,amt) def y_change(self,amt): self.y=val(self.y,amt) def wobble(self,tvmm): t,v,mi,ma=tvmm v=t #print (t,v,mi,ma) return v def footR(self,tvsl): t,v,s,l=tvsl if t<7: v+=1 elif t<10: v-=2 else: v=0 #print (t,v,s,l) return v def footL(self,tvsl): t,v,s,l=tvsl if t<7: v+=1 elif t<10: v-=2 else: v=0 #print (t,v,s,l) return v def swish(self,tvmm): t,v,min,max=tvmm v=t #print (t,v) return v def jump_displacement(self,tvmm): print("jump disp called") t,v,min,max=tvmm if t<4: v+=2 else: v+=1 return v def end_jump(self): print("jump complete") self.current_state["jumping"]=False def __init__(self,sx,sy, level): self.level=level self.current_state["standing"]=True self.current_state["crouching"]=False self.current_state["walking"]=False self.current_state["jumping"]=False self.current_state["wandswish"]=False self.current_state["falling"]=False self.x=sx self.y=sy self.startx=sx self.starty=sy self.AG_walk=ActionGroup() self.AG_walk.append("wobble",Action(func=self.wobble,max=5,cycle=True,min=-5,reverseloop=True,init_tick=0)) self.AG_walk.append("footR",Action(func=self.footR,max=13,cycle=True,min=0)) self.AG_walk.append("footL",Action(func=self.footL,max=13,cycle=True,min=0,init_tick=6)) self.AG_jump=ActionGroup() self.AG_jump.append("jump_displacement",Action(func=self.jump_displacement,max=10,min=0)) self.AG_jump.action("jump_displacement").callback=self.end_jump self.AG_walk.speed_scale(2) self.A_wandswish=Action(func=self.swish,min=-7,max=-1,cycle=False,reverseloop=False,init_tick=-7) def state_test_on(self): return [k for k in self.current_state if self.current_state[k]==True] def state_test(self,list): return len([l for l in list if self.current_state[l]==True]) def draw0(self): glTranslate(self.x,self.y,0) glutSolidCube(0.1) def draw(self,stickers=None): #correction glTranslate(0,-0.15,0) #main displacement glTranslate(self.x,self.y,0) #for experiments global X #print (X,self.AG_walk.value("footL"),self.AG_walk.value("footR")) #scale down character glScale(0.3,0.3,0.3) if stickers!=None: for st in stickers: glPushMatrix() ##glLoadIdentity() print((st)) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours[st[3]]) glTranslate(st[0]*10,10*st[1],st[2]) glutSolidCube(0.5) glPopMatrix() #rotate to direction facing if self.facing==-1: glRotatef(180,0,1,0) #correction for drawing character glRotatef(-90.0,1.0,0,0) drawSolProperly=True #if not drawSolProperly: # glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["white"]) # glutSolidCube(0.2) #############################entering crouch section############################### glPushMatrix() if "crouching" in self.state_test_on(): glTranslate(0,0,-0.3) #hat glPushMatrix() if "walking" in self.state_test_on(): glRotatef(-float(self.AG_walk.value("wobble")),1.0,0,0) glTranslate(0,0,0.5) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["hat"]) if drawSolProperly: glutSolidCone(1,2,12,6) glPopMatrix() #head/body glPushMatrix() glTranslate(0,0,0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["body"]) if drawSolProperly: glutSolidSphere(1,12,12) glPopMatrix() #left arm glPushMatrix() if self.state_test(["walking"])>0: glTranslate(0-float(self.AG_walk.value("footR"))/10,0.9,0) elif self.state_test(["standing","crouching","wandswish"])>0: glTranslate(0,0.9,0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["arm"]) if drawSolProperly: glutSolidSphere(0.5,24,12) glPopMatrix() #right arm glPushMatrix() #glTranslate(0,-0.9,0) if self.state_test(["wandswish"])>0: res=self.A_wandswish.do() if res==None: poo=0.0 else: poo=float(res/0.05) #print poo glTranslate(0,-0.9,0) glRotatef(poo,1,1,0) elif self.state_test(["walking"])>0: glTranslate(float(self.AG_walk.value("footL"))/10,-0.9,0) elif self.state_test(["standing","crouching"])>0: glTranslate(0,-0.9,0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["arm"]) if drawSolProperly: glutSolidSphere(0.5,24,12) #move pop to end to keep arm local system #wand q=gluNewQuadric() glPushMatrix() glTranslate(1.1,0,0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["wandtip"]) glRotatef(90,0,1,0) if drawSolProperly: gluCylinder(q,0.1,0.1,0.2,12,1) glPopMatrix() glPushMatrix() glTranslate(0.5,0,0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["wand"]) glRotatef(90,0,1,0) if drawSolProperly: gluCylinder(q,0.1,0.1,0.6,12,1) glPopMatrix() #from arm glPopMatrix() #eyes glPushMatrix() glTranslate(1,.2,.1) glRotatef(90,0,1,0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["wandtip"]) if drawSolProperly: gluDisk(q,0.05,0.2,12,12) glPopMatrix() glPushMatrix() glTranslate(1,-.2,.1) glRotatef(90,0,1,0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["wandtip"]) if drawSolProperly: gluDisk(q,0.05,0.2,12,12) glPopMatrix() #nose glPushMatrix() glTranslate(1,0,-.1) glScale(1,1,0.5) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["arm"]) if drawSolProperly: glutSolidSphere(0.3,24,12) glPopMatrix() ##########################left crouch section################################## glPopMatrix() #end of crouch #drawing rest of body (feet) #left foot glPushMatrix() glTranslate(-0.5,0,0) #apply rotation if walking if self.state_test(["walking"])>0: glRotatef(-15*float(self.AG_walk.value("footL")),0,1,0) elif self.state_test(["standing","crouching","wandswish"])>0: glRotatef(0,0,1,0) glTranslate(0.5,0,0) glScale(1.5,1,.5) glTranslate(0,0.5,-2) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["shoe"]) if drawSolProperly: glutSolidSphere(0.5,24,12) glPopMatrix() #right foot glPushMatrix() glTranslate(-0.5,0,0) #apply rotation if walking if self.state_test(["walking"])>0: glRotatef(-15*float(self.AG_walk.value("footR")),0,1,0) elif self.state_test(["standing","crouching","wandswish"])>0: glRotatef(0,0,1,0) glTranslate(0.5,0,0) glScale(1.5,1,.5) glTranslate(0,-0.5,-2) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["shoe"]) if drawSolProperly: glutSolidSphere(0.5,24,12) glPopMatrix() X+=1
class Sprite: collision_action = None def __init__(self, x, y): self.x = x self.y = y self.draw_func = self.temp_drawfunc self.AG_move = ActionGroup() self.AG_move.append( "x_action", Action(func=self.x_action, max=100, cycle=True, min=0)) self.AG_move.append( "y_action", Action(func=self.y_action, max=100, cycle=True, min=0)) self.AG_move.append( "z_action", Action(func=self.z_action, max=100, cycle=True, min=0)) self.AG_move.append( "xrot_action", Action(func=self.xrot_action, max=360, cycle=True, min=0)) self.AG_move.append( "yrot_action", Action(func=self.yrot_action, max=360, cycle=True, min=0)) self.AG_move.append( "zrot_action", Action(func=self.zrot_action, max=360, cycle=True, min=0)) def setDrawFuncToList(self, listid): self.listnumber = listid self.draw_func = self.drawList def drawList(self): glCallList(self.listnumber) def runDetection(self, level): #level.detect(self.x,self.y,collision_bound=2.0,callback=self.collision_action,ignoreDots=True,ignoreTheseSprites=[self]) pass def draw(self): #glPushMatrix() #print str((self.x,self.y)) glTranslate(self.x, self.y, 0) glTranslate(float(self.AG_move.value("x_action")), float(self.AG_move.value("y_action")), float(self.AG_move.value("z_action"))) glRotate(float(self.AG_move.value("xrot_action")), 1, 0, 0) glRotate(float(self.AG_move.value("yrot_action")), 0, 1, 0) glRotate(float(self.AG_move.value("zrot_action")), 0, 0, 1) #glRotate(15,1,0,0) glMaterialfv(GL_FRONT, GL_DIFFUSE, colours["white"]) #glutWireCube(1) self.draw_func() #glPopMatrix() def temp_drawfunc(self): glutSolidCube(0.8) def run_collision_action(self): if self.collision_action != None: self.collision_action() return self def do(self): self.AG_move.do() def x_action(self, tvmm): return 0 def y_action(self, tvmm): return 0 def z_action(self, tvmm): return 0 def xrot_action(self, tvmm): return 0 def yrot_action(self, tvmm): return 0 def zrot_action(self, tvmm): t, v, min, max = tvmm v = t * 2 return v
class Level: counter=0 grid=None baddies=[] solomon=None sprites=[] bursts=[] door=None target_z=6 proper_z=6 AG_twinklers=None def sin_gen_1(self,tvsl): t,v,s,l=tvsl v=0.5*(1+sin(2*pi*t/(l))) #print (t,v) return v def sin_gen_2(self,tvsl): t,v,s,l=tvsl v=0.5*(1+sin(4*pi*t/(l))) #print (t,v) return v def __init__(self,griddata): griddata.reverse() #self.grid=griddata self.grid=[] for line in griddata: self.grid.append(list(line)) rr=0 for r in self.grid: cc=0 for c in r: if c=="@": self.solomon=Solomon(cc,rr,self) self.grid[rr][cc]="." self.solomon.A_wandswish.callback=self.block_swap elif c=="4": self.door=[cc,rr] self.grid[rr][cc]="." elif c=="k": ns=Sprite(cc,rr+0.5) ns.setDrawFuncToList(lists["green_key"]) ns.collision_action=self.key_detected_something_test self.sprites.append(ns) self.grid[rr][cc]="." elif not c in ["b","B","s"]: self.grid[rr][cc]="." cc+=1 rr+=1 self.AG_twinklers=ActionGroup() self.AG_twinklers.append("twinkle1",Action(func=self.sin_gen_1,max=200,cycle=True,min=0,reverseloop=False,init_tick=0)) self.AG_twinklers.append("twinkle2",Action(func=self.sin_gen_2,max=100,cycle=True,min=0,reverseloop=False,init_tick=10)) def eval_grid(self,coords): return self.grid[coords[1]][coords[0]] def key_detected_something_test(self): print ("KEY GOT!") def block_swap(self,bump_only=False): self.solomon.current_state["wandswish"]=False print("hi from block swap "+str(self.solomon)) takeoff_for_crouching=0 if self.solomon.state_test(["crouching"])>0 : takeoff_for_crouching=-0.5 ##if res=="OK": return yy = self.block_to_action[1] xx = self.block_to_action[0] ch = self.grid[yy][xx] print('yy {} xx {} ch {} '.format(yy,xx,ch)) if not bump_only: #wand flare! crouch=0.5 if self.solomon.current_state["crouching"]==True: crouch=0 self.bursts.append(Burst(x=self.solomon.x+self.solomon.facing*0.5,y=self.solomon.y+crouch,z=0)) #must be in correct place first distanceLeft = 0.5 + (self.solomon.x)-(int(self.solomon.x)) distanceRight = 0.5 + (int(self.solomon.x+1))-(self.solomon.x) distance = 0 if self.solomon.facing==-1: distance=distanceLeft elif self.solomon.facing==1: distance=distanceRight print('distance {}'.format(distance)) #check not in block space when casting if distance > 1.1: if bump_only: if ch=="b": print("destroy block") self.grid[yy][xx]="B" elif ch=="B": print("destroy block") self.grid[yy][xx]="." elif ch==".": print("create") self.grid[yy][xx]="b" else: if ch=="b": print("destroy block") self.grid[yy][xx]="." elif ch==".": print("create") self.grid[yy][xx]="b" elif ch=="k" and not bump_only: print ("*****KEY STRUCK!*****") def grid_cell(self,coords): x=coords[0] y=int(coords[1]) dx = round(x - int(x),3) #print('in {} {} dx {}'.format(x,y,dx)) if dx>0.5: x = int(x) + int(1) else: x = int(x) return [x,y] def set_state(self,state,value=True): self.solomon.current_state[state]=value #print('state {} set to {}'.format(state,value)) def calculate_states(self): if False: print('solomon x,y: {0},{1}'.format(self.solomon.x,self.solomon.y)) gx,gy=int(self.solomon.x),int(self.solomon.y) print('solomon gx,gy: {0},{1}'.format(gx,gy)) print('grid on: {0}'.format(self.grid[gy][gx])) print('grid below: {0}'.format(self.grid[gy-1][gx])) self.solomon.drawSolProperly=True #int((self.counter)/20)%2==0 #under box twitch_left = 0.01 # so the box presses to right edge flush sol_bottom_edge_x1 = self.solomon.x - self.solomon.size_x/2 + twitch_left sol_bottom_edge_x2 = self.solomon.x + self.solomon.size_x/2 sol_bottom_edge_y2 = self.solomon.y - self.solomon.fall_detect sol_top_edge_y2 = self.solomon.y + self.solomon.size_y sol_top_edge_y2_jumping = self.solomon.y + self.solomon.size_y + 0.2 #grid cell for below left and right sol_blockbelow_coord_x1 = self.grid_cell([sol_bottom_edge_x1,sol_bottom_edge_y2]) sol_blockbelow_coord_x2 = self.grid_cell([sol_bottom_edge_x2,sol_bottom_edge_y2]) #grid cell for above left and right sol_blockabove_coord_x1 = self.grid_cell([sol_bottom_edge_x1,sol_top_edge_y2_jumping]) sol_blockabove_coord_x2 = self.grid_cell([sol_bottom_edge_x2,sol_top_edge_y2_jumping]) #grid cell for right top and bottom sol_blockright_coord_y1 = self.grid_cell([sol_bottom_edge_x2+self.solomon.step_inc,sol_top_edge_y2]) sol_blockright_coord_y2 = self.grid_cell([sol_bottom_edge_x2+self.solomon.step_inc,self.solomon.y]) #grid cell for left top and bottom sol_blockleft_coord_y1 = self.grid_cell([sol_bottom_edge_x1-self.solomon.step_inc,sol_top_edge_y2]) sol_blockleft_coord_y2 = self.grid_cell([sol_bottom_edge_x1-self.solomon.step_inc,self.solomon.y]) #print ('{} {}'.format(sol_blockbelow_coord_x1,sol_blockbelow_coord_x2)) #set state on behalf of block below if self.grid[sol_blockbelow_coord_x1[1]][sol_blockbelow_coord_x1[0]]=='.' and \ self.grid[sol_blockbelow_coord_x2[1]][sol_blockbelow_coord_x2[0]]=='.': self.set_state("canfall") else: self.set_state("canfall", False) #set state on behalf of block above if self.grid[sol_blockabove_coord_x1[1]][sol_blockabove_coord_x1[0]]=='.' and \ self.grid[sol_blockabove_coord_x2[1]][sol_blockabove_coord_x2[0]]=='.': self.set_state("headhurt", False) else: self.set_state("headhurt") #set state on behalf of right top and bottom if self.grid[sol_blockright_coord_y1[1]][sol_blockright_coord_y1[0]]=='.' and \ self.grid[sol_blockright_coord_y2[1]][sol_blockright_coord_y2[0]]=='.': self.set_state("cwright") else: self.set_state("cwright", False) #set state on behalf of left top and bottom if self.grid[sol_blockleft_coord_y1[1]][sol_blockleft_coord_y1[0]]=='.' and \ self.grid[sol_blockleft_coord_y2[1]][sol_blockleft_coord_y2[0]]=='.': self.set_state("cwleft") else: self.set_state("cwleft", False) #stickers for underneath (floor) detection #self.solomon.stickers.append([sol_bottom_edge_x1,sol_bottom_edge_y2,0,'green']) #self.solomon.stickers.append([sol_bottom_edge_x2,sol_bottom_edge_y2,0,'blue']) #stickers for underneath (head crash) detection self.solomon.stickers.append([sol_bottom_edge_x1,sol_top_edge_y2,0,'green']) self.solomon.stickers.append([sol_bottom_edge_x2,sol_top_edge_y2,0,'blue']) #stickers for right side solomon (wall) detect #self.solomon.stickers.append([sol_bottom_edge_x2,sol_top_edge_y2,0,'green']) #self.solomon.stickers.append([sol_bottom_edge_x2,self.solomon.y,0,'blue']) #stickers for left side solomon (wall) detect #self.solomon.stickers.append([sol_bottom_edge_x1,sol_top_edge_y2,0,'green']) #self.solomon.stickers.append([sol_bottom_edge_x1,self.solomon.y,0,'blue']) #stickers for showing which block is investigating #self.solomon.stickers.append(sol_blockbelow_coord_x1+[0.1,'green']) #self.solomon.stickers.append(sol_blockbelow_coord_x2+[0,'blue']) def evaluate(self,joystick,keys): self.counter+=1 if self.counter>100000: self.counter=0 self.solomon.stickers=[] self.solomon.stickers.append([self.solomon.x,self.solomon.y,0,"white"]) """ TODO redo current_state was rubbish anyway """ self.calculate_states() #wand is used to hover, so comes before fall and walk if joystick.isFire(keys): print(' fire') if self.solomon.current_state["wandswish"]==False: print(' swish is false') print('self.solomon.wand_rest {}'.format(self.solomon.wand_rest)) if self.solomon.wand_rest==0: self.solomon.wand_rest=self.solomon.wand_rest_start #start swish self.set_state("wandswish") print (" swish") if self.solomon.facing==-1: self.block_to_action = self.grid_cell([self.solomon.x-1,self.solomon.y]) elif self.solomon.facing==1: if round((self.solomon.x-int(self.solomon.x)),3)==0.45: print (' booh! {}'.format(self.grid_cell([self.solomon.x+1,self.solomon.y]))) print (' plop') self.block_to_action = self.grid_cell([self.solomon.x+1,self.solomon.y]) if self.solomon.current_state["crouching"]==True: self.block_to_action=[self.block_to_action[0],self.block_to_action[1]-1] else: #continue swish print (" blah") pass self.calculate_states() # decrease wand swish counter if self.solomon.wand_rest>0: self.solomon.wand_rest-=1 if self.solomon.current_state["canfall"]==False: #if not int(self.solomon.y)==self.solomon.y: # print("integer problem {}".format(self.solomon.y)) # self.solomon.set_y(round(self.solomon.y ,0)) # print("fixed {}".format(self.solomon.y)) if self.solomon.current_state["falling"]==True: self.set_state("falling", False) if self.solomon.current_state["jumping"]==True: self.set_state("jumping", False) print("jumping set false") self.calculate_states() # can fall state true and not swishing wand means can fall if self.solomon.current_state["canfall"] and self.solomon.current_state["wandswish"]==False: print('falling') self.solomon.set_y(self.solomon.y-self.solomon.fall_inc) self.calculate_states() # defaults to can't walk self.solomon.current_state["walking"]=False # crouch, does nothing to collision detection # only offsets the wand target self.solomon.current_state["crouching"]=False if joystick.isDown(keys): self.set_state("crouching") # if not crouching and keys pressed try walking right, animate but move only if can if joystick.isRight(keys): self.solomon.facing=1 if self.solomon.current_state["crouching"]==False: self.set_state("walking") if self.solomon.current_state["cwright"]: self.solomon.set_x(self.solomon.x+self.solomon.step_inc) self.calculate_states() # if not crouching and keys pressed try walking left, animate but move only if can if joystick.isLeft(keys): self.solomon.facing=-1 if self.solomon.current_state["crouching"]==False: self.set_state("walking") if self.solomon.current_state["cwleft"]: self.solomon.set_x(self.solomon.x-self.solomon.step_inc) self.calculate_states() if self.solomon.current_state["jumping"]==False and \ self.solomon.current_state["crouching"]==False and \ self.solomon.current_state["falling"]==False and \ self.solomon.current_state["canfall"]==False and \ self.solomon.current_state["headhurt"]==False: if joystick.isUp(keys): self.set_state("jumping") self.solomon.jumping_rest=self.solomon.jumping_rest_start print ("jumping rest {}".format(self.solomon.jumping_rest)) if self.solomon.jumping_rest>0: self.solomon.jumping_rest-=1 self.solomon.jump_inc=self.solomon.jump_inc_start self.calculate_states() if self.solomon.jumping_rest==0 and self.solomon.current_state["jumping"]==True: print("jumping rest is 0 and jumping is true") if self.solomon.current_state["headhurt"]==False: print('jumping true, no head hurt {}'.format(self.solomon.y)) self.solomon.set_y(round(self.solomon.y + self.solomon.jump_inc_falloff,3)) #if self.solomon.jump_inc<0.01: # self.set_state("jumping", False) else: self.set_state("jumping", False) print("jumping set false") self.calculate_states() # animate the walk cycle if self.solomon.current_state["walking"]: self.solomon.AG_walk.do() self.AG_twinklers.do() return def reset_z(self): self.target_z=self.proper_z print ("reset z called") def detect(self): pass def draw(self): #global X #glRotate(X,1,0,0) glPushMatrix() glTranslate(8,6.5,-0.55) glScale(15,12,0.1) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["red"]) glutSolidCube(1) glPopMatrix() rr=0 for r in self.grid: cc=0 for c in r: #if True: if rr>=0 and rr<=13 and cc>=0 and cc<=16: glPushMatrix() glTranslate(cc,rr+0.5,0) if c in ["b","s"]: if c=="b": color = [0.3,0.3,1.0,1.0] elif c=="s": color = [1.0,1.0,0.0,1.0] glMaterialfv(GL_FRONT,GL_DIFFUSE,color) if True: glutSolidCube(1) elif c in ["d","6"]: glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_SRC_ALPHA); if c=="d": color = [0.8,0.5,0.0, 0.1+0.2*float(self.AG_twinklers.value("twinkle1")) ] elif c=="6": color = [10,0.5,0.0, 0.1+0.2*float(self.AG_twinklers.value("twinkle1")) ] glMaterialfv(GL_FRONT,GL_DIFFUSE,color) glutSolidCube( float(self.AG_twinklers.value("twinkle2"))*0.3+0.7) glBlendFunc(GL_SRC_ALPHA, GL_ONE) glDisable(GL_BLEND) elif c in ["B"]: ##i.e changed to half a block because recieved bash color = [0.3,0.3,1.0,1.0] glMaterialfv(GL_FRONT,GL_DIFFUSE,color) glCallList(lists["broken brick"]) glPopMatrix() cc+=1 rr+=1 glPushMatrix() self.solomon.draw(self.solomon.stickers) glPopMatrix() if debug==True: ''' glPushMatrix() glTranslate(self.solomon_block_below[0],self.solomon_block_below[1],0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["white"]) glutWireCube(0.85) glPopMatrix() glPushMatrix() glTranslate(self.solomon_block_above[0],self.solomon_block_above[1],0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["white"]) glutWireCube(0.85) glPopMatrix() glPushMatrix() glTranslate(self.solomon_block_above_brow1[0],self.solomon_block_above_brow1[1],0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["green"]) glutWireCube(0.85) glPopMatrix() glPushMatrix() glTranslate(self.solomon_block_above_brow2[0],self.solomon_block_above_brow2[1],0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["green"]) glutWireCube(0.85) glPopMatrix() glPushMatrix() glTranslate(self.solomon_block_left[0],self.solomon_block_left[1],0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["white"]) glutWireCube(0.85) glPopMatrix() glPushMatrix() glTranslate(self.solomon_block_right[0],self.solomon_block_right[1],0) glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["white"]) glutWireCube(0.85) glPopMatrix() ''' #centre spot ##glPushMatrix() ##glTranslate(self.solomon.x,self.solomon.y,0) ##glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["white"]) ##glScale(0.4, 0.4,0.4) ##glutSolidCube(1) ##glPopMatrix() #####size box for edge/falling etc ####glPushMatrix() ####glTranslate(self.solomon.x,self.solomon.y,0) ####glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["yellow"]) ####glTranslate(0,self.solomon.size_y/2,0) ####glScale(self.solomon.size_x, self.solomon.size_y,1.0) ####glutWireCube(1) ####glPopMatrix() #### #####detection box for enemies/items ####glPushMatrix() ####glTranslate(self.solomon.x,self.solomon.y,0) ####glMaterialfv(GL_FRONT,GL_DIFFUSE,colours["yellow"]) ####glTranslate(0,self.solomon.det_size_y/2,0) ####glScale(self.solomon.det_size_x, self.solomon.det_size_y,1.0) ####glutWireCube(1) ####glPopMatrix() for s in self.sprites: glPushMatrix() s.runDetection(self) s.draw() glPopMatrix() s.do() for b in self.bursts: if b.draw(): self.bursts.remove(b)