コード例 #1
0
    def next_map(self):
        """Switch to next map"""

        # Clean the map:
        self.object_mgr.destroy_all_objects()
        self.object_mgr.clear_display(self.fill_color)

        # Currently showing the same map again
        next_map_details = self.loader.next_map()
        if next_map_details:
            self.map = Map(*next_map_details[0:2])
        else:
            self.change_scene = SceneInit("Score", players=self.players)
            return

        # Resetting balls and adding them back to simulation space
        for player in self.players:
            player.ball.state = BallState.NOT_MOVING
            player.ball.shape.body.position = Vec2d(flip_coords(next_map_details[2]['pos']))
            player.ball.shape.body.velocity = Vec2d(0.0, 0.0)
            self.object_mgr.register_object(player.ball)
            print("Player {} points: {}".format(player.id, player.points))

        self.next_turn = False

        # Setting up first player
        self.players[0].ball.turn = True
        print("Player 0 to move")

        self.show_score()

        self.object_mgr.blit_on_display(self.object_mgr.draw_static_objects())
コード例 #2
0
    def __init__(self, position, dimension, image, id=-1, name='', obj_mgr=None, obj_type='static', rotation=0):
        super().__init__()

        self.image = pygame.transform.scale(image, dimension)
        self.image = pygame.transform.rotate(self.image, rotation)
        self.id = id
        self.name = name
        self.shape = self.prepare_body(flip_coords(position))
        self.object_mgr = obj_mgr
        self.type = obj_type

        # Add newly created objects to the Object Manager, if not mentioned otherwise
        if obj_mgr is not None:
            obj_mgr.register_object(self)
コード例 #3
0
 def draw(self, display):
     pos = (self.shape.bb.left - self.offset[0],
            self.shape.bb.top + self.offset[1])
     display.blit(self.image, flip_coords(pos))
コード例 #4
0
 def get_position(self, pygame=False):
     if pygame:
         return flip_coords(self.shape.body.position)
     else:
         return self.shape.body.position
コード例 #5
0
    def handle_event(self, event):
        """Handling specific scene events"""
        if event.type == pygame.MOUSEBUTTONDOWN:

            if event.button == pygame.BUTTON_LEFT:

                # Find ball that was clicked if none than omit
                event_pos = flip_coords((event.pos[0] - self.camera_offset[0],
                                         event.pos[1] - self.camera_offset[1]))
                ball = self.clicked_ball(Vec2d(event_pos[0], event_pos[1]))

                if ball:

                    # If its ball's turn and all balls are still, let the ball be clicked to hit
                    if ball.turn and self.balls_not_moving():
                        pos = ball.shape.body.position
                        ball.state = BallState.CLICKED
                        self.trajectory = [(flip_coords(pos)[0] + self.camera_offset[0],
                                            flip_coords(pos)[1] + self.camera_offset[1]),
                                           (flip_coords(pos)[0] + self.camera_offset[0],
                                            flip_coords(pos)[1] + self.camera_offset[1])]

        elif event.type == pygame.MOUSEBUTTONUP:

            # Get current player
            player = self.current_player()

            if player.ball.state is BallState.CLICKED:
                self.trajectory = None

                if event.button == pygame.BUTTON_LEFT:

                    direction = 10 * Vec2d(
                        player.ball.shape.body.position.x - flip_coords(event.pos)[0] + self.camera_offset[0],
                        player.ball.shape.body.position.y - flip_coords(event.pos)[1] - self.camera_offset[1]
                    )

                    # Max impulse
                    if math.fabs(direction.x) > 1800:
                        coeff = math.fabs(1800 / direction.x)
                        direction *= coeff

                    if math.fabs(direction.y) > 1800:
                        coeff = math.fabs(1800 / direction.y)
                        direction *= coeff

                    player.ball.shape.body.apply_impulse_at_local_point(direction)
                    self.next_turn = True

                    # Adding points
                    if str(self.map.id) in player.points.keys():
                        player.points[str(self.map.id)] += 1
                    else:
                        player.points[str(self.map.id)] = 1
                    self.show_score()

                else:
                    player.ball.state = BallState.NOT_MOVING

        elif event.type == pygame.MOUSEMOTION and self.trajectory:
            self.trajectory[1] = pygame.mouse.get_pos()