import pygame import random # Initialize pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Chaos Game - Sierpinski Triangle") # Define the triangle vertices vertex1 = (WIDTH // 2, 50) vertex2 = (50, HEIGHT - 50) vertex3 = (WIDTH - 50, HEIGHT - 50) vertices = [vertex1, vertex2, vertex3] # Start with a random point current_point = (random.randint(0, WIDTH), random.randint(0, HEIGHT)) # Set up the clock for controlling frame rate clock = pygame.time.Clock() # Fill background color (black) screen.fill((0, 0, 0)) # Main loop running = True while running: # Handle events (close window) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Plot several points per frame for a faster build-up for _ in range(1): # Choose a random vertex chosen_vertex = random.choice(vertices) # Compute the midpoint between current point and chosen vertex current_point = ((current_point[0] + chosen_vertex[0]) // 2, (current_point[1] + chosen_vertex[1]) // 2) # Draw the point (using a small rectangle as a pixel) screen.fill((255, 255, 255), (current_point[0], current_point[1], 1, 1)) # Update display pygame.display.flip() # Limit to 60 frames per second clock.tick() pygame.quit()