def __init__(self, pos=(0, 0), radious=10, density=800): '''@pos (x, y) is the pixel position in the screen @radious of the ball for collision detection, in pixels @density of the sphere, in kg/m^3''' Actor.__init__(self) # set up the sprite self.image = pygame.Surface((radious * 2, radious * 2), pygame.SRCALPHA | pygame.HWSURFACE, 32).convert_alpha() self.rect = self.image.get_rect(center=pos) image = pygame.image.load('resources/images/ball.png').convert_alpha() image = pygame.transform.scale(image, self.rect.size) self.image.blit(image, (0, 0)) # some internal values self.x, self.y = pos self.r = radious # dynamic properties set self.body = ode.Body(globals.ode_world) self.mass = ode.Mass() self.mass.setSphere(density, world.pix_to_dist(radious)) self.body.setMass(self.mass) # object spatial setup self.geom = ode.GeomSphere(globals.ode_space, world.pix_to_dist(radious)) self.geom.setBody(self.body) # custom attributes self.geom.mu = 0.0 self.geom.bounce = 0.5 # bind it to the 2d plane self.body.setPosition((0, 0, 0)) self.join = ode.Plane2DJoint(globals.ode_world) self.join.attach(self.body, ode.environment) # to simulate friction we set the plane2djoint as if it was a 2d motor # always trying to reach a 0 velocity self.join.setXParam(ode.paramVel, 0) self.join.setXParam(ode.paramFMax, globals.FIELD_FRICTION) self.join.setYParam(ode.paramVel, 0) self.join.setYParam(ode.paramFMax, globals.FIELD_FRICTION)
def __init__(self, pos=0, penguins=1, controller=None, team=0): ''' @pos is the x position of this bar, 0 is the goal @penguins is the number of penguins in this bar (1 for keeper, etc...) @controller is a Controller instance that will control the bar @team the team number ''' # for now, testing purpose, the position is hard-coded #pos = (globals.DISPLAY_SIZE[0] / 2, globals.DISPLAY_SIZE[1] / 2) pos = (globals.FIELD_TOP_LEFT[0] + pos, globals.DISPLAY_SIZE[1] / 2 + 2) Actor.__init__(self) self.controller = controller self.team = team if team == 0: self.color = (0, 64, 0) # TODO config? elif team == 1: self.color = (64, 0, 0) else: self.color = (128, 128, 128) # set up the sprite self.base_image = pygame.Surface((64, globals.FIELD_SIZE[1]), pygame.SRCALPHA | pygame.HWSURFACE, 32).convert_alpha() self.image = self.base_image.copy() self.rect = self.image.get_rect(center=pos) self.base_pos = self.rect.topleft if not PenguinBar.bar_image: PenguinBar.bar_image = pygame.image.load( 'resources/images/barra.png').convert_alpha() if not PenguinBar.sprites: PenguinBar.sprites = (pygame.image.load( 'resources/images/penguinR.png').convert_alpha(), pygame.image.load( 'resources/images/penguinL.png').convert_alpha()) PenguinBar.sprites_k = (pygame.image.load( 'resources/images/keeperR.png').convert_alpha(), pygame.image.load( 'resources/images/keeperL.png').convert_alpha()) r = PenguinBar.sprites[0].get_rect() PenguinBar.sprite_size = r.h PenguinBar.num_frames = r.w / r.h # drawing optimization stuff self.updated = False # get a position for the bar bar_pos = world.pix_to_w(pos) bar_pos = (bar_pos[0], bar_pos[1], self.BAR_HEIGHT) # height over z axis # init the sliding part self.slider_bar = ode.Body(globals.ode_world) self.slider_bar.setPosition(bar_pos) self.slider = ode.SliderJoint(globals.ode_world) self.slider.attach(self.slider_bar, ode.environment) self.slider.setAxis((0, 1, 0)) #self.slider.setParam(ode.ParamVel, 0.0) # add a motor to simulate friction #self.slider.setParam(ode.ParamFMax, 1000.0) # the hinged part self.rot_bar = ode.Body(globals.ode_world) self.rot_bar.setPosition(bar_pos) self.hinge = ode.HingeJoint(globals.ode_world) self.hinge.attach(self.rot_bar, self.slider_bar) self.hinge.setAxis((0, 1, 0)) self.hinge.setAnchor(bar_pos) #self.hinge.setParam(ode.ParamVel, 0.0) # add a motor to simulate friction #self.hinge.setParam(ode.ParamFMax, 100.0) self.rotating = 0 # penguins self.num_penguins = penguins self.bodies = [None] * penguins penguin_separation = globals.FIELD_SIZE[1] / penguins first_penguin = globals.FIELD_TOP_LEFT[1] + penguin_separation / 2.0 for i in xrange(penguins): # set up the ode body with a mass and position peng = ode.Body(globals.ode_world) self.bodies[i] = peng pgpos = world.pix_to_w((pos[0], first_penguin + penguin_separation * i)) peng.setPosition(pgpos) # glue it to the hinged bar with a fixed joint peng.fixjoint = ode.FixedJoint(globals.ode_world) peng.fixjoint.attach(self.bodies[i], self.rot_bar) peng.fixjoint.setFixed() # penguins are the only part of the bar that will collide, so they # need to have a geom representation. can be whatever, but for starters # we will use a single sphere peng.geom = ode.GeomSphere(globals.ode_space, world.pix_to_dist(self.PENGUIN_SIZE)) peng.geom.setBody(self.bodies[i]) # set custom friction and bounce for the penguins peng.geom.mu = 0 peng.geom.bounce = -0.5 # will not bounce the ball, so we can stop it h = world.pix_to_dist(globals.FIELD_SIZE[1]) if self.num_penguins == 1: # keeper can only move inside the goal area self.max_extent = world.pix_to_dist(globals.GOAL_SIZE[1] * 1.5) else: self.max_extent = (h / float(self.num_penguins)) / 2