Пример #1
0
 def ball_update(self):
     for a in range(0, len(self.all_balls) - 1):
         for b in range(a + 1, len(self.all_balls)):
             ball, next_ball = self.all_balls[a], self.all_balls[b]
             delta = next_ball.coords - ball.coords
             if (next_ball.coords - ball.coords).length <= ball.RADIUS * 2:
                 if ball.velocity.length > 0 and \
                         next_ball.velocity.length > 0:
                     ball.coords += Vec2D.normalized(delta) *\
                         (delta.length - ball.RADIUS * 2)
                     next_ball.coords += Vec2D.normalized(-delta) *\
                         (delta.length - ball.RADIUS * 2)
                     self.ball_collision(ball, next_ball)
                 elif ball.velocity.length > 0:
                     if isinstance(ball, balls.WhiteBall):
                         self.hitted_balls.append(next_ball)
                     ball.coords += Vec2D.normalized(delta) *\
                         (delta.length - ball.RADIUS * 2)
                     self.ball_collision(ball, next_ball)
                 elif next_ball.velocity.length > 0:
                     if isinstance(next_ball, balls.WhiteBall):
                         self.hitted_balls.append(ball)
                     next_ball.coords += Vec2D.normalized(-delta) *\
                         (delta.length - ball.RADIUS * 2)
                     self.ball_collision(ball, next_ball)
Пример #2
0
 def ball_update(self):
     for a in range(0, len(self.all_balls)-1):
         for b in range(a+1, len(self.all_balls)):
             ball, next_ball = self.all_balls[a], self.all_balls[b]
             delta = next_ball.coords - ball.coords
             if (next_ball.coords - ball.coords).length <= ball.RADIUS * 2:
                 if ball.velocity.length > 0 and \
                         next_ball.velocity.length > 0:
                     ball.coords += Vec2D.normalized(delta) *\
                         (delta.length - ball.RADIUS * 2)
                     next_ball.coords += Vec2D.normalized(-delta) *\
                         (delta.length - ball.RADIUS * 2)
                     self.ball_collision(ball, next_ball)
                 elif ball.velocity.length > 0:
                     if isinstance(ball, balls.WhiteBall):
                         self.hitted_balls.append(next_ball)
                     ball.coords += Vec2D.normalized(delta) *\
                         (delta.length - ball.RADIUS * 2)
                     self.ball_collision(ball, next_ball)
                 elif next_ball.velocity.length > 0:
                     if isinstance(next_ball, balls.WhiteBall):
                         self.hitted_balls.append(ball)
                     next_ball.coords += Vec2D.normalized(-delta) *\
                         (delta.length - ball.RADIUS * 2)
                     self.ball_collision(ball, next_ball)
Пример #3
0
 def pocket_walls_collision(self, start_point, end_point):
     incoming_vec = self.coords - start_point
     pocket_wall_vec = end_point - start_point
     projected = Vec2D.projection(incoming_vec, pocket_wall_vec)
     distance = Vec2D.get_distance(incoming_vec, projected)
     if 0 <= math.fabs(projected.x) <=\
             math.fabs(end_point.x - start_point.x) and\
             0 <= math.fabs(projected.y) <=\
             math.fabs(end_point.y - start_point.y) and\
             distance < self.RADIUS:
         self.coords = Vec2D.normalized(incoming_vec - projected) *\
             self.RADIUS + start_point + projected
         self.velocity = 2 * (self.velocity.dot(pocket_wall_vec)
                              / pocket_wall_vec.dot(pocket_wall_vec)) *\
             pocket_wall_vec - self.velocity
 def pocket_walls_collision(self, start_point, end_point):
     incoming_vec = self.coords - start_point
     pocket_wall_vec = end_point - start_point
     projected = Vec2D.projection(incoming_vec, pocket_wall_vec)
     distance = Vec2D.get_distance(incoming_vec, projected)
     if 0 <= math.fabs(projected.x) <=\
             math.fabs(end_point.x - start_point.x) and\
             0 <= math.fabs(projected.y) <=\
             math.fabs(end_point.y - start_point.y) and\
             distance < self.RADIUS:
         self.coords = Vec2D.normalized(incoming_vec - projected) *\
             self.RADIUS + start_point + projected
         self.velocity = 2 * (self.velocity.dot(pocket_wall_vec)
                              / pocket_wall_vec.dot(pocket_wall_vec)) *\
             pocket_wall_vec - self.velocity
 def cue_handler(self):
     start_pos, end_pos = self.cue.get_cue_pos(self.white_ball.coords)
     self.painter.cue_draw(self.cue, start_pos, end_pos)
     keys = pygame.key.get_pressed()
     if keys[pygame.K_KP_ENTER]:
         new_velocity = Vec2D.normalized(start_pos - end_pos)
         force = Vec2D(self.white_ball.coords - start_pos).length
         self.white_ball.velocity = new_velocity * force ** 2 / MIN_HITTING_FORCE
         self.hit = True
