Reputation: 61
I'm new in Python, and I'm trying to code my own version of Conway's Game of Life with Pygame. In order to draw the cells, I coded this method, which is part of a 'Grid' class:
def draw_cells(self, display_surf, tile_colour, bg_colour):
for column in self.grid:
for cell in column:
if cell.state:
pygame.draw.rect(display_surf, tile_colour, (cell.x, cell.y, self.tile_size, self.tile_size))
where 'grid' is a two dimension array (a list of lists). The problem is that the bottom row -that is, the last 'cell' in each 'column'- isn't being drawn, and I don't understand why. I used the debugger step by step and the last iteration of the inner loop is being executed, but the rectangles aren't being drawn. Even more, I put a couple of counters for each loop and both of them reach the same amount of iterations (if the X dimension works fine but the Y dimension wouldn't because of the last iteration of the inner loop, the outer counter should be greater than the inner one).
Actually I'm experimenting the same issue with another Grid class' method that draws the grid:
def draw_grid(self, display_surf, line_colour):
# vertical lines
for column in self.grid:
vertical_start = (column[0].x, column[0].y)
vertical_end = (vertical_start[0], vertical_start[1] + self.size_y * self.tile_size)
pygame.draw.line(display_surf, line_colour, vertical_start, vertical_end)
# latest vertical line (right border)
vertical_start = (self.grid[0][0].x + self.size_x * self.tile_size, self.grid[0][0].y)
vertical_end = (vertical_start[0], vertical_start[1] + self.size_y * self.tile_size)
pygame.draw.line(display_surf, line_colour, vertical_start, vertical_end)
# horizontal lines
for row in self.grid[0]:
horizontal_start = (row.x, row.y)
horizontal_end = (horizontal_start[0] + self.size_x * self.tile_size, horizontal_start[1])
pygame.draw.line(display_surf, line_colour, horizontal_start, horizontal_end)
# latest horizontal line (bottom border)
horizontal_start = (self.grid[0][0].x, self.grid[0][0].y + self.size_y * self.tile_size)
horizontal_end = (horizontal_start[0] + self.size_x * self.tile_size, horizontal_start[1])
pygame.draw.line(display_surf, line_colour, horizontal_start, horizontal_end)
In this case, the right and bottom borders are drawn correctly, as well as the last vertical line prior to the right border, but the last horizontal line prior to the bottom border isn't being drawn.
Does anyone understand what is happening?
Thanks in advance :)
Upvotes: 0
Views: 111
Reputation: 61
The problem was in another method which updates the state of the entire grid calculating the next state of each cell. The inner iterator limit in that method wasn't correct, so the grid was actually losing the last row (so there was nothing to draw there!)
Upvotes: 2