From 8231f489c70f50197a8ca2c288736b78bcaabdfd Mon Sep 17 00:00:00 2001 From: Laurent Bernabé Date: Sat, 21 Sep 2013 09:11:29 +0000 Subject: Now there is a function in order to automatize the placement of a balls list so that they don't collide each other. --- diff --git a/balls_collision.py b/balls_collision.py index eed62ab..0aa8cf5 100644 --- a/balls_collision.py +++ b/balls_collision.py @@ -7,6 +7,7 @@ Created on Fri Sep 20 19:57:22 2013 """ from math import sqrt +from random import randint def are_colliding_balls(ball_1, ball_2): """ @@ -38,4 +39,26 @@ def manage_colliding_balls(balls_list): 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 + snd_ball.oppose_velocity_and_move() + +def place_balls(balls_list, balls_area): + """ + Places all the balls of balls_list, randomly, within balls_area + but assuring that balls don't collide each other. + balls_list : list of balls => List of Ball + balls_area : space where ball is allowed to be => Tuple of 4 integers ( + left side x, top side y, right side x, bottom side y) + """ + placed_balls = [] + for ball in balls_list: + while True: + ball_x = randint(balls_area[0], balls_area[2] + 1) + ball_y = randint(balls_area[1], balls_area[3] + 1) + ball.move_to((ball_x, ball_y)) + collides = False + for placed_ball in placed_balls: + curr_collision_state = are_colliding_balls(ball, placed_ball) + collides = collides or curr_collision_state + if not collides: + placed_balls.append(ball) + break diff --git a/main.py b/main.py index 5ed65d8..8971051 100644 --- a/main.py +++ b/main.py @@ -55,10 +55,7 @@ def main(): Operation(120, 240, OPER_ADD), balls_area, (1.7, -1.2))] - the_balls[0].move_to((430, 200)) - the_balls[1].move_to((400, 360)) - the_balls[2].move_to((200, 80)) - the_balls[3].move_to((330, 100)) + balls_collision.place_balls(the_balls, balls_area) while True: screen.fill(BACKGROUND) -- cgit v0.9.1