Exemplo n.º 1
0
    def make_chain(self, body1, body2, pos1, pos2, link_length, radius):
        dist = int(distance(pos1, pos2) + 0.5)
        x1, y1 = pos1
        x2, y2 = pos2
        bearing = math.atan2((y2 - y1), (x2 - x1))

        if dist < link_length:  # Too short to make a chain
            self.game.world.add.joint(body1, body2, pos1, pos2)
            return

        # Draw circles along the path and join them together
        prev_circle = body1
        prev_pos = tuple_to_int((x1, y1))
        for current_point in range(int(link_length / 2.), dist,
                                   int(link_length)):
            x = x1 + (current_point) * math.cos(bearing)
            y = y1 + (current_point) * math.sin(bearing)
            circle = self.game.world.add.ball(tuple_to_int((x, y)),
                                              radius, dynamic=True,
                                              density=1.0,
                                              restitution=0.16, friction=0.1)
            circle.userData['color'] = (0, 0, 0)
            self.game.world.add.joint(
                prev_circle, circle, prev_pos, tuple_to_int((x, y)), False)
            if (current_point + link_length) >= dist:
                self.game.world.add.joint(
                    circle, body2, tuple_to_int((x, y)), pos2, False)
            prev_circle = circle
            prev_pos = tuple_to_int((x, y))
Exemplo n.º 2
0
    def make_chain(self, body1, body2, pos1, pos2, link_length, radius):
        dist = int(distance(pos1, pos2) + 0.5)
        x1, y1 = pos1
        x2, y2 = pos2
        bearing = math.atan2((y2 - y1), (x2 - x1))

        if dist < link_length:  # Too short to make a chain
            self.game.world.add.joint(body1, body2, pos1, pos2)
            return

        # Draw circles along the path and join them together
        prev_circle = body1
        prev_pos = tuple_to_int((x1, y1))
        for current_point in range(int(link_length / 2.), dist,
                                   int(link_length)):
            x = x1 + (current_point) * math.cos(bearing)
            y = y1 + (current_point) * math.sin(bearing)
            circle = self.game.world.add.ball(tuple_to_int((x, y)),
                                              radius,
                                              dynamic=True,
                                              density=1.0,
                                              restitution=0.16,
                                              friction=0.1)
            circle.userData['color'] = (0, 0, 0)
            self.game.world.add.joint(prev_circle, circle, prev_pos,
                                      tuple_to_int((x, y)), False)
            if (current_point + link_length) >= dist:
                self.game.world.add.joint(circle, body2, tuple_to_int((x, y)),
                                          pos2, False)
            prev_circle = circle
            prev_pos = tuple_to_int((x, y))
Exemplo n.º 3
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN and event.button == 1:
         self.vertices = [tuple_to_int(event.pos)]
         self.safe = False
     elif event.type == MOUSEBUTTONUP and event.button == 1:
         if not self.vertices:
             return
         if len(self.vertices) == 1 and self.previous_vertices is not None:
             last_x_y = self.previous_vertices[-1]
             delta_x = last_x_y[0] - tuple_to_int(event.pos)[0]
             delta_y = last_x_y[1] - tuple_to_int(event.pos)[1]
             self.vertices = [[i[0] - delta_x, i[1] - delta_y]
                              for i in self.previous_vertices]
             self.safe = True
         if self.vertices and self.safe:
             self.constructor(
                 self.vertices,
                 density=self.palette_data['density'],
                 restitution=self.palette_data['restitution'],
                 friction=self.palette_data['friction'])
             self.previous_vertices = self.vertices[:]
         self.vertices = None
     elif event.type == MOUSEMOTION and self.vertices:
         self.vertices.append(tuple_to_int(event.pos))
         if not self.vertices:
             return
         if distance(tuple_to_int(event.pos), self.vertices[0]) >= 55 and \
                 len(self.vertices) > 3:
             self.safe = True
