conway_final.py

Created by florian-allard

Created on July 25, 2020

6.76 KB


from kandinsky import draw_string,set_pixel,fill_rect
from random import randint
from ion import *

def menu(chaine,var,dec,mode,choix1,choix2):
  draw_string(chaine,10,v,'orange')
  draw_string("<"+" "*(5+dec)+">",10+len(chaine)*10,v,'blue')
  while not (keydown(KEY_EXE) or keydown(KEY_OK)):
#    if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
      if mode=="nombre":
        var = (var-mini+keydown(KEY_RIGHT)-keydown(KEY_LEFT))%(maxi-mini+1)+mini
        draw_string(" "+str(var)+" px ",10+(len(chaine)+1)*10+5*dec-5*len(str(var)),v,'purple')
      else:#mode booléen
        draw_string(choix1*var+choix2*(1-var),10+(len(chaine)+2)*10,v,'purple')
        if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
          var = not var
      while keydown(KEY_LEFT) or keydown(KEY_RIGHT):True
  while keydown(KEY_EXE) or keydown(KEY_OK):True
  return var


def curseur(couleur):
  for pos in [0,1]:
    fill_rect(dg+d*col+pos*(c+(s==3))+(s==1)+(s==2)-(s==3),dh+d*lig-2*(s==0)+5*(s==1)+(s==2)-(s==3),1,c+(s==3),couleur)
    fill_rect(dg+d*col+(s==1)+(s==2)-(s==3),dh+d*lig+pos*(c-(s==1)+(s==3))-2*(s==0)+5*(s==1)+(s==2)-(s==3),1+c+(s==3),1,couleur)
  return None

def carre(i,j,couleur):
  if rapide:
    for u in D:
      for v in D:
        set_pixel(dg+d*j+u,dh+d*i+v,couleur)
  else:
    fill_rect(dg+d*j,dh+d*i,c,c,couleur)

v=20
S = ['o',chr(215),chr(176),' ']
s = 0
draw_string("Choix du symbole : ",10,v,'orange')
draw_string("<   >",10+19*10,v,'blue')
draw_string(S[s],10+21*10,v,'purple')
while not (keydown(KEY_EXE) or keydown(KEY_OK)):
  if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
    s = (s+keydown(KEY_RIGHT)-keydown(KEY_LEFT))%len(S)
    draw_string(S[s],10+21*10,v+4*(s==2),'purple')
    if s == 3:
      fill_rect(10+21*10+3,v+7,4,4,'purple')
    while keydown(KEY_LEFT) or keydown(KEY_RIGHT):True
while keydown(KEY_EXE) or keydown(KEY_OK):True


c=9-2*s
mini=(s+2)%4+1
  
if s == 3:
  v+=20
  rapide=True #ajouter menu de choix de vitesse
  rapide=menu("Execution : ",rapide,3,"booleen","rapide","fluide")
  v+=20
  c=3
  mini=1
  maxi=15
  c=menu("Taille des cellules : ",c,2,"nombre","","")
  mini=max(0,5-c) #espacement mini

maxi=9

v+=20
e = 5
e=menu("Espacement : ",e,1,"nombre","","")

v+=20
alea = True
alea=menu("Initialisation : ",alea,6,"booleen","aléatoire","à choisir")

v+=20
clr = True
clr=menu("Vieillissement : ",clr,7,"booleen","en couleur","monochrome")

Naissance = [3]
Survie = [2,3]
for etape in [Naissance,Survie]:
  v+=20
  draw_string("Voisins pour "+"une naissance : "*(etape==Naissance)+"la survie : "*(etape==Survie),10,v,'orange')
  for n in range(9):
    draw_string(str(n),30+30*n,v+15,'purple'*(n in etape)+'grey'*(n not in etape))
  pos = 3
  while not (keydown(KEY_EXE) or keydown(KEY_OK)):
    if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
      for k in [-1,1]:
        draw_string(" ",30+30*pos+10*k,v+15)
      pos = (pos+(keydown(KEY_RIGHT)-keydown(KEY_LEFT)))%9
    draw_string("[",30+30*pos-10,v+15,'blue')
    draw_string("]",30+30*pos+10,v+15,'blue')
      #while keydown(KEY_LEFT) or keydown(KEY_RIGHT):True
    if keydown(KEY_UP) or keydown(KEY_DOWN):
      if pos in etape:
        etape.remove(pos)
      else:
        etape.append(pos)
      draw_string(str(pos),30+30*pos,v+15,'purple'*(pos in etape)+'grey'*(pos not in etape))
      while keydown(KEY_UP) or keydown(KEY_DOWN):True
  while keydown(KEY_EXE) or keydown(KEY_OK):True
  etape.sort()
  etape.reverse()
  v+=20

