From 9cb2e22ebe8590343571f06c4f471aa73f2e9651 Mon Sep 17 00:00:00 2001 From: GILBERTKEETR Date: Sat, 21 Oct 2023 12:08:56 +0300 Subject: [PATCH 1/2] pygame code pattern --- pattern_printer.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 pattern_printer.py diff --git a/pattern_printer.py b/pattern_printer.py new file mode 100644 index 0000000..8619902 --- /dev/null +++ b/pattern_printer.py @@ -0,0 +1,47 @@ +import pygame +import math + +# Initialize Pygame +pygame.init() + +# Set up the screen +width, height = 800, 600 +screen = pygame.display.set_mode((width, height)) +pygame.display.set_caption("Fractal Tree") + +# Colors +background_color = (0, 0, 0) +tree_color = (0, 255, 0) + +# Function to draw a fractal tree +def draw_tree(x, y, branch_length, angle, depth): + if depth > 0: + # Calculate the endpoint of the branch + x2 = x + int(math.cos(math.radians(angle)) * branch_length) + y2 = y - int(math.sin(math.radians(angle)) * branch_length) + + # Draw the branch + pygame.draw.line(screen, tree_color, (x, y), (x2, y2), 2) + + # Recursive calls for right and left branches + draw_tree(x2, y2, branch_length * 0.7, angle - 20, depth - 1) + draw_tree(x2, y2, branch_length * 0.7, angle + 20, depth - 1) + +# Main drawing loop +running = True +while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + # Clear the screen + screen.fill(background_color) + + # Draw the fractal tree + draw_tree(width // 2, height - 50, 100, -90, 9) + + # Update the display + pygame.display.flip() + +# Quit Pygame +pygame.quit() From 1670415949ac64b11153290e85f42a76e17e01a3 Mon Sep 17 00:00:00 2001 From: GILBERTKEETR Date: Sat, 21 Oct 2023 12:13:15 +0300 Subject: [PATCH 2/2] new pattern --- pattern_printer.py | 60 +++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 41 deletions(-) diff --git a/pattern_printer.py b/pattern_printer.py index 8619902..147c347 100644 --- a/pattern_printer.py +++ b/pattern_printer.py @@ -1,47 +1,25 @@ -import pygame -import math +def sierpinski(level): + if level == 0: + return ["*"] -# Initialize Pygame -pygame.init() + lower_triangle = sierpinski(level - 1) + spaces = [" " * (2 ** level - 1) for _ in range(2 ** (level - 1))] -# Set up the screen -width, height = 800, 600 -screen = pygame.display.set_mode((width, height)) -pygame.display.set_caption("Fractal Tree") + result = [] + for i in range(2 ** level): + if i < 2 ** (level - 1): + result.append(lower_triangle[i] + " " + lower_triangle[i]) + else: + result.append(lower_triangle[i - 2 ** (level - 1)] + spaces[i - 2 ** (level - 1)] + lower_triangle[i - 2 ** (level - 1)]) -# Colors -background_color = (0, 0, 0) -tree_color = (0, 255, 0) + return result -# Function to draw a fractal tree -def draw_tree(x, y, branch_length, angle, depth): - if depth > 0: - # Calculate the endpoint of the branch - x2 = x + int(math.cos(math.radians(angle)) * branch_length) - y2 = y - int(math.sin(math.radians(angle)) * branch_length) +def print_sierpinski(level): + sierpinski_pattern = sierpinski(level) + for row in sierpinski_pattern: + print(row) - # Draw the branch - pygame.draw.line(screen, tree_color, (x, y), (x2, y2), 2) +# Set the desired level of the Sierpinski Triangle (adjust as needed) +level = 4 - # Recursive calls for right and left branches - draw_tree(x2, y2, branch_length * 0.7, angle - 20, depth - 1) - draw_tree(x2, y2, branch_length * 0.7, angle + 20, depth - 1) - -# Main drawing loop -running = True -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - # Clear the screen - screen.fill(background_color) - - # Draw the fractal tree - draw_tree(width // 2, height - 50, 100, -90, 9) - - # Update the display - pygame.display.flip() - -# Quit Pygame -pygame.quit() +print_sierpinski(level)