Exemplo n.º 4
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN and event.button == 1:
         self.vertices = [tuple_to_int(event.pos)]
         self.safe = False
     elif event.type == MOUSEBUTTONUP and event.button == 1:
         if not self.vertices:
             return
         if len(self.vertices) == 1 and self.previous_vertices is not None:
             last_x_y = self.previous_vertices[-1]
             delta_x = last_x_y[0] - tuple_to_int(event.pos)[0]
             delta_y = last_x_y[1] - tuple_to_int(event.pos)[1]
             self.vertices = [[i[0] - delta_x, i[1] - delta_y]
                              for i in self.previous_vertices]
             self.safe = True
         if self.vertices and self.safe:
             self.constructor(self.vertices,
                              density=self.palette_data['density'],
                              restitution=self.palette_data['restitution'],
                              friction=self.palette_data['friction'])
             self.previous_vertices = self.vertices[:]
         self.vertices = None
     elif event.type == MOUSEMOTION and self.vertices:
         self.vertices.append(tuple_to_int(event.pos))
         if not self.vertices:
             return
         if distance(tuple_to_int(event.pos), self.vertices[0]) >= 55 and \
                 len(self.vertices) > 3:
             self.safe = True
Exemplo n.º 5
0
 def draw(self):
     Tool.draw(self)
     # Draw a circle from pt1 to mouse
     if self.pt1 is not None:
         delta = distance(self.pt1, tuple_to_int(pygame.mouse.get_pos()))
         if delta > 0:
             self.radius = max(delta, 5)
             pygame.draw.circle(self.game.screen, (100, 180, 255), self.pt1,
                                int(self.radius), 3)
             pygame.draw.line(self.game.screen, (100, 180, 255), self.pt1,
                              tuple_to_int(pygame.mouse.get_pos()), 1)
Exemplo n.º 6
0
 def draw(self):
     Tool.draw(self)
     # Draw a circle from pt1 to mouse
     if self.pt1 is not None:
         delta = distance(self.pt1,
                          tuple_to_int(pygame.mouse.get_pos()))
         if delta > 0:
             self.radius = max(delta, 5)
             pygame.draw.circle(self.game.screen, (100, 180, 255),
                                self.pt1, int(self.radius), 3)
             pygame.draw.line(self.game.screen, (100, 180, 255), self.pt1,
                              tuple_to_int(pygame.mouse.get_pos()), 1)
Exemplo n.º 7
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button == 1:
             self.pt1 = tuple_to_int(event.pos)
     elif event.type == MOUSEBUTTONUP:
         if event.button == 1 and self.pt1 is not None:
             mouse_x_y = tuple_to_int(event.pos)
             self.constructor(self.pt1, mouse_x_y,
                              self.palette_data['density'],
                              self.palette_data['restitution'],
                              self.palette_data['friction'])
             self.pt1 = None
Exemplo n.º 8
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button == 1:
             self.pt1 = tuple_to_int(event.pos)
     elif event.type == MOUSEBUTTONUP:
         if event.button == 1 and self.pt1 is not None:
             mouse_x_y = tuple_to_int(event.pos)
             self.constructor(self.pt1, mouse_x_y,
                              self.palette_data['density'],
                              self.palette_data['restitution'],
                              self.palette_data['friction'])
             self.pt1 = None
Exemplo n.º 9
0
 def draw(self):
     Tool.draw(self)
     if self.pm_mode_active:
         pygame.draw.circle(
             self.game.screen, (255, 255, 255),
             tuple_to_int((self.pm_x, self.pm_y)),
             self.PIN_MOTOR_RADIUS, 0)
