fromkandinskyimport*importrandomimporttimelengthOfScreen,widthOfScreen=320,200#the width isn't the real width of the screen because we need room to place the generation counter
definitMap(width,length,map):#init map randomly
foryinrange(width):forxinrange(length):map[y][x]=random.randint(0,1)defprintMap(width,length,map):# draw the map in an adaptive way to fit in the screen
squale=(320/length)squale=int(squale)beginingX,beginingY,limitX,limitY=0,0,squale,squale#size of each pixel
pX,pY=0,0#the pointers
foryinrange(width):forxinrange(length):ifmap[y][x]==1:forpXinrange(beginingX,limitX):forpYinrange(beginingY,limitY):set_pixel(pX,pY,color(0,0,0))elifmap[y][x]==0:forpXinrange(beginingX,limitX):forpYinrange(beginingY,limitY):set_pixel(pX,pY,color(255,255,255))beginingX=beginingX+squalelimitX=beginingX+squalebeginingY=beginingY+squalelimitY=beginingY+squalebeginingX=0defcleanMap():#earase everything on the screen
foryinrange(widthOfScreen):forxinrange(lengthOfScreen):set_pixel(x,y,color(255,255,255))deflogic(width,length,map):#aply the rules of the game
newMap=[[0forxinrange(length)]foryinrange(width)]foryinrange(width):forxinrange(length):num=[]ify==0andx==0:# en haut à gauche
num.append(map[y][x+1])num.append(map[y+1][x+1])num.append(map[y+1][x])ify==0andx==length-1:# en haut à droite
num.append(map[y+1][x])num.append(map[y+1][x-1])num.append(map[y][x-1])ify==width-1andx==length-1:# en bas à droite
num.append(map[y][x-1])num.append(map[y-1][x-1])num.append(map[y-1][x])ify==width-1andx==0:# en bas à gauche
num.append(map[y-1][x])num.append(map[y-1][x+1])num.append(map[y][x+1])elify==0andx!=0andx!=length-1:# première ligne
num.append(map[y][y+1])num.append(map[y+1][x+1])num.append(map[y+1][x])num.append(map[y+1][x-1])num.append(map[y][x-1])elifx==0andy!=0andy!=width-1:# première colonne
num.append(map[y-1][x])num.append(map[y-1][x+1])num.append(map[y][x+1])num.append(map[y+1][x+1])num.append(map[y+1][x])elify==width-1andx!=0andx!=length-1:# dernière ligne
num.append(map[y][x+1])num.append(map[y-1][x+1])num.append(map[y-1][x])num.append(map[y-1][x-1])num.append(map[y][x-1])elifx==length-1andy!=0andy!=width-1:# dernière colonne
num.append(map[y+1][x])num.append(map[y-1][x])num.append(map[y-1][x-1])num.append(map[y][x-1])num.append(map[y+1][x-1])elifx!=0andx!=length-1andy!=0andy!=width-1:num.append(map[y-1][x+1])num.append(map[y][x+1])num.append(map[y+1][x+1])num.append(map[y+1][x])num.append(map[y+1][x-1])num.append(map[y][x-1])num.append(map[y-1][x-1])num.append(map[y-1][x])livingCells=num.count(1)diedCells=num.count(0)iflivingCells==3:newMap[y][x]=1iflivingCells==2andmap[y][x]==1:newMap[y][x]=1eliflivingCells!=2andlivingCells!=3:newMap[y][x]=0foryinrange(width):forxinrange(length):map[y][x]=newMap[y][x]# merge the older vers with the new one
defstart(generations=8,length=32,width=20):#40*25
map=[[0forxinrange(length)]foryinrange(width)]initMap(width,length,map)printMap(width,length,map)draw_string("gen : 0",10,204)i=1whilei<=int(generations):time.sleep(1.16)logic(width,length,map)cleanMap()printMap(width,length,map)draw_string("gen : {}".format(i),10,204)i+=1start()
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
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.