ATTENTION: CE PROGRAMME EXIGE TOUTE LA MEMOIRE DE LA CALCULATRICE (donc supprimer tous les autres programmes python présents avant de l’injecter).
Le jeu de la vie de Conway. Essayez gameoflife(). Pour changer la configuration initiale, allez dans #initialsetup est choisissez les cellules en vie avec live(x,y). Noter que 0,0 est le coin haut gauche et que une cellule représente 20 pixels. live(20,0) rend vivante la deuxième cellule en partant du haut gauche et en comptant de gauche à droite.
from math import * import kandinsky import time global tsize global screen tsize=10 white=kandinsky.color(255,255,255) dark=kandinsky.color(0,0,0) screen=[] #upperleftcorner 0,0 def die(x,y): for cx in range(tsize): for cy in range(tsize): kandinsky.set_pixel(cx+x,cy+y,dark) def live(x,y): for cx in range(tsize): for cy in range(tsize): kandinsky.set_pixel(cx+x,cy+y,white) def isalive(x,y): if kandinsky.get_pixel(x,y)==dark: return 0 else: return 1 def resetscreen(): for loopx in range(320): for loopy in range(201): kandinsky.set_pixel(loopx,loopy,dark) def surround(x,y): s=0 #surroundofacell if isalive(x-tsize,y+tsize)==1: s+=1 if isalive(x,y+tsize)==1: s+=1 if isalive(x+tsize,y+tsize)==1: s+=1 if isalive(x+tsize,y)==1: s+=1 if isalive(x+tsize,y-tsize)==1: s+=1 if isalive(x,y-tsize)==1: s+=1 if isalive(x-tsize,y-tsize)==1: s+=1 if isalive(x-tsize,y)==1: s+=1 return s #rules def compute(): for cx in range(320/tsize): for cy in range(200/tsize): sur=surround(cx*tsize,cy*tsize) if isalive(cx*tsize,cy*tsize)==1: if sur>=2 and sur<=3: screen[cy][cx]=1 if sur<2 or sur>3: screen[cy][cx]=0 if isalive(cx*tsize,cy*tsize)==0: if sur==3: screen[cy][cx]=1 if sur!=3: screen[cy][cx]=0 def show(): for cx in range(320/tsize): for cy in range(200/tsize): if screen[cy][cx]==1: live(cx*tsize,cy*tsize) if screen[cy][cx]==0: die(cx*tsize,cy*tsize) def gameoflife(t=0): global screen screen=[] resetscreen() #initialsetup live(0,10) live(10,20) live(20,20) live(20,10) live(20,0) #endsetup gen=0 #creatingscreenmatrix #changeoftsizeimplies #changingthisstupidbug for cy in range(200/tsize): screen.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) while 1: kandinsky.draw_string("gen:%s"%gen,130,201,) time.sleep(t) kandinsky.draw_string("processing",220,206) compute() show() kandinsky.draw_string(" done",220,206) gen+=1 screen=[]