jdv.py

Created by ississkerrouche67

Created on January 17, 2026

3.35 KB


from ion import *
from random import randint
from kandinsky import fill_rect
from time import sleep
def init_vide(n,m):
    ls = []
    for i in range(n):
        ls.append([])
        for j in range(m):
            ls[i].append(0)
    return ls

def init_aleatoire(n,m,p):
    ls = []
    p = p*100
    for i in range(n):
        ls.append([])
        for j in range(m):
            if p>randint(1,100):
                ls[i].append(1)
            else : 
                ls[i].append(0)
    return ls

def affichage_diff(old, new):
    for i in range(len(new)):
        for j in range(len(new[0])):
            if old[i][j] != new[i][j]:
                couleur = (255,255,255) if new[i][j] else (0,0,0)
                fill_rect(j*taille, i*taille+1, taille, taille, couleur)

        

def voisin(ls, x, y):
    v = 0
    for dx in (-1, 0, 1):
        for dy in (-1, 0, 1):
            if dx != 0 or dy != 0:
                nx = x + dx
                ny = y + dy
                if 0 <= nx < len(ls) and 0 <= ny < len(ls[0]):
                    v += ls[nx][ny]
    return v

def edition(ls):
    global cx, cy

    while True:
        affichage_monde(ls)
        draw_cursor(ls, cx, cy)

        if keydown(KEY_UP) and cx > 0:
            cx -= 1
            while keydown(KEY_UP): pass

        if keydown(KEY_DOWN) and cx < len(ls)-1:
            cx += 1
            while keydown(KEY_DOWN): pass

        if keydown(KEY_LEFT) and cy > 0:
            cy -= 1
            while keydown(KEY_LEFT): pass

        if keydown(KEY_RIGHT) and cy < len(ls[0])-1:
            cy += 1
            while keydown(KEY_RIGHT): pass

        if keydown(KEY_EXE):
            ls[cx][cy] = 1 - ls[cx][cy]
            while keydown(KEY_EXE): pass

        if keydown(KEY_OK):
            while keydown(KEY_OK): pass
            break


def draw_cursor(ls, x, y):
    if ls[x][y] == 0:
        couleur = (255, 0, 0)   
    else:
        couleur = (0, 255, 0) 

    fill_rect(y*taille, x*taille+1, taille, taille, couleur)

def monde_etendu(ls):
    lsres = init_vide(len(ls)+2,len(ls[0])+2)
    for i in range(len(ls)):
        for j in range(len(ls[i])):
            lsres[i+1][j+1]=ls[i][j]
    return lsres

def affichage_monde(ls):
    for i in range(len(ls)):
        for j in range(len(ls[i])):
            couleur = (255,255,255) if ls[i][j] else (0,0,0)
            fill_rect(j*taille, i*taille+1, taille, taille, couleur)

def pause():
    while keydown(KEY_OK):
        pass        
    while not keydown(KEY_OK):
        pass      
    while keydown(KEY_OK):
        pass         

    
n=0
resolution= 2
taille = 10 // resolution
NL=22*resolution
NC=32*resolution
cx = 0
cy = 0
delai = 0
gen=init_aleatoire(NL,NC, 0.15)
affichage_monde(gen)
while True:

    
   
    if keydown(KEY_MINUS):
        while keydown(KEY_MINUS): pass
        if delai<2:
            delai+=0.1
    if keydown(KEY_PLUS):
        while keydown(KEY_PLUS): pass
        if delai>=0.1:
            delai-=0.1    
    if keydown(KEY_OK):
        while keydown(KEY_OK): pass
        edition(gen)

    fgen = init_vide(NL, NC)

    for x in range(NL):
        for y in range(NC):
            v = voisin(gen, x, y)
            gp = gen[x][y]

            if v == 2:
                fgen[x][y] = gp
            elif v == 3:
                fgen[x][y] = 1
            else:
                fgen[x][y] = 0

    affichage_diff(gen, fgen)  
    gen = fgen

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.