So first I want to make clear, the following is not intended to run. It is just the kind of thing I want to see stylistically. You will need to deal with the details of making real code out of this:
Other than that you have bad habits that need to be gotten rid of now.
Most of your issues in turning your program into good modular code will be in removing the globals.
I would honestly use classes but this might be too much for the moment.
Any names defined in the global space that are intended to be constant should be capitalized like:
No more * imports. Stop that now.
Also, stop the semicolons to squeeze multiple statements on one line.
Learn proper comment style. # comments are for short, either inline or mid-function comments.
For real documentation use docstrings.
A doc string looks like this:
You can even use specific IDEs that give you warnings if you are in violation of standards (though honestly that is a bridge too far for me).
def bind_keys(canvas): """Bind all appropriate keys to the correct function.""" bindings = {"<keypress-up>" : movesquare, "<keypress-down>": movesquare, "<keypress-left>": movesquare, "<keypress-right>": movesquare, "x": otherkey, "f": otherkey, "r": otherkey} for binding in bindings: canvas.bind_all(binding, bindings[binding]) def setup_phase(window, canvas): done = False while not done: window.update() window.update_idletasks() if keypressed == "f": #finished cells = on_finish_setup(canvas, sqident) done = True elif keypressed == "r": #create 15 random squares cells = on_random_setup(canvas, sqident) done = True return cells def on_finish_setup(canvas, sqident): # Your func here. def on_random_setup(canvas, sqident): # Your func here. def main_phase(cells, window, canvas): while 1: window.update();window.update_idletasks() livelist = canvas.find_all() createlist, killlist = [],[] update_create_kill(createlist, kill_list) render(createlist, killlist) def main(): window = tk(); window.title("conway's game of life") canvas = canvas(window, width=1200, height=900, bd=0, highlightthickness=0) canvas.pack() cells = setup_phase(window, canvas) main_phase(cells, window, canvas) if __name__ == "__main__": main()Note how every function is very short and does one specific thing. If any further complicated logic is needed it is then put in its own function as well. One function, one job.
Other than that you have bad habits that need to be gotten rid of now.
Most of your issues in turning your program into good modular code will be in removing the globals.
I would honestly use classes but this might be too much for the moment.
Any names defined in the global space that are intended to be constant should be capitalized like:
CELL_SIZE = 50 MOVES = {"up": (0, -CELL_SIZE), "down": (0, CELL_SIZE), "left" : (-CELL_SIZE, 0), "right" : (CELL_SIZE, 0)}Global variables are not acceptable. If it is defined globally it better not change. Consider the global keyword completely off limits.
No more * imports. Stop that now.
Also, stop the semicolons to squeeze multiple statements on one line.
Learn proper comment style. # comments are for short, either inline or mid-function comments.
For real documentation use docstrings.
A doc string looks like this:
def some_function(): """ I am a doc string. """ passAs the previous user suggested you should really adhere to at least the majority of pep8.
You can even use specific IDEs that give you warnings if you are in violation of standards (though honestly that is a bridge too far for me).