import pygame import random import sys # Initialize Pygame pygame.init() # Set up the display window screen_width, screen_height = 600, 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Procedurally Generated Cartoon Faces") def draw_face(surface): """Draws a cartoon face with random features on the given surface.""" # Fill background surface.fill((255, 255, 255)) # --- Draw the Face --- # Define face center and radius, then choose a random skin tone face_center = (screen_width // 2, screen_height // 2) face_radius = 200 skin_tones = [(255, 229, 204), (255, 204, 153), (204, 153, 102)] face_color = random.choice(skin_tones) pygame.draw.circle(surface, face_color, face_center, face_radius) # --- Draw the Eyes --- # Parameters for eyes eye_radius = 20 pupil_radius = 10 # Random variations for eye positions eye_offset_x = 60 + random.randint(-15, 15) eye_y = face_center[1] - 40 + random.randint(-10, 10) # Left eye position left_eye_x = face_center[0] - eye_offset_x left_eye_pos = (left_eye_x, eye_y) # Right eye position right_eye_x = face_center[0] + eye_offset_x right_eye_pos = (right_eye_x, eye_y) # Draw white part of eyes pygame.draw.circle(surface, (255, 255, 255), left_eye_pos, eye_radius) pygame.draw.circle(surface, (255, 255, 255), right_eye_pos, eye_radius) # Draw pupils pygame.draw.circle(surface, (0, 0, 0), left_eye_pos, pupil_radius) pygame.draw.circle(surface, (0, 0, 0), right_eye_pos, pupil_radius) # --- Draw the Nose --- # Define nose points with some randomness nose_top = (face_center[0], face_center[1] - 10 + random.randint(-5, 5)) nose_left = (face_center[0] - 20 + random.randint(-5, 5), face_center[1] + 30 + random.randint(-5, 5)) nose_right = (face_center[0] + 20 + random.randint(-5, 5), face_center[1] + 30 + random.randint(-5, 5)) pygame.draw.polygon(surface, (255, 153, 153), [nose_top, nose_left, nose_right]) # --- Draw the Mouth --- # Define a rectangle where the mouth will be drawn and add some randomness mouth_width, mouth_height = 80, 40 mouth_x = face_center[0] - mouth_width // 2 + random.randint(-10, 10) mouth_y = face_center[1] + 40 + random.randint(-10, 10) mouth_rect = pygame.Rect(mouth_x, mouth_y, mouth_width, mouth_height) # Draw an arc to represent a smiling mouth; adjust angle for variation if desired start_angle = 3.14 # π (start from the left side, roughly) end_angle = 2 * 3.14 # 2π (end at the right side) pygame.draw.arc(surface, (0, 0, 0), mouth_rect, start_angle, end_angle, 3) # Update the display so the new face appears immediately pygame.display.flip() # Main loop clock = pygame.time.Clock() draw_face(screen) # Draw an initial face while True: for event in pygame.event.get(): # Allow the user to quit the window if event.type == pygame.QUIT: pygame.quit() sys.exit() # Press spacebar to generate a new face if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: draw_face(screen) clock.tick(30) # Limit the frame rate to 30 FPS