Exemplo n.º 10
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)
        if pygame.mouse.get_pressed()[0] and hasattr(event, "pos"):
            if not self.vertices:
                self.vertices = []
            self.vertices.append(tuple_to_int(event.pos))
            if len(self.vertices) > 10:
                self.vertices.pop(0)

            body_to_remove = find_body(self.game.world, event.pos)
            if body_to_remove is not None:
                tracklist = list(self.game.trackinfo.items())
                destroyed_body = False
                for key, info in tracklist:
                    trackdex = info[4]
                    if 'track_indices' in body_to_remove.userData and \
                       trackdex in body_to_remove.userData['track_indices'] \
                       and info[3] is False:
                        self.game.world.world.DestroyBody(info[1])
                        self.game.trackinfo[key][3] = True
                        destroyed_body = True
                        break

                jointnode = body_to_remove.joints
                if jointnode and not destroyed_body:
                    joint = jointnode[-1].joint
                    self.game.world.world.DestroyJoint(joint)
                elif not destroyed_body:
                    self.game.world.world.DestroyBody(body_to_remove)
        elif event.type == MOUSEBUTTONUP and event.button == 1:
            self.cancel()
Exemplo n.º 11
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)
        if pygame.mouse.get_pressed()[0] and hasattr(event, "pos"):
            if not self.vertices:
                self.vertices = []
            self.vertices.append(tuple_to_int(event.pos))
            if len(self.vertices) > 10:
                self.vertices.pop(0)

            body_to_remove = find_body(self.game.world, event.pos)
            if body_to_remove is not None:
                tracklist = self.game.trackinfo.items()
                destroyed_body = False
                for key, info in tracklist:
                    trackdex = info[4]
                    if 'track_indices' in body_to_remove.userData and \
                       trackdex in body_to_remove.userData['track_indices'] \
                       and info[3] is False:
                        self.game.world.world.DestroyBody(info[1])
                        self.game.trackinfo[key][3] = True
                        destroyed_body = True
                        break

                jointnode = body_to_remove.joints
                if jointnode and not destroyed_body:
                    joint = jointnode[-1].joint
                    self.game.world.world.DestroyJoint(joint)
                elif not destroyed_body:
                    self.game.world.world.DestroyBody(body_to_remove)
        elif event.type == MOUSEBUTTONUP and event.button == 1:
            self.cancel()
Exemplo n.º 12
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         self.jb1pos = tuple_to_int(event.pos)
         self.jb1 = find_body(self.game.world, event.pos)
         if self.jb1 is not None:
             self.constructor(self.jb1pos)
         self.jb1 = self.jb1pos = None
Exemplo n.º 13
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         self.jb1pos = tuple_to_int(event.pos)
         self.jb1 = find_body(self.game.world, event.pos)
         if self.jb1 is not None:
             self.constructor(self.jb1pos)
         self.jb1 = self.jb1pos = None
Exemplo n.º 14
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button >= 1:
             # Grab the first body
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             if self.jb1 is not None:
                 self.constructor(self.jb1pos, self.palette_data['speed'])
             self.jb1 = self.jb1pos = None
Exemplo n.º 15
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button >= 1:
             # Grab the first body
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             if self.jb1 is not None:
                 self.constructor(self.jb1pos, self.palette_data['speed'])
             self.jb1 = self.jb1pos = None
Exemplo n.º 16
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button == 1:
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             if self.jb1 is not None:
                 self.jb1[0].userData['rollMotor'] = {}
                 self.jb1[0].userData['rollMotor']['targetVelocity'] = -10
                 self.jb1[0].userData['rollMotor']['strength'] = 40
             self.jb1 = self.jb1pos = None
Exemplo n.º 17
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button >= 1:
             # Grab the first body
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             self.jb2pos = None
     elif event.type == MOUSEBUTTONUP:
         if event.button == 1:
             # Grab the second position
             self.jb2pos = tuple_to_int(event.pos)
             self.constructor(self.jb1pos, self.jb2pos)
             # add joint to ground body
             # elif self.jb1:
             #    groundBody = self.game.world.world.GetGroundBody()
             #    self.game.world.add.joint(self.jb1[0], groundBody,
             #                              self.jb1pos, self.jb2pos)
             # regardless, clean everything up
             self.jb1 = self.jb1pos = self.jb2pos = None
