diff options
author | Laurent Bernabé <laurent.bernabe@gmail.com> | 2013-09-20 20:26:35 (GMT) |
---|---|---|
committer | Laurent Bernabé <laurent.bernabe@gmail.com> | 2013-09-20 20:26:35 (GMT) |
commit | 6a9d0db65529cd94155b9ec7abbfeefcc4e2ebfd (patch) | |
tree | da100614da4a1136d56761e08869e79837eb7b06 | |
parent | a664dd5a1ca7e878cc4ed0885dd3c8d6f8f7a441 (diff) |
Now the program manages balls collisions with themselves.
-rw-r--r-- | ball.py | 15 | ||||
-rw-r--r-- | balls_collision.py | 41 | ||||
-rw-r--r-- | main.py | 28 |
3 files changed, 76 insertions, 8 deletions
@@ -22,7 +22,8 @@ class Ball(object): velocity : current direction of move => A tuple of 2 floats (x,y) """ info = pygame.display.Info() - self._SCREEN_WIDTH, self._SCREEN_HEIGTH = info.current_w, info.current_h + self._SCREEN_WIDTH = info.current_w + self._SCREEN_HEIGHT = info.current_h self._txt_font = txt_font self._txt_color = txt_color self._bg_color = bg_color @@ -102,7 +103,15 @@ class Ball(object): self._center = (self._center[0] + self._velocity[0], self._center[1] + self._velocity[1]) elif (self._center[1] < radius - or self._center[1] > self._SCREEN_HEIGTH - radius): + or self._center[1] > self._SCREEN_HEIGHT - radius): self._velocity = (self._velocity[0], -self._velocity[1]) self._center = (self._center[0] + self._velocity[0], - self._center[1] + self._velocity[1])
\ No newline at end of file + self._center[1] + self._velocity[1]) + + def oppose_velocity_and_move(self): + """ + Alters the velocity, by multiplying each of its values + by -1, then move it from one step (calling move). + """ + self._velocity = (self._velocity[0] * -1, + self._velocity[1] * -1)
\ No newline at end of file diff --git a/balls_collision.py b/balls_collision.py new file mode 100644 index 0000000..eed62ab --- /dev/null +++ b/balls_collision.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +""" +Manages balls collisions +Created on Fri Sep 20 19:57:22 2013 + +@author: laurent-bernabe +""" + +from math import sqrt + +def are_colliding_balls(ball_1, ball_2): + """ + Says whether two balls collides. + ball_1 : ball to test => Ball + ball_2 : ball to test => Ball + => Boolean + """ + b1_x, b1_y = ball_1.get_center() + b2_x, b2_y = ball_2.get_center() + x_dist = abs(b1_x - b2_x) + y_dist = abs(b1_y - b2_y) + b1_radius = ball_1.get_diameter() / 2 + b2_radius = ball_2.get_diameter() / 2 + centers_dist = sqrt(x_dist**2 + y_dist**2) + return centers_dist <= (b1_radius + b2_radius) + +def manage_colliding_balls(balls_list): + """ + Detects all colliding balls couples of balls_list, + and, for each colliding balls couple, alter both ball + velocity and make a move for both. + balls_list : list of balls => List of Ball + """ + for fst_ball_index in range(len(balls_list[:-1])): + fst_ball = balls_list[fst_ball_index] + inner_range = range(fst_ball_index + 1, len(balls_list)) + for snd_ball_index in inner_range: + snd_ball = balls_list[snd_ball_index] + if (are_colliding_balls(fst_ball, snd_ball)): + fst_ball.oppose_velocity_and_move() + snd_ball.oppose_velocity_and_move()
\ No newline at end of file @@ -13,6 +13,7 @@ from pygame.locals import QUIT from ball import * from operation import * from elements_painter import * +import balls_collision def main(): @@ -31,20 +32,37 @@ def main(): font = PangoFont(family='Helvetica', size=16, bold=True) BLACK = (0, 0, 0) BLUE = (0, 0, 255) - the_ball = Ball(font, BLACK, BLUE, Operation(1000, 3000, OPER_MUL), - (2, 1.2)) - the_ball.move_to((140, 170)) + YELLOW = (255, 255, 0) + RED = (255, 0, 0) + GREEN = (0, 255, 0) + the_balls = [Ball(font, BLACK, BLUE, + Operation(1000, 3000, OPER_MUL), (2, 1.2)), + Ball(font, BLACK, YELLOW, + Operation(120, 45, OPER_SUB), (1.6,-0.4)), + Ball(font, BLACK, RED, + Operation(9, 3, OPER_DIV), (-0.8, 1.6)), + Ball(font, BLACK, GREEN, + Operation(120, 240, OPER_ADD), (1.7, -1.2))] + + + the_balls[0].move_to((140, 170)) + the_balls[1].move_to((400, 300)) + the_balls[2].move_to((200, 80)) + the_balls[3].move_to((330, 70)) while True: screen.fill(BACKGROUND) - paint_ball(the_ball, screen) + for ball in the_balls: + paint_ball(ball, screen) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() exit() pygame.display.update() clock.tick(FPS) - the_ball.move() + for ball in the_balls: + ball.move() + balls_collision.manage_colliding_balls(the_balls) if __name__ == "__main__": main() |