Skip to content

Commit

Permalink
fixed errors in the design of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Daria authored and Daria committed Jan 21, 2025
1 parent aa446f3 commit a26da8d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
5 changes: 3 additions & 2 deletions homework04/life.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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])

1 change: 0 additions & 1 deletion homework04/life_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ def run(self) -> None:
running = False
break
curses.endwin()

47 changes: 36 additions & 11 deletions homework04/life_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
"""Отобразить состояние клеток."""
Expand All @@ -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:
Expand All @@ -74,27 +85,41 @@ 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:
self.screen.blit(button_text, (self.button.x + 10, self.button.y + 15))

# сообщения об ошибках
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:
Expand Down
23 changes: 18 additions & 5 deletions homework04/life_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
"""Запустить игру"""
Expand Down Expand Up @@ -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)

Expand All @@ -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

Expand Down

0 comments on commit a26da8d

Please sign in to comment.