Exemplo n.º 18
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button >= 1:
             # Grab the first body
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             self.jb2pos = None
     elif event.type == MOUSEBUTTONUP:
         if event.button == 1:
             # Grab the second position
             self.jb2pos = tuple_to_int(event.pos)
             self.constructor(self.jb1pos, self.jb2pos)
             # add joint to ground body
             # elif self.jb1:
             #    groundBody = self.game.world.world.GetGroundBody()
             #    self.game.world.add.joint(self.jb1[0], groundBody,
             #                              self.jb1pos, self.jb2pos)
             # regardless, clean everything up
             self.jb1 = self.jb1pos = self.jb2pos = None
Exemplo n.º 19
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button == 1:
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             if self.jb1 is not None:
                 self.jb1[0].userData['rollMotor'] = {}
                 self.jb1[0].userData['rollMotor']['targetVelocity'] = -10
                 self.jb1[0].userData['rollMotor']['strength'] = 40
             self.jb1 = self.jb1pos = None
Exemplo n.º 20
0
 def draw(self):
     Tool.draw(self)
     # Draw a box from pt1 to mouse
     if self.pt1 is not None:
         mouse_x_y = tuple_to_int(pygame.mouse.get_pos())
         if mouse_x_y[0] != self.pt1[0] or mouse_x_y[1] != self.pt1[1]:
             self.width = mouse_x_y[0] - self.pt1[0]
             self.height = mouse_x_y[1] - self.pt1[1]
             self.rect = pygame.Rect(self.pt1, (self.width, self.height))
             self.rect.normalize()
             pygame.draw.rect(self.game.screen, (100, 180, 255),
                              self.rect, 3)
Exemplo n.º 21
0
 def draw(self):
     Tool.draw(self)
     # Draw a box from pt1 to mouse
     if self.pt1 is not None:
         mouse_x_y = tuple_to_int(pygame.mouse.get_pos())
         if mouse_x_y[0] != self.pt1[0] or mouse_x_y[1] != self.pt1[1]:
             self.width = mouse_x_y[0] - self.pt1[0]
             self.height = mouse_x_y[1] - self.pt1[1]
             self.rect = pygame.Rect(self.pt1, (self.width, self.height))
             self.rect.normalize()
             pygame.draw.rect(self.game.screen, (100, 180, 255), self.rect,
                              3)
Exemplo n.º 22
0
 def draw(self):
     Tool.draw(self)
     # Draw the poly being created
     if self.vertices:
         for i in range(len(self.vertices) - 1):
             pygame.draw.line(self.game.screen, (100, 180, 255),
                              self.vertices[i], self.vertices[i + 1], 3)
         pygame.draw.line(self.game.screen, (100, 180, 255),
                          self.vertices[-1],
                          tuple_to_int(pygame.mouse.get_pos()), 3)
         pygame.draw.circle(self.game.screen, (100, 180, 255),
                            self.vertices[0], 15, 3)
Exemplo n.º 23
0
 def draw(self):
     Tool.draw(self)
     # Draw the poly being created
     if self.vertices:
         for i in range(len(self.vertices) - 1):
             pygame.draw.line(self.game.screen, (100, 180, 255),
                              self.vertices[i], self.vertices[i + 1], 3)
         pygame.draw.line(self.game.screen, (100, 180, 255),
                          self.vertices[-1],
                          tuple_to_int(pygame.mouse.get_pos()), 3)
         pygame.draw.circle(self.game.screen, (100, 180, 255),
                            self.vertices[0], 15, 3)
