Python Forum
conway's game of life
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
conway's game of life
#1
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).

#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()
Reply


Messages In This Thread
conway's game of life - by hanscvanleeuwen - Nov-25-2016, 07:51 PM
RE: conway's game of life - by Skaperen - Nov-26-2016, 08:25 AM
RE: conway's game of life - by hanscvanleeuwen - Nov-26-2016, 09:33 AM
RE: conway's game of life - by hanscvanleeuwen - Nov-26-2016, 12:24 PM
RE: conway's game of life - by Skaperen - Nov-27-2016, 01:45 AM
RE: conway's game of life - by Mekire - Nov-28-2016, 04:30 AM
RE: conway's game of life - by hanscvanleeuwen - Nov-28-2016, 07:55 AM
RE: conway's game of life - by heiner55 - Nov-29-2016, 02:41 PM
RE: conway's game of life - by hanscvanleeuwen - Nov-30-2016, 01:45 PM
RE: conway's game of life - by hanscvanleeuwen - Dec-07-2016, 02:02 PM
RE: conway's game of life - by Ofnuts - Dec-08-2016, 09:56 PM
RE: conway's game of life - by Mekire - Dec-09-2016, 03:35 AM
RE: conway's game of life - by hanscvanleeuwen - Dec-09-2016, 10:13 AM
RE: conway's game of life - by Mekire - Dec-09-2016, 11:03 AM
RE: conway's game of life - by hanscvanleeuwen - Dec-09-2016, 11:25 AM
RE: conway's game of life - by hanscvanleeuwen - Dec-09-2016, 01:51 PM
RE: conway's game of life - by Mekire - Dec-09-2016, 02:10 PM
RE: conway's game of life - by hanscvanleeuwen - Dec-09-2016, 04:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  While loop/half-life Miraclefruit 6 8,556 Mar-06-2017, 05:24 PM
Last Post: nilamo
  conway's game of life / Single Responsibility Principle hanscvanleeuwen 13 11,243 Dec-17-2016, 08:30 AM
Last Post: hanscvanleeuwen
  Game of life using IDLE Informatics109 4 5,147 Oct-29-2016, 01:39 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020