Write a code to generate random path that has a start and finish in pygame
- User
Write a code to generate random path that has a start and finish in pygame
- Assistant
Sure! Here's an example code that generates a random path with a start and finish point using Pygame:
import pygame import random # Window size WIDTH, HEIGHT = 800, 600 # Colors BLACK = (0, 0, 0) GREEN = (0, 255, 0) # Initialize Pygame pygame.init() window = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Random Path") # Constants START_POS = (100, 100) # Start position FINISH_POS = (700, 500) # Finish position RADIUS = 20 # Circle radius STEP = 10 # Step distance def generate_random_path(start, finish): path = [start] # List to store coordinates of the path current_pos = list(start) # Current position while current_pos != list(finish): dx = random.choice([-1, 0, 1]) * STEP # Randomly choose a step in x-direction dy = random.choice([-1, 0, 1]) * STEP # Randomly choose a step in y-direction # Update current position current_pos[0] += dx current_pos[1] += dy # Append current position to path path.append(tuple(current_pos)) return path def draw_path(path): for i in range(len(path)-1): pygame.draw.line(window, GREEN, path[i], path[i+1], RADIUS) # Generate random path path = generate_random_path(START_POS, FINISH_POS) # Main Loop running = True while running: # Clear the window window.fill(BLACK) # Draw path draw_path(path) # Draw start and finish points pygame.draw.circle(window, GREEN, START_POS, RADIUS) pygame.draw.circle(window, GREEN, FINISH_POS, RADIUS) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Update the window pygame.display.update() # Quit the program pygame.quit() - User
Write a code to generate random path with two lines that has a start and finish in pygame
- Assistant
Certainly! Here's an example code that generates a random path with two lines, each with a start and finish point, using Pygame:
import pygame import random # Window size WIDTH, HEIGHT = 800, 600 # Colors BLACK = (0, 0, 0) GREEN = (0, 255, 0) # Initialize Pygame pygame.init() window = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Random Path") # Constants START_POS = (100, 100) # Start position FINISH_POS = (700, 500) # Finish position RADIUS = 20 # Circle radius STEP = 10 # Step distance def generate_random_path(start, finish): path = [start] # List to store coordinates of the path current_pos = list(start) # Current position while current_pos != list(finish): dx = random.choice([-1, 0, 1]) * STEP # Randomly choose a step in x-direction dy = random.choice([-1, 0, 1]) * STEP # Randomly choose a step in y-direction # Update current position current_pos[0] += dx current_pos[1] += dy # Append current position to path path.append(tuple(current_pos)) return path def draw_path(path): for i in range(len(path)-1): pygame.draw.line(window, GREEN, path[i], path[i+1], RADIUS) # Generate random paths for two lines path1 = generate_random_path(START_POS, (START_POS[0], FINISH_POS[1] // 2)) path2 = generate_random_path((FINISH_POS[0] // 2, FINISH_POS[1] // 2), FINISH_POS) # Main Loop running = True while running: # Clear the window window.fill(BLACK) # Draw path for Line 1 draw_path(path1) # Draw path for Line 2 draw_path(path2) # Draw start and finish points pygame.draw.circle(window, GREEN, START_POS, RADIUS) pygame.draw.circle(window, GREEN, FINISH_POS, RADIUS) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Update the window pygame.display.update() # Quit the program pygame.quit()