Exemplo n.º 24
0
 def handleToolEvent(self, event):
     radius = int(self.palette_data[self.palette_data_type]['radius'])
     link_length = \
         self.palette_data[self.palette_data_type]['link_length']
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN and event.button == 1:
         self.vertices = [tuple_to_int(event.pos)]
         self.safe = False
     elif event.type == MOUSEBUTTONUP and event.button == 1:
         if not self.vertices:
             return
         if len(self.vertices) == 1 and self.previous_vertices is not None:
             last_x_y = self.previous_vertices[-1]
             delta_x = last_x_y[0] - tuple_to_int(event.pos)[0]
             delta_y = last_x_y[1] - tuple_to_int(event.pos)[1]
             self.vertices = [[i[0] - delta_x, i[1] - delta_y]
                              for i in self.previous_vertices]
             self.safe = True
         if self.vertices and self.safe:
             self.constructor(self.vertices, link_length, radius)
             self.previous_vertices = self.vertices[:]
         self.vertices = None
     elif event.type == MOUSEMOTION and self.vertices:
         last_x_y = self.vertices[-1]
         if distance(tuple_to_int(event.pos), last_x_y) >= link_length:
             self.vertices.append(tuple_to_int(event.pos))
         if not self.vertices:
             return
         if distance(tuple_to_int(event.pos),
                     self.vertices[0]) >= link_length * 5 and \
                 len(self.vertices) > 3:
             self.safe = True
Exemplo n.º 25
0
 def handleToolEvent(self, event):
     radius = int(self.palette_data[self.palette_data_type]['radius'])
     link_length = \
         self.palette_data[self.palette_data_type]['link_length']
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN and event.button == 1:
         self.vertices = [tuple_to_int(event.pos)]
         self.safe = False
     elif event.type == MOUSEBUTTONUP and event.button == 1:
         if not self.vertices:
             return
         if len(self.vertices) == 1 and self.previous_vertices is not None:
             last_x_y = self.previous_vertices[-1]
             delta_x = last_x_y[0] - tuple_to_int(event.pos)[0]
             delta_y = last_x_y[1] - tuple_to_int(event.pos)[1]
             self.vertices = [[i[0] - delta_x, i[1] - delta_y]
                              for i in self.previous_vertices]
             self.safe = True
         if self.vertices and self.safe:
             self.constructor(self.vertices, link_length, radius)
             self.previous_vertices = self.vertices[:]
         self.vertices = None
     elif event.type == MOUSEMOTION and self.vertices:
         last_x_y = self.vertices[-1]
         if distance(tuple_to_int(event.pos), last_x_y) >= link_length:
             self.vertices.append(tuple_to_int(event.pos))
         if not self.vertices:
             return
         if distance(tuple_to_int(event.pos),
                     self.vertices[0]) >= link_length * 5 and \
                 len(self.vertices) > 3:
             self.safe = True
Exemplo n.º 26
0
 def draw(self):
     Tool.draw(self)
     # Draw a triangle from pt1 to mouse
     if self.pt1 is not None:
         mouse_x_y = tuple_to_int(pygame.mouse.get_pos())
         if mouse_x_y[0] != self.pt1[0] or mouse_x_y[1] != self.pt1[1]:
             self.vertices = constructTriangleFromLine(self.pt1, mouse_x_y)
             self.line_delta = [mouse_x_y[0] - self.pt1[0],
                                mouse_x_y[1] - self.pt1[1]]
             pygame.draw.polygon(self.game.screen, (100, 180, 255),
                                 self.vertices, 3)
             pygame.draw.line(self.game.screen, (100, 180, 255),
                              self.pt1, mouse_x_y, 1)
Exemplo n.º 27
0
 def draw(self):
     Tool.draw(self)
     # Draw a triangle from pt1 to mouse
     if self.pt1 is not None:
         mouse_x_y = tuple_to_int(pygame.mouse.get_pos())
         if mouse_x_y[0] != self.pt1[0] or mouse_x_y[1] != self.pt1[1]:
             self.vertices = constructTriangleFromLine(self.pt1, mouse_x_y)
             self.line_delta = [
                 mouse_x_y[0] - self.pt1[0], mouse_x_y[1] - self.pt1[1]
             ]
             pygame.draw.polygon(self.game.screen, (100, 180, 255),
                                 self.vertices, 3)
             pygame.draw.line(self.game.screen, (100, 180, 255), self.pt1,
                              mouse_x_y, 1)