Пример #6
0
 def cue_handler(self):
     start_pos, end_pos = self.cue.get_cue_pos(self.white_ball.coords)
     self.painter.cue_draw(self.cue, start_pos, end_pos)
     keys = pygame.key.get_pressed()
     if keys[pygame.K_KP_ENTER]:
         new_velocity = Vec2D.normalized(start_pos - end_pos)
         force = Vec2D(self.white_ball.coords - start_pos).length
         self.white_ball.velocity = new_velocity *\
             force ** 2 / MIN_HITTING_FORCE
         self.hit = True
Пример #7
0
 def move(self, pockets):
     check = Vec2D(self.velocity)
     self.velocity -= Vec2D.normalized(self.velocity) * FRICTION * TIMER
     self.coords += self.velocity * TIMER
     if check.x * self.velocity.x < 0 or check.y * self.velocity.y < 0:
         self.velocity = Vec2D(0, 0)
     if int(self.coords.x) not in range(139, 534) and\
             int(self.coords.x) not in range(564, 960)\
             and int(self.coords.y) not in range(90, 484):
         """Ако някоя от топките е в обсега на някой от джобовете
         влизаме в този if и почваме да следим за сблъсък със стената на
         някой от джобовете или за попадение в джоба.
         """
         for pocket in pockets:
             self.pocket_walls_collision(pockets[pocket][1],
                                         pockets[pocket][2])
             self.pocket_walls_collision(pockets[pocket][3],
                                         pockets[pocket][4])
             if (self.coords - pockets[pocket][0]).length <= POCKET_R:
                 self.vizibility = False
                 self.coords = Vec2D(1000, 1000) + self.pos
                 self.velocity = Vec2D(0, 0)
                 self.is_potted = True
     else:
         if self.coords.x < LEFT_BORDER or self.coords.x > RIGHT_BORDER:
             if self.coords.x < LEFT_BORDER:
                 self.coords.x = LEFT_BORDER
             else:
                 self.coords.x = RIGHT_BORDER
             self.velocity.x = -self.velocity.x
         elif self.coords.y < UP_BORDER or self.coords.y > DOWN_BORDER:
             if self.coords.y < UP_BORDER:
                 self.coords.y = UP_BORDER
             else:
                 self.coords.y = DOWN_BORDER
             self.velocity.y = -self.velocity.y
 def move(self, pockets):
     check = Vec2D(self.velocity)
     self.velocity -= Vec2D.normalized(self.velocity) * FRICTION * TIMER
     self.coords += self.velocity * TIMER
     if check.x * self.velocity.x < 0 or check.y * self.velocity.y < 0:
         self.velocity = Vec2D(0, 0)
     if int(self.coords.x) not in range(139, 534) and\
             int(self.coords.x) not in range(564, 960)\
             and int(self.coords.y) not in range(90, 484):
         """Ако някоя от топките е в обсега на някой от джобовете
         влизаме в този if и почваме да следим за сблъсък със стената на
         някой от джобовете или за попадение в джоба.
         """
         for pocket in pockets:
             self.pocket_walls_collision(pockets[pocket][1],
                                         pockets[pocket][2])
             self.pocket_walls_collision(pockets[pocket][3],
                                         pockets[pocket][4])
             if (self.coords - pockets[pocket][0]).length <= POCKET_R:
                 self.vizibility = False
                 self.coords = Vec2D(1000, 1000) + self.pos
                 self.velocity = Vec2D(0, 0)
                 self.is_potted = True
     else:
         if self.coords.x < LEFT_BORDER or self.coords.x > RIGHT_BORDER:
             if self.coords.x < LEFT_BORDER:
                 self.coords.x = LEFT_BORDER
             else:
                 self.coords.x = RIGHT_BORDER
             self.velocity.x = -self.velocity.x
         elif self.coords.y < UP_BORDER or self.coords.y > DOWN_BORDER:
             if self.coords.y < UP_BORDER:
                 self.coords.y = UP_BORDER
             else:
                 self.coords.y = DOWN_BORDER
             self.velocity.y = -self.velocity.y