Hello, im creating the famous game of life, but without using pygame, numpy and all other programs, just the basic IDLE. My plan is to create a window consisting of 6 buttons (START CLEAR RANDOMIZE LOAD SAVE QUIT) where for each button python performs different tasks.
Im almost there ( i think) but i just cant seem to go any further, no matter which way i try to program. Please help eather with the program itself or tips or anything is greatly appreciated!! :)
Im almost there ( i think) but i just cant seem to go any further, no matter which way i try to program. Please help eather with the program itself or tips or anything is greatly appreciated!! :)
from graphics import * from random import * from time import sleep from copy import deepcopy from math import * def size(): """ Returns the size of the board. """ return 20 def board(): board = [ [ False for i in range(size()) ] for j in range(size()) ] return board def living_neighbors(x,y,board): """ Returns the number of living neighbors of the cell at coordinates (x,y). If either coordinate is on the edge of the board, we need to wrap to the opposite board edge: i.e. we are on a torus.""" neighbors = [] neighbor = [] neighbor.append(board[(x-1)%size()][(y-1)%size()]) neighbor.append(board[(x)%size()][(y-1)%size()]) neighbor.append(board[(x+1)%size()][(y-1)%size()]) neighbor.append(board[(x-1)%size()][(y)%size()]) neighbor.append(board[(x+1)%size()][(y)%size()]) neighbor.append(board[(x-1)%size()][(y+1)%size()]) neighbor.append(board[(x)%size()][(y+1)%size()]) neighbor.append(board[(x+1)%size()][(y+1)%size()]) neighbors=neighbor.count(True) return neighbors def iterate(board): """ Does one interation, and returns the next generation of cells. Returns the tuple (living, board), while living is the number of alive cells and board is a matrix of boolean values corresponding to the aliveness of each cell.""" new_board = [] for x in range(size()): row = [] for y in range(size()): row.append(False) new_board.append(row) for x in range(size()): for y in range(size()): neighbors=living_neighbors(x,y,board) if board[x][y] == True and neighbors < 2: new_board[x][y] = False if board[x][y] == True and neighbors > 3: new_board[x][y] = False if board[x][y] == False and neighbors == 3: new_board[x][y] = True if board[x][y] ==False and neighbors != 3: new_board[x][y] = False if board[x][y] == True and neighbors == 2: new_board[x][y] = True if board[x][y] == True and neighbors ==3: new_board[x][y] = True living=new_board.count(True) return (living,new_board) def random_bit(): """ Returns a random value of True or False. """ return choice([True,False]) def update_color(x,y,board,rectangles): """ Sets color in rectangles [x,y] to correct value according to the status of its corresponding cell in board .""" if (board[x][y]): rectangles[x][y].setFill("red4") else: rectangles[x][y].setFill("ghostwhite") return rectangles def initiate(): board = [] rectangles = [] for i in range(size()): m = [] M = [] for j in range(size()): rec=(Rectangle(Point(i,j),Point(i+1,j+1))) m.append(rec) M.append(random_bit()) rectangles.append(m) board.append(M) return(board,rectangles) def gameoflife(): win2 = GraphWin("Game of Life",600,600) win2.setCoords(0,-3,size(),size()) board,rectangles=initiate() play= True for x in range(size()): for y in range(size()): rectangles[x][y].draw(win2) update_color(x,y,board,rectangles) living,new_board = iterate(board) while play == True: for i in range(size()): for j in range(size()): update_color(i,j,new_board,rectangles) living,new_board=iterate(new_board) if win2.checkMouse(): break def is_inside(rectangle,point): dx = point.getX() dy = point.getY() r1 = rectangle.getP1() x1 = r1.getX() y1 = r1.getY() r2 = rectangle.getP2() x2 = r2.getX() y2 = r2.getY() if dx > x1 and dx < x2 and dy > y1 and dy < y2: return True else: return False def switch(point,new_board,window): """Takes in point were mouse was clicked, then changes cell value and color""" dx = point.getX() dy = point.getY() dx = int(dx) dy = int(dy) update = 0 cell = Rectangle(Point(dx,dy),Point(dx+1,dy+1)) update = new_board[dx][dy] if update == 0: cell.setFill('red4') new_board[dx][dy] = 1 else: cell.setFill('ghostwhite') new_board[dx][dy] = 0 cell.draw(window) return (new_board) def process(action,new_board): if action == 0: return elif action == 1: nullset = [] board = [] for k in range(size()): board.append([]) for n in range (size()): board[k].append(0) nullset.append(k) nullset.append(n) m = int(int(len(nullset))/2) return (board,nullset,m) elif action == 3: file = eval(input("Enter filename: ")) infile = open(file,'r') pattern = infile.read() pattern=''.join(i for i in pattern if i.isdigit()) n = len(pattern) m = int(sqrt(n)) pattern = list(pattern) pattern = [int(q) for q in pattern] load_board = [] for x in range(0,m): load_board.append(pattern[(0+x*m):(m+(x*m))]) infile.close() return(load_board) elif action == 4: filename = eval(input("Enter filename: ")) outfile = open(filename,'w') pattern = str(new_board) outfile.write(pattern) outfile.close() return(new_board) def window2(): win2 = GraphWin("Game of Life",600,600) win2.setCoords(0,-3,size(),size()) # Drawing the interface. Text(Point(8,-0.5),"Iterations:").draw(win2) input = Entry(Point(10,-0.5),5) input.setText("0.0") input.draw(win2) button = Text(Point(1.5,-2),"Start") button.draw(win2) rect1 = Rectangle(Point(0.5,-2.5),Point(2.5,-1.5)).draw(win2) clear = Text(Point(4.5,-2),"Clear") clear.draw(win2) rect2 = Rectangle(Point(3.5,-2.5),Point(5.5,-1.5)).draw(win2) randomize = Text(Point(8.5,-2),"Randomize") randomize.draw(win2) rect3 = Rectangle(Point(6.5,-2.5),Point(10.5,-1.5)).draw(win2) load = Text(Point(12.5,-2),"Load") load.draw(win2) rect4 = Rectangle(Point(11.5,-2.5),Point(13.5,-1.5)).draw(win2) save = Text(Point(15.5,-2),"Save") save.draw(win2) rect5 = Rectangle(Point(14.5,-2.5),Point(16.5,-1.5)).draw(win2) quitt = Text(Point(18.5,-2),"Quit") quitt.draw(win2) rect6 = Rectangle(Point(17.5,-2.5),Point(19.5,-1.5)).draw(win2) rectangle = [rect1,rect2,rect3,rect4,rect5,rect6] k = len(rectangle) # Initiates the initial board and rectangles. board,rectangles=deepcopy(initiate()) # Draws the board and rectangles to the interface. for x in range(size()): for y in range(size()): rectangles[x][y] = Rectangle(Point(x,y),Point(x+1,y+1)) for x in range(size()): for y in range(size()): rectangles = update_color(x,y,board,rectangles) rectangles[x][y].draw(win2) while True: # Wait for a mouse click. point1 = win2.getMouse() dy = point1.getY if point1.getY > 0: board = switch(point1,board,win2) else: click = is_inside(rectangle[0],point1) if click == True: sleep(0.5) break iterations = int(input.getText()) count = iterations while True: action = 0 point = win2.getMouse() dy = point.getY() if dy > 0: switch(point,board,win2) else: for i in range(0,k): click = is_inside(rectangle[i],point) if click == True: action = i if action == 1: new_board = process(action,board)[0] changes = process(action,board)[1] m = process(action,board)[2] elif action == 2: new_board = initiate()[0] changes = process(1,board)[1] m = process(1,board)[2] elif action == 3: new_board = process(action,board) changes = process(1,board)[1] m = process(1,board)[2] elif action == 4: new_board = process(action,board) m = o count += 1 elif action == 5: win2.close() break else: new_board = iterate(board)[1] changes = iterate(board)[2] m = int(int(len(changes))/2) rectangles = [] update = [] for x in range(0,m): xx = changes[x+x] yy = changes[x+x+1] redtangles.append(Rectangle(Point(xx,yy),Point(xx+1,yy+1))) update.append(new_board[xx][yy]) rectangles = update_color(x,'update',update,rectangles) rectangles[x].draw(win2) board = new_board count -= 1 if count == 0: sleep(3) win2.close() break window2()
Error:The error I recieve:
Traceback (most recent call last):
File "C:\Users\game.py", line 287, in <module>
window2()
File "C:\Users\game.py", line 226, in window2
if point1.getY > 0:
TypeError: unorderable types: method() > int()
Reply