Exemplo n.º 28
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)

        if pygame.mouse.get_pressed()[0]:
            body = find_body(self.game.world, event.pos)
            if body is not None:
                color = body.userData['color']
                point_pos = tuple_to_int(event.pos)
                self.constructor(point_pos, color)

                if not self.added_badge:
                    self.add_badge(icon='trophy-icon-physics',
                                   name='Isaac Newton',
                                   message='Congratulations! You just added a'
                                   ' Pen to your machine!')
                    self.added_badge = True
Exemplo n.º 29
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)

        if pygame.mouse.get_pressed()[0]:
            body = find_body(self.game.world, event.pos)
            if body is not None:
                color = body.userData['color']
                point_pos = tuple_to_int(event.pos)
                self.constructor(point_pos, color)

                if not self.added_badge:
                    self.add_badge(
                        icon='trophy-icon-physics',
                        name='Isaac Newton',
                        message='Congratulations! You just added a'
                                ' Pen to your machine!'
                    )
                    self.added_badge = True
Exemplo n.º 30
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if hasattr(event, 'button') and event.button == 1:
         if event.type == MOUSEBUTTONDOWN and self.vertices is None:
             self.vertices = [tuple_to_int(event.pos)]
             self.safe = False
         if not self.vertices:
             return
         if event.type == MOUSEBUTTONUP and self.vertices is not None and \
                 len(self.vertices) == 1 and \
                 tuple_to_int(event.pos)[0] == self.vertices[0][0] and \
                 tuple_to_int(event.pos)[1] == self.vertices[0][1]:
             if self.previous_vertices is not None:
                 last_x_y = self.previous_vertices[-1]
                 delta_x = last_x_y[0] - tuple_to_int(event.pos)[0]
                 delta_y = last_x_y[1] - tuple_to_int(event.pos)[1]
                 self.vertices = [[i[0] - delta_x, i[1] - delta_y]
                                  for i in self.previous_vertices]
                 self.safe = True
                 self.constructor(
                     self.vertices,
                     density=self.palette_data['density'],
                     restitution=self.palette_data['restitution'],
                     friction=self.palette_data['friction'])
             self.vertices = None
         elif (event.type == MOUSEBUTTONUP or
               event.type == MOUSEBUTTONDOWN):
             if self.vertices is None or (tuple_to_int(event.pos)[0] ==
                                          self.vertices[-1][0] and
                                          tuple_to_int(event.pos)[1] ==
                                          self.vertices[-1][1]):
                 # Skip if coordinate is same as last one
                 return
             if distance(tuple_to_int(event.pos), self.vertices[0]) < 15 \
                     and self.safe:
                 self.vertices.append(self.vertices[0])  # Connect polygon
                 self.constructor(
                     self.vertices,
                     density=self.palette_data['density'],
                     restitution=self.palette_data['restitution'],
                     friction=self.palette_data['friction'])
                 self.previous_vertices = self.vertices[:]
                 self.vertices = None
             elif distance(tuple_to_int(event.pos), self.vertices[0]) < 15:
                 self.vertices = None
             else:
                 self.vertices.append(tuple_to_int(event.pos))
                 if distance(tuple_to_int(event.pos),
                             self.vertices[0]) > 54:
                     self.safe = True
