def click_in_grid(self,x,y,button,mods): if button != 1: return fx = x/768.0 * 40 fy = y/768.0 * 40 coords = collision.nearest_neighbours(fx,fy,0).next() level = self.level if self.cellcode == "S": level.hexes[level.start] = " " level.start = coords level.hexes[coords] = "S" elif self.cellcode == "X": level.hexes[level.exit] = " " level.exit = coords level.hexes[coords] = "X" elif self.cellcode is None: if (coords not in [level.start,level.exit] and coords in level.hexes): del level.hexes[coords] self.unplace(coords) elif self.cellcode in self.monster_list: self.place_monster(coords,self.cellcode) elif self.cellcode in self.balls_list: self.place_powerup(coords,self.cellcode) level.hexes[coords] = "P" elif self.cellcode in ["Au","Ag","Cu","Pt"]: level.hexes[coords] = self.cellcode else: level.hexes[coords] = self.cellcode self.hexfield.build_dl() self.hexfield.prepare()
def on_collision(self,what,where,direction): if isinstance(what,graphics.Ball): self.hunt(what,where,1.1) self.harm_type = "provoked a" sounds.play("roar") self.enrage() elif isinstance(what,graphics.Player): self.eat(what,direction) sounds.play("munch") else: self.turn_to(direction) self.pos = where self.rage -= 1 if self.rage <= 0: self.hiding = True x,y,_ = where hc,hr = collision.nearest_neighbours(x,y,0).next() self.pos = collision.h_centre(hc,hr) self.velocity = Vec(0,0,0) self.prepare()
def obstacles_near(self,x,y): return [(hc,hr,self.hexes[hc,hr]) for hc,hr in collision.nearest_neighbours(x,y,2) if self.hexes.get((hc,hr)," ") not in " SXOP"]
def step_player(self,ms): ms = min(ms,35.0) # avoid collision glitches when fps is low player = self.player if self.keysdown: if self.first_person: a = self.player.angle strafe = 0 if pygletkey.Q in self.keysdown: strafe = a+90 elif pygletkey.E in self.keysdown: strafe = a-90 if pygletkey.A in self.keysdown: a += 5 if pygletkey.D in self.keysdown: a -= 5 theta = radians(a) if pygletkey.W in self.keysdown: v = Vec(cos(theta),sin(theta)) * ms * 0.01 elif pygletkey.S in self.keysdown: v = Vec(cos(theta),sin(theta)) * ms * -0.01 else: v = Vec(0,0) if strafe: A = radians(strafe) v += Vec(cos(A),sin(A)) * ms * 0.01 dx,dy,dz = v player.angle = a self.camera.look_from_spherical(30,a + 180,20,200) else: z = Vec(0,0) v = sum((self.movekeys.get(k,z) for k in self.keysdown),z).normalise() * ms * 0.01 dx,dy,dz = v if dx or dy: a = degrees(atan2(dy,dx)) player.angle = a if dx or dy: # See if player will collide with any hexagons px,py,pz = player.pos obstacles = self.level.obstacles_near(px,py) newpos = v + player.pos r = player.getgeom('radius',0.49) for hc,hr,cell in obstacles: P = collision.collides(hc,hr,player.pos,r,v,collision.COLLIDE_POSITION) if P: newpos = P break player.pos = newpos self.camera.look_at(tuple(player.pos)) # See if player has escaped phex = collision.nearest_neighbours(newpos.x,newpos.y,0).next() if phex == self.player_exit: sounds.play("fanfare") if self.levelnum > 0: levelnum = self.levelnum + 1 else: levelnum = self.levelnum - 1 level = self.find_level(levelnum) if level: self.exit_to(GameScreen,score=self.score,level=level, levelnum=levelnum,ammo=self.special_ammo, special=self.special_ball) else: self.exit_to(VictoryScreen,score=self.score) elif phex in self.level.powerups: bname = self.hexfield.collect(*phex) if bname: B = getattr(graphics,bname) if B == self.special_ball: self.special_ammo += B.ammo else: self.special_ball = B self.special_ammo = B.ammo sounds.play("chamber") self["hud"].set_ammo(self.special_ammo, self.special_ball.__name__) b = self[repr(phex)] if b: self["powerups"].remove(b)