def draw_living_cells(self): self.screen.fill(BLACK) for x in range(0, GRID_WIDTH + 1): for y in range(0, GRID_HEIGHT): if self.grid[x + 1][y + 1] == 1: pos = (int(x * CELL_SIZE), int(y * CELL_SIZE), int(CELL_SIZE), int(CELL_SIZE)) pg.draw.rect(self.screen, LIFE_COLOR, pos, 0) pg.display.flip()
I would suggest that you modify your code to use pygame's Sprite mechanics and in particular look at the sprite group
pygame.sprite.DirtySprite. You can then mark the cells that have changed as dirty and have it only redraw those cells instead of all the cells.
This change would also require you to not redraw the entire background with the
Since your method is named
draw_living_cells(), that implies that there are dead cells that you do not redraw. Since you would not be filling the entire background that means that you have to draw the background onto the screen where the dead cell used to be.
pygame.PixelArrayfor direct pixel access of the target
Surface. Set the pixels directly, instead of drawing each cell separately by