Exemplo n.º 31
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)

        # We handle two types of 'grab' depending on simulation running or not
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                # Give preference to pins and motors being caught
                for joint in self.game.world.world.joints:
                    x, y = joint.anchorA
                    ppm = self.game.world.ppm
                    x, y = self.game.world.to_screen((x * ppm, y * ppm))
                    wh_half = self.PIN_MOTOR_RADIUS
                    wh = 2 * self.PIN_MOTOR_RADIUS
                    rect = pygame.Rect(x - wh_half, y - wh_half, wh, wh)
                    if isinstance(joint, box2d.b2RevoluteJoint) \
                       and rect.collidepoint(tuple_to_int(event.pos)):
                        logging.debug("found a pin or motor")

                        self._moving_pm = joint
                        self.pm_mode_active = True
                        self.pm_x = x
                        self.pm_y = y
                        self.game.world.world.DestroyJoint(joint)

                        break

                if self.pm_mode_active:
                    # Game is stopped when moving pins and motors
                    # So that the game doesn't mess up, and for user
                    # convenience
                    self._game_run_prev_state = self.game.world.run_physics
                    self.game.world.run_physics = False

                    return
                    # Don't want to register the body under too

                # Grab the first object at the mouse pointer
                bodylist = self.game.world.get_bodies_at_pos(
                    tuple_to_int(event.pos),
                    include_static=False)
                if bodylist and len(bodylist) > 0:
                    if self.game.world.run_physics:
                        self.game.world.add.mouseJoint(bodylist[0],
                                                       tuple_to_int(event.pos))
                    else:
                        self._current_body = bodylist[0]
        elif event.type == MOUSEBUTTONUP:
            # Let it go
            if event.button == 1:
                if self.game.world.run_physics:
                    self.game.world.add.remove_mouseJoint()
                else:
                    if self.pm_mode_active:
                        pos = event.pos
                        body = find_body(self.game.world, pos)
                        if body is not None:
                            if self._moving_pm.enableMotor:
                                self.game.world.add.motor(
                                    body, pos,
                                    speed=MotorTool.palette_data['speed'])
                            else:
                                self.game.world.add.joint(body, pos)

                        # Check motor/pin and add joint accordingly

                        self.pm_mode_active = False
                        self._moving_pm = None

                        self.game.world.run_physics = self._game_run_prev_state

                    self._current_body = None
        elif event.type == MOUSEMOTION:  # and event.buttons[0]:
            # Move it around
            if self.game.world.run_physics:
                # Use box2D mouse motion
                self.game.world.mouse_move(tuple_to_int(event.pos))
            else:
                if self.pm_mode_active:
                    self.pm_x, self.pm_y = event.pos
                    return

                # Position directly (if we have a current body)
                if self._current_body is not None:
                    x, y = self.game.world.to_world(tuple_to_int(event.pos))
                    x /= self.game.world.ppm
                    y /= self.game.world.ppm
                    self._current_body.position = (x, y)
Exemplo n.º 32
0
 def draw(self):
     Tool.draw(self)
     if self.jb1:
         pygame.draw.line(self.game.screen, (100, 180, 255), self.jb1pos,
                          tuple_to_int(pygame.mouse.get_pos()), 3)
Exemplo n.º 33
0
 def draw(self):
     Tool.draw(self)
     if self.jb1:
         pygame.draw.line(self.game.screen, (100, 180, 255), self.jb1pos,
                          tuple_to_int(pygame.mouse.get_pos()), 3)
Exemplo n.º 34
0
 def draw(self):
     Tool.draw(self)
     if self.pm_mode_active:
         pygame.draw.circle(self.game.screen, (255, 255, 255),
                            tuple_to_int((self.pm_x, self.pm_y)),
                            self.PIN_MOTOR_RADIUS, 0)
