#!/usr/bin/python import sys , os , random, controler from view import View from controler import Controller QUIT = 0 NB_BILLES = 2 WIDTH = 5 HEIGHT = 5 MAX_SPEED = 10 gen_id = 0 ### Model ### class Bille: def __init__(self,pos_x,pos_y,vector_x,vector_y): global gen_id self.id = gen_id gen_id += 1 self.x = pos_x self.y = pos_y self.vector_x = vector_x self.vector_y = vector_y def move(self): self.x += self.vector_x self.y += self.vector_y def detect_collision(self,others): for bille in others: if bille != self: if bille.x == self.x and bille.y == self.y: return bille return False def collision(self,other): tmp_vector_x = self.vector_x tmp_vector_y = self.vector_y self.vector_x = other.vector_x self.vector_y = other.vector_y other.vector_x = tmp_vector_x other.vector_y = tmp_vector_y print "collision ", self.id," ", other.id def next(self): return (self.x + self.vector_x , self.y + self.vector_y) def toString(self): print "bille :",self.id,"\npos_x =",self.x,"\npos_y =",self.y,"\nvect_x =",self.vector_x,"\nvect_y =",self.vector_y def main(): # init ctrl = Controller(WIDTH,HEIGHT) ctrl.create(NB_BILLES) # Initialize View view = View(WIDTH,HEIGHT) while not QUIT: for bille in billes: bille.move() # inclure un position de position suivant pour detecter un future collision tmp = bille.detect_collision(billes) if tmp: print "move" bille.collision(tmp) if bille.x <= 0 or bille.x >= WIDTH: bille.vector_x = -bille.vector_x if bille.y <= 0 or bille.y >= HEIGHT: bille.vector_y = -bille.vector_y view.showall(billes) view.update() main()