jeu_de_la_vie.py

Created by marius-layan

Created on May 31, 2024

2.39 KB

Le jeu de la Vie est un « jeu à zéro joueur ». Il s’agit d’un automate cellulaire, qui à 3 règles exécutées à chaque frame.

Les cellules peuvent prendre deux états distincts : « vivante » ou « morte » (blanc et noir).

• si une cellule a exactement trois voisines vivantes, elle est vivante à l’étape suivante.

• si une cellule a exactement deux voisines vivantes, elle reste dans son état actuel à l’étape suivante.

• si une cellule a strictement moins de deux ou strictement plus de trois voisines vivantes, elle est morte à l’étape suivante.

La fonction aleatoire() permet de jouer avec un des cellules défini vivantes aléatoirement.

La fonction dessin permet de choisir ses propres cellules vivante avec [EXE].


from random import *
from kandinsky import *
from ion import *
from time import *
def affiche():
  if l[i][j]==1:fill_rect(j*6,i*6,6,6,'black')
  if l[i][j]==0:fill_rect(j*6,i*6,6,6,'white')
def dessine():
  global l,l1,i,j
  for i in range(len(l)):
    for j in range(len(l[0])):affiche()
  while keydown(KEY_EXE):continue
  c=[0,0]
  fill_rect(c[0]*6,c[1]*6,6,6,'green')
  while not keydown(KEY_EXE):
    if keydown(KEY_RIGHT) and c[0]!=53:
      c[0]+=1
      for i in range(len(l)):
        for j in range(len(l[0])):affiche()
      fill_rect(c[0]*6,c[1]*6,6,6,'green')
      while keydown(KEY_RIGHT):continue
    if keydown(KEY_LEFT) and c[0]!=0:
      c[0]-=1
      for i in range(len(l)):
        for j in range(len(l[0])):affiche()
      fill_rect(c[0]*6,c[1]*6,6,6,'green')
      while keydown(KEY_LEFT):continue
    if keydown(KEY_UP) and c[1]!=0:
      c[1]-=1
      for i in range(len(l)):
        for j in range(len(l[0])):affiche()
      fill_rect(c[0]*6,c[1]*6,6,6,'green')
      while keydown(KEY_UP):continue
    if keydown(KEY_DOWN) and c[1]!=37:
      c[1]+=1
      for i in range(len(l)):
        for j in range(len(l[0])):affiche()
      fill_rect(c[0]*6,c[1]*6,6,6,'green')
      while keydown(KEY_DOWN):continue
    if keydown(KEY_OK):
      if l[c[1]][c[0]]==0:l[c[1]][c[0]],l1[c[1]][c[0]]=1,1
      else:l[c[1]][c[0]],l1[c[1]][c[0]]=0,0
      while keydown(KEY_OK):continue
      for i in range(len(l)):
        for j in range(len(l[0])):affiche()
  main()
def create():
  global l,l1
  l=[[0 for i in range(54)] for i in range(38)]
  l1=[[0 for i in range(54)] for i in range(38)]
def aleatoire():
  global l,l1,i,j
  create()
  for i in range(len(l)):
    for j in range(len(l[0])):
      if randint(0,5)==0:l[i][j],l1[i][j]=1,1
      affiche()
  main()
def dessin():
  create()
  dessine()
def main():
  global v,i,j
  while keydown(KEY_EXE):continue
  while True:
    if keydown(KEY_EXE):
      while keydown(KEY_EXE):continue
      dessine()
    for i in range(len(l)):
      for j in range(len(l[0])):
        affiche()
        v=0
        if l[i][j]==1:v-=1
        for a in range(3):
          for b in range(3):
            if i not in (0,37) and j not in (0,53):
              if l[i+a-1][j+b-1]==1:v+=1
        if l[i][j]==0 and v==3:l1[i][j]=1
        elif l[i][j]==1 and v in (2,3):continue
        else:l1[i][j]=0
    for i in range(len(l)):
      for j in range(len(l[0])):l[i][j]=l1[i][j]