diff --git a/newgame/032124.py b/newgame/032124.py new file mode 100644 index 0000000..86ce814 --- /dev/null +++ b/newgame/032124.py @@ -0,0 +1,125 @@ +import pygame +from random import choice + +RES = WIDTH, HEIGHT = 1202, 902 +TILE = 50 +cols, rows = WIDTH // TILE, HEIGHT // TILE + +pygame.init() +sc = pygame.display.set_mode(RES) +clock = pygame.time.Clock() + + +class Cell: + def __init__(self, x, y): + self.x, self.y = x, y + self.walls = {'top': True, 'right': True, 'bottom': True, 'left': True} + self.visited = False + + def draw_current_cell(self): + x, y = self.x * TILE, self.y * TILE + pygame.draw.rect(sc, pygame.Color('saddlebrown'), (x + 2, y + 2, TILE - 2, TILE - 2)) + + def draw(self): + x, y = self.x * TILE, self.y * TILE + if self.visited: + pygame.draw.rect(sc, pygame.Color('black'), (x, y, TILE, TILE)) + + if self.walls['top']: + pygame.draw.line(sc, pygame.Color('darkorange'), (x, y), (x + TILE, y), 3) + if self.walls['right']: + pygame.draw.line(sc, pygame.Color('darkorange'), (x + TILE, y), (x + TILE, y + TILE), 3) + if self.walls['bottom']: + pygame.draw.line(sc, pygame.Color('darkorange'), (x + TILE, y + TILE), (x , y + TILE), 3) + if self.walls['left']: + pygame.draw.line(sc, pygame.Color('darkorange'), (x, y + TILE), (x, y), 3) + + def check_cell(self, x, y): + find_index = lambda x, y: x + y * cols + if x < 0 or x > cols - 1 or y < 0 or y > rows - 1: + return False + return grid_cells[find_index(x, y)] + + def check_neighbors(self): + neighbors = [] + top = self.check_cell(self.x, self.y - 1) + right = self.check_cell(self.x + 1, self.y) + bottom = self.check_cell(self.x, self.y + 1) + left = self.check_cell(self.x - 1, self.y) + if top and not top.visited: + neighbors.append(top) + if right and not right.visited: + neighbors.append(right) + if bottom and not bottom.visited: + neighbors.append(bottom) + if left and not left.visited: + neighbors.append(left) + return choice(neighbors) if neighbors else False + + +def remove_walls(current, next): + dx = current.x - next.x + if dx == 1: + current.walls['left'] = False + next.walls['right'] = False + elif dx == -1: + current.walls['right'] = False + next.walls['left'] = False + dy = current.y - next.y + if dy == 1: + current.walls['top'] = False + next.walls['bottom'] = False + elif dy == -1: + current.walls['bottom'] = False + next.walls['top'] = False + + +grid_cells = [Cell(col, row) for row in range(rows) for col in range(cols)] +current_cell = grid_cells[0] +stack = [] +colors, color = [], 40 + + + +def generate_maze(): + global current_cell + while True: + current_cell.visited = True + next_cell = current_cell.check_neighbors() + if next_cell: + next_cell.visited = True + stack.append(current_cell) + remove_walls(current_cell, next_cell) + current_cell = next_cell + elif stack: + current_cell = stack.pop() + else: + break +generate_maze() + +while True: + sc.fill(pygame.Color('darkslategray')) + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + exit() + + [cell.draw() for cell in grid_cells] + current_cell.visited = True + current_cell.draw_current_cell() + [pygame.draw.rect(sc, colors[i], (cell.x * TILE + 2, cell.y * TILE + 2, + TILE - 4, TILE - 4)) for i, cell in enumerate(stack)] + + next_cell = current_cell.check_neighbors() + if next_cell: + next_cell.visited = True + stack.append(current_cell) + colors.append((min(color, 255), 10, 100)) + color += 1 + remove_walls(current_cell, next_cell) + current_cell = next_cell + elif stack: + current_cell = stack.pop() + + pygame.display.flip() + clock.tick(30) \ No newline at end of file diff --git a/newgame/1.py b/newgame/1.py new file mode 100644 index 0000000..e97d194 --- /dev/null +++ b/newgame/1.py @@ -0,0 +1,105 @@ +import pygame +import random + +# Размеры окна +WIDTH, HEIGHT = 800, 800 +# Размеры ячейки +CELL_SIZE = 20 +# Количество ячеек +ROWS, COLS = HEIGHT // CELL_SIZE, WIDTH // CELL_SIZE + +# Цвета +WHITE = (255, 255, 255) +GREEN = (0, 255, 0) + +# Инициализация Pygame +pygame.init() +WIN = pygame.display.set_mode((WIDTH, HEIGHT)) + +# Класс ячейки +class Cell: + def __init__(self, row, col): + self.row = row + self.col = col + self.visited = False + self.walls = {"top": True, "right": True, "bottom": True, "left": True} + + def draw(self): + x, y = self.col * CELL_SIZE, self.row * CELL_SIZE + if self.visited: + pygame.draw.rect(WIN, GREEN, (x, y, CELL_SIZE, CELL_SIZE)) + if self.walls["top"]: + pygame.draw.line(WIN, WHITE, (x, y), (x + CELL_SIZE, y)) + if self.walls["right"]: + pygame.draw.line(WIN, WHITE, (x + CELL_SIZE, y), (x + CELL_SIZE, y + CELL_SIZE)) + if self.walls["bottom"]: + pygame.draw.line(WIN, WHITE, (x, y + CELL_SIZE), (x + CELL_SIZE, y + CELL_SIZE)) + if self.walls["left"]: + pygame.draw.line(WIN, WHITE, (x, y), (x, y + CELL_SIZE)) + +# Генерация сетки +grid = [[Cell(i, j) for j in range(COLS)] for i in range(ROWS)] + +def main(): + clock = pygame.time.Clock() + running = True + + # Начальная ячейка + start = grid[0][0] + stack = [start] + + while running: + clock.tick(60) + + # Обработка событий + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + # Генерация лабиринта + if len(stack) > 0: + current = stack[-1] + current.visited = True + neighbours = [] + + # Проверка соседей + if current.row > 0 and not grid[current.row - 1][current.col].visited: + neighbours.append(grid[current.row - 1][current.col]) + if current.col < COLS - 1 and not grid[current.row][current.col + 1].visited: + neighbours.append(grid[current.row][current.col + 1]) + if current.row < ROWS - 1 and not grid[current.row + 1][current.col].visited: + neighbours.append(grid[current.row + 1][current.col]) + if current.col > 0 and not grid[current.row][current.col - 1].visited: + neighbours.append(grid[current.row][current.col - 1]) + + if len(neighbours) > 0: + next_cell = random.choice(neighbours) + stack.append(next_cell) + + # Удаление стен + if next_cell == grid[current.row - 1][current.col]: + current.walls["top"] = False + next_cell.walls["bottom"] = False + elif next_cell == grid[current.row][current.col + 1]: + current.walls["right"] = False + next_cell.walls["left"] = False + elif next_cell == grid[current.row + 1][current.col]: + current.walls["bottom"] = False + next_cell.walls["top"] = False + elif next_cell == grid[current.row][current.col - 1]: + current.walls["left"] = False + next_cell.walls["right"] = False + else: + stack.pop() + + # Отрисовка сетки + WIN.fill((0, 0, 0)) + for row in grid: + for cell in row: + cell.draw() + pygame.display.update() + + pygame.quit() + +if __name__ == "__main__": + main()