Exemplo n.º 35
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)

        # We handle two types of 'grab' depending on simulation running or not
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                # Give preference to pins and motors being caught
                for joint in self.game.world.world.joints:
                    x, y = joint.anchorA
                    ppm = self.game.world.ppm
                    x, y = self.game.world.to_screen((x * ppm, y * ppm))
                    wh_half = self.PIN_MOTOR_RADIUS
                    wh = 2 * self.PIN_MOTOR_RADIUS
                    rect = pygame.Rect(x - wh_half, y - wh_half, wh, wh)
                    if isinstance(joint, box2d.b2RevoluteJoint) \
                       and rect.collidepoint(tuple_to_int(event.pos)):
                        logging.debug("found a pin or motor")

                        self._moving_pm = joint
                        self.pm_mode_active = True
                        self.pm_x = x
                        self.pm_y = y
                        self.game.world.world.DestroyJoint(joint)

                        break

                if self.pm_mode_active:
                    # Game is stopped when moving pins and motors
                    # So that the game doesn't mess up, and for user
                    # convenience
                    self._game_run_prev_state = self.game.world.run_physics
                    self.game.world.run_physics = False

                    return
                    # Don't want to register the body under too

                # Grab the first object at the mouse pointer
                bodylist = self.game.world.get_bodies_at_pos(
                    tuple_to_int(event.pos), include_static=False)
                if bodylist and len(bodylist) > 0:
                    if self.game.world.run_physics:
                        self.game.world.add.mouseJoint(bodylist[0],
                                                       tuple_to_int(event.pos))
                    else:
                        self._current_body = bodylist[0]
        elif event.type == MOUSEBUTTONUP:
            # Let it go
            if event.button == 1:
                if self.game.world.run_physics:
                    self.game.world.add.remove_mouseJoint()
                else:
                    if self.pm_mode_active:
                        pos = event.pos
                        body = find_body(self.game.world, pos)
                        if body is not None:
                            if self._moving_pm.enableMotor:
                                self.game.world.add.motor(
                                    body,
                                    pos,
                                    speed=MotorTool.palette_data['speed'])
                            else:
                                self.game.world.add.joint(body, pos)

                        # Check motor/pin and add joint accordingly

                        self.pm_mode_active = False
                        self._moving_pm = None

                        self.game.world.run_physics = self._game_run_prev_state

                    self._current_body = None
        elif event.type == MOUSEMOTION:  # and event.buttons[0]:
            # Move it around
            if self.game.world.run_physics:
                # Use box2D mouse motion
                self.game.world.mouse_move(tuple_to_int(event.pos))
            else:
                if self.pm_mode_active:
                    self.pm_x, self.pm_y = event.pos
                    return

                # Position directly (if we have a current body)
                if self._current_body is not None:
                    x, y = self.game.world.to_world(tuple_to_int(event.pos))
                    x /= self.game.world.ppm
                    y /= self.game.world.ppm
                    self._current_body.position = (x, y)
Exemplo n.º 36
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if hasattr(event, 'button') and event.button == 1:
         if event.type == MOUSEBUTTONDOWN and self.vertices is None:
             self.vertices = [tuple_to_int(event.pos)]
             self.safe = False
         if not self.vertices:
             return
         if event.type == MOUSEBUTTONUP and self.vertices is not None and \
                 len(self.vertices) == 1 and \
                 tuple_to_int(event.pos)[0] == self.vertices[0][0] and \
                 tuple_to_int(event.pos)[1] == self.vertices[0][1]:
             if self.previous_vertices is not None:
                 last_x_y = self.previous_vertices[-1]
                 delta_x = last_x_y[0] - tuple_to_int(event.pos)[0]
                 delta_y = last_x_y[1] - tuple_to_int(event.pos)[1]
                 self.vertices = [[i[0] - delta_x, i[1] - delta_y]
                                  for i in self.previous_vertices]
                 self.safe = True
                 self.constructor(
                     self.vertices,
                     density=self.palette_data['density'],
                     restitution=self.palette_data['restitution'],
                     friction=self.palette_data['friction'])
             self.vertices = None
         elif (event.type == MOUSEBUTTONUP
               or event.type == MOUSEBUTTONDOWN):
             if self.vertices is None or (tuple_to_int(
                     event.pos)[0] == self.vertices[-1][0] and tuple_to_int(
                         event.pos)[1] == self.vertices[-1][1]):
                 # Skip if coordinate is same as last one
                 return
             if distance(tuple_to_int(event.pos), self.vertices[0]) < 15 \
                     and self.safe:
                 self.vertices.append(self.vertices[0])  # Connect polygon
                 self.constructor(
                     self.vertices,
                     density=self.palette_data['density'],
                     restitution=self.palette_data['restitution'],
                     friction=self.palette_data['friction'])
                 self.previous_vertices = self.vertices[:]
                 self.vertices = None
             elif distance(tuple_to_int(event.pos), self.vertices[0]) < 15:
                 self.vertices = None
             else:
                 self.vertices.append(tuple_to_int(event.pos))
                 if distance(tuple_to_int(event.pos),
                             self.vertices[0]) > 54:
                     self.safe = True