v+=20
N = 10
draw_string("Nombre de cycles : ",10,v,'orange')
draw_string("<"+5*" "+">",10+19*10,v,'blue')
draw_string(str(N),10+21*10,v,'purple')
while not (keydown(KEY_EXE) or keydown(KEY_OK)):
  if keydown(KEY_LEFT) or keydown(KEY_RIGHT) or keydown(KEY_UP) or keydown(KEY_DOWN):
    N = (N-1+(10*keydown(KEY_RIGHT)-10*keydown(KEY_LEFT)+keydown(KEY_UP)-keydown(KEY_DOWN)))%999+1
    draw_string(" "+str(N)+" ",10+20*10+5*(3-len(str(N))),v,'purple')
while keydown(KEY_EXE) or keydown(KEY_OK):True

d = c+e
D = range(d-e)
a = 222//d #222//6=36
b = 320//d #320//6=53
A = range(a)
B = range(b)
dg = (320-b*d+e)//2-2*(s==1)-(s==2)
dh = (222-a*d+e)//2-5*(s==1)-(s==2)+(s==0)
Liste = [[0 for col in B] for lig in A]
fill_rect(1,1,320,222,'white')

if alea:
  for lig in A:
    for col in B:
      Liste[lig][col] = 2*randint(0,1)
      if Liste[lig][col] == 2:
        if s<3:
          draw_string(S[s],dg+d*col,(dh+d*lig)*(1-2*(s==0))+207*(s==0),'blue')
        else:
          carre(lig,col,'blue')
else:
  col = b//2
  lig = a//2
  while not keydown(KEY_SHIFT):
    curseur('orange')
    if keydown(KEY_RIGHT) or keydown(KEY_LEFT) or keydown(KEY_UP) or keydown(KEY_DOWN):        
      curseur('white')
      lig = (lig+keydown(KEY_DOWN)-keydown(KEY_UP))%a 
      col = (col+keydown(KEY_RIGHT)-keydown(KEY_LEFT))%b
      if s==3 and e==0:
        for rangl in [-2,-1,0,1,2]:
          for rangc in [-2,-1,0,1,2]:
            if Liste[(lig+rangl)%a][(col+rangc)%b]>0:
              carre(lig+rangl,col+rangc,'blue')

    if keydown(KEY_EXE):
      if s == 0:
        Liste[a-1-lig][col] = 2-Liste[a-1-lig][col]
        couleur = 'white'*(Liste[a-1-lig][col] == 0)+'blue'*(Liste[a-1-lig][col] > 0)
      else: 
        Liste[lig][col] = 2-Liste[lig][col]
        couleur = 'white'*(Liste[lig][col] == 0)+'blue'*(Liste[lig][col] > 0)
      if s<3:
        draw_string(S[s],dg+d*col,dh+d*lig-7*(s==0),couleur)
        if s == 0 and e<5 and lig>0:
          for rang in range(a-lig,a):
            if Liste[rang][col]==0:break
            draw_string(S[s],dg+d*col,dh+d*(a-rang-1)-7*(s==0),'blue')
        if s == 1 and e<5:
          for rang in range(lig+1,a):
            if Liste[rang][col]==0:break
            draw_string(S[s],dg+d*col,dh+d*rang,'blue')
        if s == 2:
          if e<3:
            for rangc in range(col,a):
                for rangl in range(lig,a):
                  if Liste[rangl][rangc]>0: 
                    draw_string(S[s],dg+d*rangc,dh+d*rangl,'blue')
          else:
            for rang in range(lig+1,a):
              if Liste[rang][col]>0:
                draw_string(S[s],dg+d*col,dh+d*rang,'blue')
              
      else:
        carre(lig,col,couleur)
    while keydown(KEY_EXE):True
curseur('white')

for gen in range(N):
  for i in A:
    for j in B:
      Voisins = 0
      for k in [-1,0,1]:
        for l in [-1,0,1]:
          if [k,l] != [0,0] and Liste[(i+k)%a][(j+l)%b]**2 > 3:
            Voisins += 1
      if (Liste[i][j]==0 and Voisins in Naissance) or ( Liste[i][j] > 0 and Voisins in Survie ):
        Liste[i][j] += 1
        couleur = (clr*min(25*Liste[i][j]-25,255),0,max(0,255+clr*(25-25*Liste[i][j])))
      else:
        Liste[i][j] *= -1
        couleur = 'white'
      if Liste[i][j] != 0:
        if s<3:
          draw_string(S[s],dg+d*j,(dh+d*i)*(1-2*(s==0))+207*(s==0),couleur)
        else:
          carre(i,j,couleur)
  for i in A:
    for j in B:
      Liste[i][j] = 2*((max(0,Liste[i][j])+1)//2)

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.