Page 1 of 1

A Python program with GUI

Posted: December 20th, 2018, 2:05 pm
by PySimpleGUI
I hope this is the right forum to post this....

I created a Python version of the Game of Life, complete with GUI. I utilized an existing game engine and added a GUI on top. You can use the mouse to create the initial board, control the speed and number of generations.

I'm open to adding reading an initial file, etc, if people are interested in using it.

https://user-images.githubusercontent.c ... a87c68.gif

Because PySimpleGUI code tends to be quite compact, the program is maybe 140 lines when you remove the large comment block. Not bad considering the interactivity of it all.

Comments and feedback welcomed.

Re: A Python program with GUI

Posted: December 21st, 2018, 5:17 am
by Senso
PySimpleGUI wrote:Comments and feedback welcomed.
Good job. I can't say anything else since you're not linking to your program.

Re: A Python program with GUI

Posted: December 21st, 2018, 12:25 pm
by PySimpleGUI
Ooops! Sorry about not linking the code...

Conways Life using PySimpleGUI

Re: A Python program with GUI

Posted: December 30th, 2018, 11:50 am
by Redstoneboi
I feel like

Code: Select all

if (x != self.N and y != self.N):
                    s += self.old_grid[x][y]
                # The remaining branches handle the case where the neighbour is off the end of the grid.
                # In this case, we loop back round such that the grid becomes a "toroidal array".
                elif (x == self.N and y != self.N):
                    s += self.old_grid[0][y]
                elif (x != self.N and y == self.N):
                    s += self.old_grid[x][0]
                else:
                    s += self.old_grid[0][0]
could've been

Code: Select all

int newX = x % self.N
int newY = y % self.N
# We need to make sure that the neighbor is still on the grid, so we make it wrap around using modulo.
s += self.old_grid[newX][newY]
saves a bunch of lines.
it uses the modulo (%) operator, which in x % y results in x wrapping around within range y, exactly what you want.

Code: Select all

if (self.old_grid[i][j] == 1 and live < 2):
                        self.new_grid[i][j] = 0  # Dead from starvation.
                    elif (self.old_grid[i][j] == 1 and (live == 2 or live == 3)):
                        self.new_grid[i][j] = 1  # Continue living.
                    elif (self.old_grid[i][j] == 1 and live > 3):
                        self.new_grid[i][j] = 0  # Dead from overcrowding.
                    elif (self.old_grid[i][j] == 0 and live == 3):
                        self.new_grid[i][j] = 1  # Alive from reproduction.
"continue living" seems like a useless line, doesn't change anything considering you added the "starvation or overcrowding only" part.
also have you considered a list of boolean values to hold the rule? so you could get all the outer totalistic rules.
you could have like a 2*9 array, containing birth rules to survival, so you can explore highlife and life without death and seeds and more!
I'm not really a Python programmer, more of a C++ coder though.

Re: A Python program with GUI

Posted: December 30th, 2018, 1:26 pm
by PySimpleGUI
Thanks for checking out the Life-specific portions. I have barely read through that code to be honest. My contribution to this was strictly the GUI.

I grabbed the code off the net and used as-is, with no modification. I was happy it worked. Credit is at the top.

I appreciate you taking the time to look at the code and write up some concrete suggestions :!: