diff --git a/homework04/life.py b/homework04/life.py index 05e2e24d..fe00f4f9 100644 --- a/homework04/life.py +++ b/homework04/life.py @@ -116,7 +116,9 @@ def is_max_generations_exceeded(self) -> bool: """ Не превысило ли текущее число поколений максимально допустимое. """ - return self.generations >= self.max_generations if self.max_generations else False + return ( + self.generations >= self.max_generations if self.max_generations else False + ) @property def is_changing(self) -> bool: @@ -147,4 +149,3 @@ def save(self, filename: pathlib.Path) -> None: """ with filename.open("w") as f: f.writelines([str(row) + "\n" for row in self.curr_generation]) - \ No newline at end of file diff --git a/homework04/life_console.py b/homework04/life_console.py index 9e9c478f..77c7e801 100644 --- a/homework04/life_console.py +++ b/homework04/life_console.py @@ -49,4 +49,3 @@ def run(self) -> None: running = False break curses.endwin() - \ No newline at end of file diff --git a/homework04/life_gui.py b/homework04/life_gui.py index be84f3e9..07de1b31 100644 --- a/homework04/life_gui.py +++ b/homework04/life_gui.py @@ -20,8 +20,12 @@ def __init__(self, life: GameOfLife, cell_size: int = 10, speed: int = 10) -> No self.button_frame = pygame.Rect(self.width // 2 - 50, 0, 104, 54) self.button = pygame.Rect(self.width // 2 - 48, 2, 100, 50) - self.error_frame = pygame.Rect(self.width // 2 - 132, self.height // 2 - 7, 304, 34) - self.error_button = pygame.Rect(self.width // 2 - 130, self.height // 2 - 5, 300, 30) + self.error_frame = pygame.Rect( + self.width // 2 - 132, self.height // 2 - 7, 304, 34 + ) + self.error_button = pygame.Rect( + self.width // 2 - 130, self.height // 2 - 5, 300, 30 + ) pygame.font.init() self.font = pygame.font.Font(None, 30) @@ -31,9 +35,13 @@ def __init__(self, life: GameOfLife, cell_size: int = 10, speed: int = 10) -> No def draw_lines(self) -> None: """Отрисовать сетку""" for x in range(0, self.width, self.cell_size): - pygame.draw.line(self.screen, pygame.Color("black"), (x, 0), (x, self.height)) + pygame.draw.line( + self.screen, pygame.Color("black"), (x, 0), (x, self.height) + ) for y in range(0, self.height, self.cell_size): - pygame.draw.line(self.screen, pygame.Color("black"), (0, y), (self.width, y)) + pygame.draw.line( + self.screen, pygame.Color("black"), (0, y), (self.width, y) + ) def draw_grid(self) -> None: """Отобразить состояние клеток.""" @@ -50,7 +58,10 @@ def draw_grid(self) -> None: else: for x in range(i * self.cell_size, (i + 1) * self.cell_size + 1): pygame.draw.line( - self.screen, pygame.Color("white"), (j * self.cell_size, x), ((j + 1) * self.cell_size, x) + self.screen, + pygame.Color("white"), + (j * self.cell_size, x), + ((j + 1) * self.cell_size, x), ) def run(self) -> None: @@ -74,17 +85,25 @@ def run(self) -> None: pos_x = mouse_pos[1] // self.cell_size pos_y = mouse_pos[0] // self.cell_size if 0 <= pos_x < self.life.rows and 0 <= pos_y < self.life.cols: - self.life.curr_generation[pos_x][pos_y] = 1 - self.life.curr_generation[pos_x][pos_y] + self.life.curr_generation[pos_x][pos_y] = ( + 1 - self.life.curr_generation[pos_x][pos_y] + ) self.screen.fill(pygame.Color("white")) # очистка экрана self.draw_grid() self.draw_lines() # кнопка паузы - button_color = pygame.Color("light steel blue") if self.status else (pygame.Color("lavender")) + button_color = ( + pygame.Color("light steel blue") + if self.status + else (pygame.Color("lavender")) + ) pygame.draw.rect(self.screen, "indigo", self.button_frame, border_radius=15) pygame.draw.rect(self.screen, button_color, self.button, border_radius=15) - button_text = self.font.render("Pause" if not self.status else "Resume", True, pygame.Color("indigo")) + button_text = self.font.render( + "Pause" if not self.status else "Resume", True, pygame.Color("indigo") + ) if not self.status: self.screen.blit(button_text, (self.button.x + 20, self.button.y + 15)) else: @@ -92,9 +111,15 @@ def run(self) -> None: # сообщения об ошибках if self.life.is_max_generations_exceeded: - pygame.draw.rect(self.screen, "black", self.error_frame, border_radius=0) - pygame.draw.rect(self.screen, "white", self.error_button, border_radius=0) - error_msg = self.font.render("Max generations exceeded", True, pygame.Color("red")) + pygame.draw.rect( + self.screen, "black", self.error_frame, border_radius=0 + ) + pygame.draw.rect( + self.screen, "white", self.error_button, border_radius=0 + ) + error_msg = self.font.render( + "Max generations exceeded", True, pygame.Color("red") + ) self.screen.blit(error_msg, (self.width // 3, self.height // 2)) self.status = True if not self.life.is_changing: diff --git a/homework04/life_proto.py b/homework04/life_proto.py index ae5711d9..915d279f 100644 --- a/homework04/life_proto.py +++ b/homework04/life_proto.py @@ -10,7 +10,9 @@ class GameOfLife: - def __init__(self, width: int = 640, height: int = 480, cell_size: int = 10, speed: int = 10) -> None: + def __init__( + self, width: int = 640, height: int = 480, cell_size: int = 10, speed: int = 10 + ) -> None: self.width = width self.height = height self.cell_size = cell_size @@ -32,9 +34,13 @@ def __init__(self, width: int = 640, height: int = 480, cell_size: int = 10, spe def draw_lines(self) -> None: """Отрисовать сетку""" for x in range(0, self.width, self.cell_size): - pygame.draw.line(self.screen, pygame.Color("black"), (x, 0), (x, self.height)) + pygame.draw.line( + self.screen, pygame.Color("black"), (x, 0), (x, self.height) + ) for y in range(0, self.height, self.cell_size): - pygame.draw.line(self.screen, pygame.Color("black"), (0, y), (self.width, y)) + pygame.draw.line( + self.screen, pygame.Color("black"), (0, y), (self.width, y) + ) def run(self) -> None: """Запустить игру""" @@ -97,7 +103,10 @@ def draw_grid(self) -> None: for col_index, col in enumerate(row): color = pygame.Color("green") if col == 1 else pygame.Color("white") rect = pygame.Rect( - col_index * self.cell_size, row_index * self.cell_size, self.cell_size, self.cell_size + col_index * self.cell_size, + row_index * self.cell_size, + self.cell_size, + self.cell_size, ) pygame.draw.rect(self.screen, color, rect) @@ -123,7 +132,11 @@ def get_neighbours(self, cell: Cell) -> Cells: neighbours = [] for i in range(row - 1, row + 2): for j in range(col - 1, col + 2): - if 0 <= i < self.cell_height and 0 <= j < self.cell_width and (row, col) != (i, j): + if ( + 0 <= i < self.cell_height + and 0 <= j < self.cell_width + and (row, col) != (i, j) + ): neighbours.append(self.grid[i][j]) return neighbours