Beispiel #1
0
    def update(self, delta):
        '''Along the game, a constant force being applied to the ball in order to pull it over the middle of the field so that it wont get stuck somewhere 
        in the game field. '''
        x, y, z = self.body.getPosition()
        self.rect.center = world.w_to_pix((x, y, z)) # update sprite position
        
        goal = self.rect.collidelist(globals.goals) # check if it's scoring and then play sound effects, draw animation if so.
        if goal >= 0:
            if globals.sound:
                goal_channel.play(goal_sound)
                goal_channel.fadeout(3000)
            globals.assets.add(GoalAnimation())
            globals.score[goal] += 1
            self.kill()
            return

        
        # position correction: the plane2d joint should keep z around 0 but
        # it's inaccurate sometimes
        self.body.setPosition((x, y, 0.0))
        
        # we will simulate the curvature of the field applying a force
        # proportional to the distance to the center of the field
        force_factor = ((globals.DISPLAY_SIZE[0] / 2.0 - self.rect.centerx)
                       / (globals.FIELD_SIZE[0] / 2.0))
        self.body.addForce((force_factor * globals.FIELD_CONCAVE_FACTOR, 0, 0))
        
        vel = self.body.getLinearVel()[:2]
        pos = self.body.getPosition()
        if (vel[0]**2 + vel[1]**2) < 0.0025 and abs(pos[0]) < 0.05:
            self.kickoff()
Beispiel #2
0
    def drawPenguins(self, i):
        ''' Drawing body attributes of the penguins on the bars;
        but before that we are doing a position converting from ode world to pygame world '''
        size = self.sprite_size
        x_offset = self.base_pos[0]
        y_offset = self.base_pos[1]
        ntl = None
        nbr = None
        for b in self.bodies:
            bpos = b.getPosition()
            dpos = world.w_to_pix(bpos)
            
            ## new boundaries
            if not ntl:
                h = max((y_offset, dpos[1] - self.sprite_size / 2.0))
                ntl = (x_offset, h)
            nbr = (x_offset + self.sprite_size, dpos[1] + self.sprite_size / 2.0)
            
            dpos = dpos[0] - x_offset, dpos[1] - y_offset            
            a = -self.hinge.getAngle()
            index = int(round((PenguinBar.num_frames/2) / math.pi * a))
            if index < 0: index += PenguinBar.num_frames

            pos = ((self.rect.w - size) / 2, dpos[1] - size / 2)            

            if self.num_penguins == 1:
                i.blit(PenguinBar.sprites_k[self.team], pos, (index * size, 0, size, size))
            else:
                i.blit(PenguinBar.sprites[self.team], pos, (index * size, 0, size, size))
            
            # ==== DEBUG
            if globals.debug:
                dpos = world.w_to_pix((bpos[0], bpos[2], bpos[1]))
                pos = (dpos[0] - self.rect.x, dpos[1] - size / 2)
                pygame.draw.line(i, (0, 0, 0), (0, 46 + self.rect.h / 2),
                                 (self.rect.w, 46 + self.rect.h / 2))
                pygame.draw.circle(i, self.color, pos, self.PENGUIN_SIZE)
            # ====
        
        self.rect = pygame.Rect(ntl, (size, nbr[1] - ntl[1]))