Skip to content

Commit a23f435

Browse files
committed
Fix crash when trying to draw outside screen
1 parent e7d43a2 commit a23f435

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

src/application.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ def _handle_events(self) -> None:
8686
self.running = False
8787
self.agent = agents[self.agent_index](self.grid)
8888

89+
def _draw_tiles(self) -> None:
90+
mouse_pos = pygame.mouse.get_pos()
91+
x, y = ((mouse_pos[0] // TILE_SIZE), ((mouse_pos[1] - HEADER_SIZE) // TILE_SIZE))
92+
93+
if 0 <= y < TILES_HEIGHT and 0 <= x < TILES_WIDTH:
94+
if self.grid[y][x] == ' ' and self.mouse_down[0] == 1:
95+
self.grid[y][x] = '#'
96+
if self.grid[y][x] == '#' and self.mouse_down[0] == 3:
97+
self.grid[y][x] = ' '
98+
99+
self.renderer.update_walls(self.grid)
100+
89101
def run(self) -> None:
90102
print('Running application')
91103

@@ -96,15 +108,7 @@ def run(self) -> None:
96108
self.agent.step()
97109

98110
if self.mouse_down and not self.running:
99-
mouse_pos = pygame.mouse.get_pos()
100-
x, y = ((mouse_pos[0] // TILE_SIZE), ((mouse_pos[1] - HEADER_SIZE) // TILE_SIZE))
101-
102-
if self.grid[y][x] == ' ' and self.mouse_down[0] == 1:
103-
self.grid[y][x] = '#'
104-
if self.grid[y][x] == '#' and self.mouse_down[0] == 3:
105-
self.grid[y][x] = ' '
106-
107-
self.renderer.update_walls(self.grid)
111+
self._draw_tiles()
108112

109113
self.renderer.render(self.agent)
110114

0 commit comments

Comments
 (0)