jeu_de_la_vie.py

Created by elnix90

Created on March 15, 2023

8.38 KB

Le célèbre Jeu de la Vie de Conway créé en 1970.

Ce programme possède 3 fonctionnalités principales: -le mode aléatoire -le mode manuel -le mode prédéfini

Chacun de ses mode vous permet de placer différemment les cellules vivantes ou mortes.

Pour chaque mode, vous pouvez choisir une taille de cellules.

ATTENTION; en dessous de la taille 6, votre calculatrice risque de ne pas pouvoir calculer toutes les cellules, c’est pourquoi je vous conseille de ne choisir que les tailles 6 et supérieures.

Il n’est pas omis que j’optimise mon programme a l’avenir, donc si vous avez des suggestions, vous pouvez me contacter sur discord: Elnix90#016


from kandinsky import *
from ion import *
from random import *
from time import *
def menu():
  global mode,taille
  fill_rect(0,0,400,400,"black")
  draw_string("CHOIX DU MODE:",95,20,"orange","black")
  draw_string("  Aleatoire   Manuel   Predefini  ",-10,70,"green","black")
  draw_string("[",-2 ,70,"white","black")
  draw_string("]",102,70,"white","black")
  mode=1
  while not (keydown(KEY_EXE) or keydown(KEY_OK)):
    if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
      if keydown(KEY_LEFT):
        if mode<2:
          mode=3
        else:
          mode-=1
      elif keydown(KEY_RIGHT):
        if mode>2:
          mode=1
        else:
          mode+=1
      if mode==1:# Aleatoire
        draw_string("  Aleatoire   Manuel   Predefini  ",-10,70,"green","black")
        draw_string("[",-2, 70,"white","black")
        draw_string("]",102,70,"white","black")
        sleep(0.1)
      elif mode==2:# A choisir
        draw_string("  Aleatoire   Manuel   Predefini  ",-10,70,"green","black")
        draw_string("[",115,70,"white","black")
        draw_string("]",192,70,"white","black")
        sleep(0.1)
      elif mode==3:#Predefini
        draw_string("  Aleatoire   Manuel   Predefini  ",-10,70,"green","black")
        draw_string("[",207,70,"white","black")
        draw_string("]",313,70,"white","black")
        sleep(0.1)
  while keydown(KEY_EXE) or keydown(KEY_OK):True
  draw_string("TAILLE DES CELLULES:",65,130,"orange","black")
  taille=6
  draw_string("  1  2  3  4  5  6  7  8  9  ",17,170,"green","black")
  draw_string("[",21+30*(taille-1),170,"white","black")
  draw_string("]",21+30*(taille-1)+31,170,"white","black")
  while not (keydown(KEY_EXE) or keydown(KEY_OK)):
    if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
      if keydown(KEY_LEFT):
        taille-=1
        sleep(0.1)
        if taille<1:
          taille=9
      elif keydown(KEY_RIGHT):
        taille+=1
        sleep(0.1)
        if taille>9:
          taille=1
      draw_string("  1  2  3  4  5  6  7  8  9  ",17,170,"green","black")
      draw_string("[",21+30*(taille-1),170,"white","black")
      draw_string("]",21+30*(taille-1)+31,170,"white","black")
  if keydown(KEY_ZERO):
    taille=30
