Dec-07-2016, 02:02 PM
Homework finished .. less than 80 lines (assigment) .colors extra . thanks to you guys
import time from tkinter import * window = Tk() window.title("Conway's Game of Life") canvas = Canvas(window, width=1200, height=900,bd=0, highlightthickness=0) canvas.pack() text_ = Text(window, height=2, width=30) text_.pack() text_.insert(END, "To MOVE use arrow keys. Press x to seed. f to finish") ##################### VARIABLES ############################ keypressed = ""; SQident = 1; double=[] 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) 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 double: # prevent creating twice sq=canvas.create_rectangle((canvas.coords(SQident)), fill="grey") #new square created double.append(canvas.coords(SQident)) SQident +=1 #new square identifier while 1: window.update() window.update_idletasks() canvas.bind_all("<KeyPress-Up>", movesquare) 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 ############################## start creation ############################# numberofgenerations =0 ; color=0 colsel=["Light Pink","Pale Violet Red","Deep Pink","Medium Violet Red","Orchid","Medium Orchid","Medium Purple","Dark Violet","Blue Violet","Purple"] Neigh=([-50,-50,-50,-50],[0,-50,0,-50],[+50,-50,+50,-50],[-50,0,-50,0],[+50,0,+50,0],[-50,+50,-50,+50],[0,+50,0,+50],[+50,+50,+50,+50]) #Neigh contains coordinates, NW,N,NW,W,E,SW, SW,SE of square while numberofgenerations < 1000: window.update() window.update_idletasks() color +=1 if color == 10: color = 1 livelist = canvas.find_all() #= retrieves list of al objectIDs on canvas createlist=[] #= Coordinates of newly created squares stored this list. killlist=[] #= objectIDs to be killed squares stored in this list. # Staying ALIVE OR DIE for live in livelist: # look in livelist for object identifier and retrieve its coordinates x1=(canvas.coords(live)[0]); y1=(canvas.coords(live)[1]) x2=(canvas.coords(live)[2]); y2=(canvas.coords(live)[3]) CN=(len(canvas.find_overlapping(x1,y1,x2,y2))-1) # Count number of neighbours (CN) by using overlap function (-1 as it includes object itself) if CN < 2: #dead too few neighbours killlist.append (canvas.find_enclosed(x1-10,y1-10,x2+10,y2+10)) # enclosed gives Object ID square within enclosed (needs wider coordinates-10,-10,+10,+10) if CN > 3: #dead too many neighbours killlist.append (canvas.find_enclosed(x1-10,y1-10,x2+10,y2+10)) # Detect whether Neighbours empty (via Encl/enclosed) and count number of adjacent(via Overl/overlapping). for Ne in Neigh: Coo=[x1+Ne[0],y1+Ne[1],x2+Ne[2],y2+Ne[3]] # Coo = surrounding coordinates Overl=len(canvas.find_overlapping(x1+Ne[0],y1+Ne[1],x2+Ne[2],y2+Ne[3])) # Number of objects surrounding Encl=len(canvas.find_enclosed(x1+Ne[0]-10,y1+Ne[1]-10,x2+Ne[2]+10,y2+Ne[3]+10)) # enclosed coordinates:0 is empty; 1 is present if Encl != 1: # enclosed not 1 thus is field is empty. Possible new life if Overl == 3: # has 3 neighbours thus new life if Coo not in createlist: # prevent creating twice createlist.append (Coo) for create in createlist: #Start creating sq=canvas.create_rectangle(create, fill=(colsel[color])) #select coordinates from createlist for kill in killlist: #Start killing canvas.delete(kill) #Delete ObjectID for TI in range(0,2): #Slow down time.sleep(0.1) numberofgenerations +=1 mainloop()