Nov-25-2016, 07:51 PM
Hi there ..first program I have made in python is conway's game of life (see wikipedia).
I used Python 3.5, tkinter using commands enclosed and overlapping.
Maybe a bit primitive for most here but it works ! and fun to see.
If anyone has time to have a look at it , I would appreciate suggestions where to improve on writing/shortening.
Here it is. Apologies for the length (150 lines).
I used Python 3.5, tkinter using commands enclosed and overlapping.
Maybe a bit primitive for most here but it works ! and fun to see.
If anyone has time to have a look at it , I would appreciate suggestions where to improve on writing/shortening.
Here it is. Apologies for the length (150 lines).
#Any live cell with fewer than two live neighbours dies, as if caused by under-population. #Any live cell with two or three live neighbours lives on to the next generation. #Any live cell with more than three live neighbours dies, as if by over-population. #Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. ######################################################################## import time from tkinter import * tk = Tk() canvas = Canvas(tk, width=1200, height=900,bd=0, highlightthickness=0) canvas.pack() ##################### VARIABLES ######################################## keypressed = "" # no key pressed yet sqident = 1 # object number of SQuare and IDENTifier dubblelist=[] # prevent object being placed twice at same position ############# GENERATE FIELD MANUALLY ###################### # ^ UP, V DOWN, < LEFT, > RIGHT, X = CREATE, f = FINISHED - START GAME sq = canvas.create_rectangle(50, 50, 100, 100, fill="grey") #starting square identifier = 1 def movesquare(event): # detect movement keys if event.keysym == "Up": canvas.move(sqident, 0, -50) #identifier, x, y elif event.keysym == "Down": canvas.move(sqident, 0, 50) elif event.keysym == "Left": canvas.move(sqident, -50, 0) elif event.keysym == "Right": canvas.move(sqident, 50, 0) def otherkey(event): #detect other keys pressed global sqident global keypressed keypressed = event.keysym if keypressed == "x": # make new square if (canvas.coords(sqident)) not in dubblelist: # prevent creating twice sqident +=1 #new square identifier sq=canvas.create_rectangle((canvas.coords(sqident-1)), fill="grey") #new square created at previous position dubblelist.append(canvas.coords(sqident-1)) while 1: canvas.bind_all("<KeyPress-Up>", movesquare) # snap niet verschil annotatie "Up"vs <KeyPress-Up> canvas.bind_all("<KeyPress-Down>", movesquare) canvas.bind_all("<KeyPress-Left>", movesquare) canvas.bind_all("<KeyPress-Right>", movesquare) canvas.bind_all("x", otherkey) canvas.bind_all("f", otherkey) if keypressed == "f": canvas.delete(sqident) break tk.update_idletasks() tk.update() ############################## start animation ############################# numberofgenerations =0 while numberofgenerations < 1000: tk.update() ##################### variables ######################################## livelist = canvas.find_all() # livelist = list of al objectIDs on canvas leli = len(livelist) # leli = number of objects on canvas createlist=[] # createlist = newly created squares stored this list (coordinates are stored!) killlist=[] # killlist = to be killed squares stored in this list (objectIDs are stored!) ######################################################################### # Staying ALIVE OR DIE using Object IDs. Detecting number of neighbours. Store in killist for x in range(1,leli+1): # look in livelist for object identifier at position x and retrieve its coordinates x1=(canvas.coords(livelist[x-1])[0]); y1=(canvas.coords(livelist[x-1])[1]) x2=(canvas.coords(livelist[x-1])[2]); y2=(canvas.coords(livelist[x-1])[3]) # Count number of Neighbours CN by using overlap function (-1 as it includes object itself) CN=(len(canvas.find_overlapping(x1,y1,x2,y2))-1) # if CN < 2: #dead too few neighbours killlist.append (canvas.find_enclosed(x1-10,y1-10,x2+10,y2+10)) # enclosed function gives Object ID square within enclosed. Enclosed needs wider coordinates # do nothing if CN==2 OR CN==3 (survive) if CN > 3: #dead too many neighbours killlist.append (canvas.find_enclosed(x1-10,y1-10,x2+10,y2+10)) ######################################################################### # Creating new lives. Store in createlist # look at coordinates NW,N,NE,W,E,SW,S,SE of object # detect whether empty and count number of adjacent to empty field. If 3 create new live for x in range(1,leli+1): x1=(canvas.coords(livelist[x-1])[0]); y1=(canvas.coords(livelist[x-1])[1]) x2=(canvas.coords(livelist[x-1])[2]); y2=(canvas.coords(livelist[x-1])[3]) NWcoordinates=[x1-50,y1-50,x2-50,y2-50] # North West coordinates NW=len(canvas.find_overlapping(x1-50,y1-50,x2-50,y2-50)) # Number of objects around NW NWencl=len(canvas.find_enclosed(x1-60,y1-60,x2-40,y2-40)) # NW coordinates -> 0 is empty ; 1 is present Nocoordinates=[x1,y1-50,x2,y2-50] #North No=len(canvas.find_overlapping(x1,y1-50,x2,y2-50)) Noencl=len(canvas.find_enclosed(x1-10,y1-60,x2+10,y2-40)) NEcoordinates=[x1+50,y1-50,x2+50,y2-50] #North East NE=len(canvas.find_overlapping(x1+50,y1-50,x2+50,y2-50)) NEencl=len(canvas.find_enclosed(x1+40,y1-60,x2+60,y2-40)) Wecoordinates=[x1-50,y1,x2-50,y2] #West We=len(canvas.find_overlapping(x1-50,y1,x2-50,y2)) Weencl=len(canvas.find_enclosed(x1-60,y1-10,x2-40,y2+10)) Eacoordinates=[x1+50,y1,x2+50,y2] #East Ea=len(canvas.find_overlapping(x1+50,y1,x2+50,y2)) Eaencl=len(canvas.find_enclosed(x1+40,y1-10,x2+60,y2+10)) SWcoordinates=[x1-50,y1+50,x2-50,y2+50] #South West SW=len(canvas.find_overlapping(x1-50,y1+50,x2-50,y2+50)) SWencl=len(canvas.find_enclosed(x1-60,y1+40,x2-40,y2+60)) Socoordinates=[x1,y1+50,x2,y2+50] #South So=len(canvas.find_overlapping(x1,y1+50,x2,y2+50)) Soencl=len(canvas.find_enclosed(x1-10,y1+40,x2+10,y2+60)) SEcoordinates=[x1+50,y1+50,x2+50,y2+50] #South East SE=len(canvas.find_overlapping(x1+50,y1+50,x2+50,y2+50)) SEencl=len(canvas.find_enclosed(x1+40,y1+40,x2+60,y2+60)) if NWencl != 1: # NWencl (enclosed) not 1 thus is empty if NW == 3: # NW 3 (overlapping) neighbours thus new life if NWcoordinates not in createlist: # prevent creating twice createlist.append (NWcoordinates) if Noencl != 1: if No == 3: if Nocoordinates not in createlist: createlist.append (Nocoordinates) if NEencl != 1: if NE == 3: if NEcoordinates not in createlist: createlist.append (NEcoordinates) if Weencl != 1: if We == 3: if Wecoordinates not in createlist: createlist.append (Wecoordinates) if Eaencl != 1: if Ea == 3: if Eacoordinates not in createlist: createlist.append (Eacoordinates) if SWencl != 1: if SW == 3: if SWcoordinates not in createlist: createlist.append (SWcoordinates) if Soencl != 1: if So == 3: if Socoordinates not in createlist: createlist.append (Socoordinates) if SEencl != 1: if SE == 3: if SEcoordinates not in createlist: createlist.append (SEcoordinates) ######################################################################### #start CReating lecl=len(createlist) #number of objects to create (len) for CR in range(0,lecl): sq=canvas.create_rectangle(createlist[CR], fill="grey") #select from createlist coordinates ######################################################################### #start KIlling leki=len(killlist) #number of objects to kill (len) for KI in range(0,leki): kill=(killlist)[KI] #select from killlist Object ID canvas.delete(kill) ######################################################################### # slow down for TI in range(0,2): time.sleep(0.2) numberofgenerations +=1 mainloop()