1

I've again encountered I don't know if a problem or just the placement of my code is off but in my python take on Conways game of life in python(taken from The Coding Train challenge #85 in js) with pygame to actually display the system.

I have finished and it seems to run, but the end result doesn't seem to me like the one you see in finished projects, mine looks more like a QR code or idk.

If you could take a look at it, would appreciate it a lot.

Here is the source code:

from itertools import count
from os import stat
import pygame
import random

WIDTH = 500
HEIGHT = 500

pygame.init()
window = pygame.display.set_mode((WIDTH,HEIGHT))

# create 2d array
def create_2d_list(cols, rows):
    arr = []
    for i in range(cols):
        arr.append([0] * rows)

    return arr
        

resolution = 20

cols = WIDTH // resolution
rows = HEIGHT // resolution

def main():



    # pygame.display.flip()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return
            
        global grid
        grid = create_2d_list(cols, rows)
        for i in range(cols):
            for j in range(rows):
                grid[i][j] = random.randint(0,1)

        draw()

        next = create_2d_list(cols, rows)

        # compute next based on grid
        
        for i in range(cols):
            for j in range(rows):
                state = grid[i][j]

                # count live neighbors
                sum = 0
                neighbors = countNeighbors(grid, i , j)


                if state == 0 and neighbors == 3:
                    next[i][j] = 1
                elif state == 1 and (neighbors < 2 or neighbors > 3):
                    next[i][j] = 0
                else:
                    next[i][j] = state
        
        grid = next
        pygame.time.delay(50)
        pygame.display.flip()
        

def draw():
    background = window.fill((255,255,255))


    for i in range(cols):
        for j in range(rows):
            x = i * resolution
            y = j * resolution
            if grid[i][j] == 1:
                pygame.draw.rect(window,(0,0,0), pygame.Rect(x , y, resolution, resolution))
            elif grid[i][j] == 0:
                pygame.draw.rect(window,(255,255,255), pygame.Rect(x , y, resolution, resolution))


def countNeighbors(grid, x, y):
    sum = 0
    for i in range(-1,2):
        for j in range(-1,2):
            col = (x + i + cols) % cols
            row = (x + i + rows) % rows          

            sum += grid[col][row]

    sum -= grid[x][y]
    return sum


if __name__ == "__main__":
    main()

I tried to debug it and it seems at least to me that the rules of Conway's Game of Life apply. But still in the bigger picture it just looks off.

I do think it has to do with my code arrangement, and I am not exactly sure how to place it to be correct.

filtom2
  • 13
  • 2
  • In `countNeighbors`: `row = (x + i + rows) % rows` -> `row = (y + j + rows) % rows`. – qouify Mar 29 '22 at 09:45
  • Also why do you keep regenerating a random grid at each game iteration? I'm not so familiar with the game of life, but are you sure it's how it's supposed to work? – qouify Mar 29 '22 at 09:48
  • both of these lines are not used: `from itertools import count` and `from os import stat` – D.L Mar 29 '22 at 09:51
  • add `time.sleep(0.5)` in the `main while loop` to slow it down – D.L Mar 29 '22 at 09:52
  • Thank you I tried your advices, but I also tried only generating fewer cells in a row. And the issue seems to be in the calculation because if I only display 3 grid it's size doesn't change and it always stays with width which was at the beginning (so for 3 cells wide it's 3 the entire time) – filtom2 Mar 30 '22 at 06:25

0 Answers0