foule.py

Created by marius-layan

Created on February 05, 2024

1.71 KB

Petit programme pour simuler la foule. les personnes sont représentée par des carrées multicolores. Ils de déplace aléatoirement dans l’espace en suivant à chaque mouvement une direction durant un temps donné.

foule( n , t )

n = nombre de personnes

t = taille des personnes


from random import *
from kandinsky import *
from time import *
def foule(n,t):
  if t>25 or t<2:
    return "la taille doit etre entre 2 et 25"
  elif n<1 or n>100:
    return "le nombre doit etre entre 1 et 100"
  global l,a
  l=[]
  while len(l)!=n:
    l,a=[],0
    for i in range(n):
      x,y=randint(0,320-t),randint(0,222-t)
      for i in range(len(l)):
        if l[i][0]-t<x<l[i][0]+t and l[i][1]-t<y<l[i][1]+t:
          a=1;break
      if a==1:break
      l.append([x,y,0,0,(randint(0,200),randint(0,200),randint(0,200)),0])
  while True:
    fill_rect(0,0,320,222,'white')
    for i in range(len(l)):
      # compteur de la distance pour une direction
      if l[i][2]!=0:
        l[i][2]-=1
        # avence (en evitant les bordures)
        if -1<l[i][3]<2 or l[i][3]==7:
          l[i][0]+=l[i][5]
          if 318-t<l[i][0]<321-t:l[i][2]=0
        elif 2<l[i][3]<6:
          l[i][0]-=l[i][5]
          if -1<l[i][0]<2:l[i][2]=0
        if 4<l[i][3]<8:
          l[i][1]+=l[i][5]
          if 220-t<l[i][1]<223-t:l[i][2]=0
        elif 0<l[i][3]<4:
          l[i][1]-=l[i][5]
          if -1<l[i][1]<2:l[i][2]=0
      # affichage des personnes
      fill_rect(l[i][0],l[i][1],t,t,l[i][4])
      # probabilite de debut de deplacement en fonction de la taille
      if randint(0,200+t*20)==0:
        # longueur du deplacement / vitesse
        l[i][2],l[i][5]=randint(0,200),randint(1,2)
        # direction
        if 318-t<l[i][0]<321-t:l[i][3]=choice([3,4,5])
        elif -1<l[i][0]<2:l[i][3]=choice([0,1,7])
        elif 220-t<l[i][1]<223-t:l[i][3]=choice([1,2,3])
        elif -1<l[i][1]<2:l[i][3]=choice([5,6,7])
        else:l[i][3]=choice([0,1,2,3,4,5,6,7])
    # temps en fonction de la taile
    sleep(0.045-t/800)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.