lordmauve / pgzero

A zero-boilerplate games programming framework for Python 3, based on Pygame.
https://pygame-zero.readthedocs.io/
GNU Lesser General Public License v3.0
527 stars 191 forks source link

Suggested change to demo Life program #288

Closed ideonaut closed 2 years ago

ideonaut commented 2 years ago

The existing Life program on the site gets weird when things reach the edges. To get traditional toroidal wrapping, I suggest:

        for dy in range(-1, 2):
            ny = (y + dy) % grid_y_count # line added
            for dx in range(-1, 2):
                nx = (x + dx) % grid_x_count # line added
                if (not (dy == 0 and dx == 0)
                    and grid[ny][nx]): # line added

# lines removed: # and (y + dy) < len(grid) # and (x + dx) < len(grid[y + dy]) # and grid[y + dy][x + dx]):

                    neighbor_count += 1

Of course the new variables ny and nx aren't really necessary, but they helped me figure out the fix and may make it more readable.

lordmauve commented 2 years ago

Which demo is this? I don't think we have a Conway's Life demo.

ideonaut commented 2 years ago

That's embarrassing! I got it from https://simplegametutorials.github.io/pygamezero/life/ , which I thought was part of the Pygame Zero site... Guess not!

I'll go bother them now.