Simulation du jeu de la vie, développé par le mathématicien John Conway (d’où le programme tire son nom). L’utilisateur peut entrer la disposition de départ des cellules en se servant des flèches, de le touche OK et la touche supprimer, et peut aussi personnaliser les graphismes et le jeu en appuyant sur la touche shift (en dessous des flèches).
from math import * from random import * from kandinsky import * from time import * from ion import * def add_figure(dim,printColor,bgColor,frameColor,speed,display): figure_index=menu(figures,"FIGURES",printColor,bgColor) pos=[dim[0][0]+(round(dim[1][0]/2)),dim[0][1]+(round(dim[1][1]/2))] while not keydown(4): if get_keys()!=set(): for i in range(0,4): if keydown(i): pos[i in [2,3]]=[-1,1][i%2] fill_rect(pos[0]+1,pos[1]+1,8,8,"gray") for i in range(figures[figure_index].count("-")): pass main(dim,printColor,bgColor,frameColor,speed,display) def settings(printColor,bgColor,frameColor,dim,speed): select=menu(["Random fill","Change text color","Change background color","Change frame Color","Change game size","Change speed","Reset","Back to game"],"SETTINGS",printColor,bgColor) if select==0: main(dim,printColor,bgColor,frameColor,speed,[choice("01") for i in range(dim[1][0]*dim[1][1])]) elif select==7: return printColor,bgColor,frameColor,dim,speed elif select==6: main(dim,printColor,bgColor,frameColor,speed) output=[printColor,bgColor,frameColor,dim,speed] output[select-1]=input("Please enter the new "+["text color","backround color","frame color","game size (like this:\n[[first cell in x,first cell in y]\n,[x width,y height]])","refresh rate\n(default: 0.25s)"][select-1]+":\n> ") if select==4:output[3]=eval(output[3]) return tuple(output) def draw_menu(choices,title,mode,printColor,bgColor): fill_rect(0,0,322,222,bgColor) draw_string(title,20,20,"red",bgColor) for i in range(len(choices)):draw_string(choices[i],30,60+20*i,(0+(125*(mode==i)),)*3,bgColor) def menu(choices,title,printColor,bgColor,select=0): draw_menu(choices,title,select,printColor,bgColor) while not keydown(KEY_OK): for i in range(0,4): if keydown(i): select+=([0,-1,1,0][i]) select%=len(choices) draw_menu(choices,title,select,printColor,bgColor) sleep(0.1) return select def draw(dim,printColor,bgColor,frameColor,display=None): draw_string("LOADING...",0,0,bgColor,frameColor,1) sleep(.5) draw_string("EDITING...",0,0,bgColor,frameColor,1) pos=[dim[0][0]+(round(dim[1][0]/2)),dim[0][1]+(round(dim[1][1]/2))] if display==None: display=["0" for i in range(dim[1][0]*dim[1][1])] fill_rect(0,0,320,222,frameColor) fill_rect(dim[0][0]*10,dim[0][1]*10,dim[1][0]*10,dim[1][1]*10,bgColor) else: display=list(display) run=True fill_rect(pos[0]*10+1,pos[1]*10+1,8,8,(220,)*3) while run: if keydown(KEY_SHIFT): printColor,bgColor,frameColor,dim,speed=settings(printColor,bgColor,frameColor,dim,.2) display=["0" for i in range(dim[1][0]*dim[1][1])] if get_keys()!=set(): fill_rect(pos[0]*10+1,pos[1]*10+1,8,8,get_pixel(pos[0]*10,pos[1]*10)) if keydown(4): fill_rect(pos[0]*10,pos[1]*10,10,10,printColor) display[(pos[0]-dim[0][0])+(pos[1]-dim[0][1])*dim[1][0]]="1" elif keydown(17): fill_rect(pos[0]*10,pos[1]*10,10,10,bgColor) try: display[(pos[0]-dim[0][0])+(pos[1]-dim[0][1])*dim[1][0]]="0" except IndexError: print("User tried to erase outside of frame") if keydown(KEY_ONOFF): run=False return "".join(display) if keydown(0):pos[0]-=1 elif keydown(3):pos[0]+=1 elif keydown(2):pos[1]+=1 elif keydown(1):pos[1]-=1 fill_rect(pos[0]*10+1,pos[1]*10+1,8,8,(220,)*3) sleep(.1) def main(dim=[[6,1],[20,20]],printColor="black",bgColor="white",frameColor="black",speed=.25,display=None): N=dim[1][0]*dim[1][1] if display==None: display=draw(dim,printColor,bgColor,frameColor) else: for i in range(N): fill_rect((dim[0][0]+i%dim[1][0])*10,(dim[0][1]+int(floor(i/dim[1][0])))*10,10,10,(bgColor if display[i]=="0" else printColor)) alive=True gen=0 draw_string("LOADING...",0,0,bgColor,frameColor,1) sleep(.5) while alive: if keydown(KEY_SHIFT): printColor,bgColor,frameColor,dim,speed=settings(printColor,bgColor,frameColor,dim,speed) fill_rect(0,0,320,222,frameColor) fill_rect(dim[0][0]*10,dim[0][1]*10,dim[1][0]*10,dim[1][1]*10,bgColor) elif keydown(KEY_ONOFF): display=draw(dim,printColor,bgColor,frameColor,display) elif keydown(4): add_figure(dim,printColor,bgColor,frameColor,speed,display) draw_string("GEN "+str(gen)+" \nCELLS:"+str(display.count("1")),0,0,bgColor,frameColor,1) display=new_gen(N,display,dim[1][0]) if display=="end": alive=False break for i in range(N): fill_rect((dim[0][0]+i%dim[1][0])*10,(dim[0][1]+int(floor(i/dim[1][0])))*10,10,10,(bgColor if display[i]=="0" else printColor)) alive=list(display).count("1") gen+=1 sleep(speed) if display!="end": draw_string("SPIECE DIED AT GEN "+str(gen),0,0,bgColor,frameColor,1) else: draw_string("STRUCTURES STABLES",0,0,bgColor,frameColor,1) sleep(.5) main(dim,printColor,bgColor,frameColor,speed,None) # Function to update cells def new_gen(N,disp,lenght): out="" disp+="0" # FOR EVERY ROW: for i in range(len(disp)-lenght-1): list=[disp[i-1-lenght],disp[i-lenght],disp[i+1-lenght],disp[i-1],"X",disp[i+1],disp[i-1+lenght],disp[i+lenght],disp[i+1+lenght]] # IF THE CELL IS A 1 if disp[i]=="1": out+=str(1*(2<=(list.count("1"))<=3)) # IF THE CELL IS A 0 else: out+=str(1*(list.count("1")==3)) for i in range(len(disp)-lenght,len(disp)): list=[disp[i-1-lenght],disp[i-lenght],disp[i+1-lenght],disp[i-1],"X",disp[(i+1)%(len(disp)-lenght)],disp[(i-1)%(len(disp)-lenght)],disp[(i)%(len(disp)-lenght)],disp[(i+1)%(len(disp)-lenght)]] # IF THE CELL IS A 1 if disp[i]=="1": out+=str(1*(2<=(list.count("1"))<=3)) # IF THE CELL IS A 0 else: out+=str(1*(list.count("1")==3)) if (out+"0")==disp: return "end" return out #111-000-101-101-101-000-111 #1111111-1000001-1001001-1011101-1001001-10000001-1111111 #0110-1001-1110 figures=["111-101-110","010-111-101"] main([[6,1],[20,20]],"black","white","black",.25)