def generation():
  fill_rect(0,0,400,400,"black")
  draw_string("GENERATION",108,90,"blue","black")
  sleep(0.5)
  fill_rect(320//2-16,115,3,3,"blue")
  sleep(0.5)
  fill_rect(320//2-3,115,3,3,"blue")
  sleep(0.5)
  fill_rect(320//2+10,115,3,3,"blue")
  sleep(0.5)
def trace_aleatoire():
    chiant=0.01
    x=0
    y=0
    fill_rect(0,0,400,400,"black")
    for colonnes in range(320//taille):
      for lignes in range(222//taille):
        if randint(0,1)==0:
          fill_rect(x,y,taille,taille,"black")
        else:
          fill_rect(x,y,taille,taille,"white")
        y+=taille
      if keydown(KEY_EXE) or keydown(KEY_OK):
        chiant=-1
      sleep(chiant)
      x+=taille
      y=0
def choisir_info():
  fill_rect(0,0,400,400,"black")
  draw_string("UTILISEZ LES FLECHES POUR",32,20,"white","black")
  draw_string("VOUS DEPLACER",95,40,"white","black")
  draw_string("APPUYEZ SUR OK POUR POSER UN",20,80,"white","black")
  draw_string("UN CARRE",120,100,"white","black")
  draw_string("UTILISEZ EXE POUR VALIDER",35,140,"white","black")
  while keydown(KEY_EXE) or keydown(KEY_OK):True
  while not (keydown(KEY_EXE) or keydown(KEY_OK)):True
  fill_rect(0,0,400,400,"black")
def chargement():
  fill_rect(0,0,400,400,"black")
  draw_string("CHARGEMENT",108,90,"blue","black")
  for u in range(270):
    fill_rect(25,180,u,15,"green")
    sleep(0.003)
  sleep(0.3)
def choisir():
  fill_rect(0,0,400,400,"black")
  x=155
  y=106
  while not x%taille==0:
    x+=1
  while not y%taille==0:
    y+=1
  while 1:
    curseur(x,y)    
    if keydown(KEY_UP):
      fill_rect(x,y,taille,taille,couleur_xy)
      y-=taille
      if y<1:
        y=222//taille*taille-taille  
      while keydown(KEY_UP):True
    
    elif keydown(KEY_DOWN):
      fill_rect(x,y,taille,taille,couleur_xy)
      y+=taille
      if y>222//taille*taille:
        y=0
      while keydown(KEY_DOWN):True
    
    elif keydown(KEY_LEFT):
      fill_rect(x,y,taille,taille,couleur_xy)
      x-=taille
      if x<0:
        x=320//taille*taille-taille
      while keydown(KEY_LEFT):True
    
    elif keydown(KEY_RIGHT):
      fill_rect(x,y,taille,taille,couleur_xy)
      x+=taille
      if x>320//taille*taille:
        x=0
      while keydown(KEY_RIGHT):True
    
    elif keydown(KEY_OK):
      if get_pixel(x,y)==(0,0,0):
        fill_rect(x,y,taille,taille,"white")
      else:
        fill_rect(x,y,taille,taille,"black")
      while keydown(KEY_OK):True
    
    elif keydown(KEY_EXE):
      fill_rect(x,y,taille,taille,couleur_xy)
      while keydown(KEY_EXE):True
      break
def curseur(x,y):
  global couleur_xy
  couleur_xy=get_pixel(x,y)
  if taille-2<0:
    d=0
    e=0
  else:
    d=taille-2
    e=taille-2
  fill_rect(x+1,y+1,d,e,"blue")
def predefini():
  global shema
  while keydown(KEY_EXE) or keydown(KEY_OK):True
  fill_rect(0,0,400,400,"black")
  draw_string("CHOIX DE LA FIGURE:",70,40,"orange","black")
  draw_string("   < Remplissage >    ",53,110,"green","black")
  shema=1
  while not (keydown(KEY_EXE) or keydown(KEY_OK)):
    if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
      if keydown(KEY_LEFT):
        while keydown(KEY_LEFT):True
        shema-=1
        if shema<1:
          shema=3
      elif keydown(KEY_RIGHT):
        while keydown(KEY_RIGHT):True
        shema+=1
        if shema>3:
          shema=1
    if shema==1:
      draw_string("   < Remplissage >    ",53,110,"green","black")
    elif shema==2:
      draw_string("      < Canon >       ",53,110,"green","black")
    elif shema==3:
      draw_string("     < Symetrie >     ",53,110,"green","black")
def remplissage():
  fill_rect(0,0,320//taille*taille,222//taille*taille,"white")
def cannon():
  fill_rect(0,0,400,400,"black")
  x=155
  y=106
  while not x%taille==0:
    x+=1
  while not y%taille==0:
    y+=1
  fill_rect(x,y,taille,3*taille,"white")
  fill_rect(x-taille,y,taille,taille,"white")
  fill_rect(x-taille*2,y+taille,taille,taille,"white")
def symetrie():
  fill_rect(0,0,400,400,"black")
  x=115
  y=71
  while not x%taille==0:
    x+=1
  while not y%taille==0:
    y+=1
  fill_rect(x,y,3*taille,3*taille,"white")
  fill_rect(x+6*taille,y,3*taille,3*taille,"white")
  fill_rect(x,y+6*taille,3*taille,3*taille,"white")
  fill_rect(x+6*taille,y+6*taille,3*taille,3*taille,"white")

  fill_rect(x+taille,y+taille,taille,taille,"black")
  fill_rect(x+7*taille,y+taille,taille,taille,"black")
  fill_rect(x+taille,y+7*taille,taille,taille,"black")
  fill_rect(x+7*taille,y+7*taille,taille,taille,"black")
def couleur(x,y):
  global Blanc,Noir
  Blanc=0
  Noir =0
  Col_Carre=get_pixel(x,y)
  ajout(x-1       ,y-1)#       1
  ajout(x         ,y-1)#       2
  ajout(x+taille+1,y-1)#       3
  ajout(x-1       ,y)#         4
  ajout(x+taille+1,y)#         5
  ajout(x-1       ,y+taille+1)#6
  ajout(x         ,y+taille+1)#7
  ajout(x+taille+1,y+taille+1)#8
  if Blanc==3:
    return "white"
  elif Blanc==2 and Col_Carre==(248, 252, 248):
    return "white"
  else:
    return "black"
def ajout(x,y):
  global Noir,Blanc
  if get_pixel(x,y)==(0,0,0):
    Noir+=1
  else:
    Blanc+=1
liste_precedente=[""]
liste=[]
def ecrire():
  global liste,liste_precedente
  liste_precedente=liste
  liste=[]
  x=0
  for t in range(2):
    a=0
    b=0
    for colonnes in range(320//taille):
      for lignes in range(222//taille):
        if t==0:
          liste.append(couleur(a,b))
        else:
          fill_rect(a,b,taille,taille,liste[x])
          x+=1
        b+=taille
      a+=taille
      b=0
def test_fin_mort():
  if 1==2:
#  if "white" not in liste:
      fill_rect(0,0,400,400,"black")
      draw_string("TOUTES LES CELLULES SONT MORTES",4,90,"orange","black")
      draw_string("FIN DE LA SIMULATION",55,120,"orange","black")
      return True
def test_fin_fige():
  if liste_precedente==liste:
      fill_rect(0,0,400,400,"black")
      draw_string("TOUTES LES CELLULES SONT FIGEES",4,90,"orange","black")
      draw_string("FIN DE LA SIMULATION",55,120,"orange","black")
      return True
menu()
if mode==1:
  generation()
  trace_aleatoire()
elif mode==2:
  choisir_info()
  chargement()
  choisir()
elif mode==3:
  predefini()
  if shema==1:
    remplissage()
  elif shema==2:
    cannon()
  elif shema==3:
    symetrie()
while keydown(KEY_EXE) or keydown(KEY_OK):True
while not (keydown(KEY_EXE) or keydown(KEY_OK) or test_fin_fige() or test_fin_mort()):
  ecrire()

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.