make a python app of a hexagon rotating with balls bouncing inside it, the balls should…
- User
make a python app of a hexagon rotating with balls bouncing inside it, the balls should have collision between each other and also should have gravity, make it an appealing color scheme
- Assistant
Below is a Python application using the
pygamelibrary to create a visually appealing simulation of a hexagon rotating with balls bouncing inside it. The balls will collide with each other, the walls of the hexagon, and will be affected by gravity. The color scheme will be modern and appealing.Prerequisites
Install
pygameif you haven't already:pip install pygamePython Code
import pygame import math import random # Initialize pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Rotating Hexagon with Bouncing Balls") # Colors HEXAGON_COLOR = (44, 62, 80) # Dark gray-blue BALL_COLORS = [(26, 188, 156), (46, 204, 113), (52, 152, 219), (155, 89, 182), (241, 196, 15)] # Appealing colors BACKGROUND_COLOR = (34, 34, 34) # Dark background # Hexagon parameters HEX_RADIUS = 200 HEX_CENTER = (WIDTH // 2, HEIGHT // 2) ROTATION_SPEED = 0.01 # Ball parameters NUM_BALLS = 10 BALL_RADIUS = 10 GRAVITY = 0.1 BOUNCE_FACTOR = 0.9 # Hexagon vertex calculation def calculate_hexagon_vertices(center, radius, angle): vertices = [] for i in range(6): x = center[0] + radius * math.cos(math.radians(60 * i + angle)) y = center[1] + radius * math.sin(math.radians(60 * i + angle)) vertices.append((x, y)) return vertices # Ball class class Ball: def __init__(self, x, y, radius, color): self.x = x self.y = y self.radius = radius self.color = color self.vx = random.uniform(-2, 2) self.vy = random.uniform(-2, 2) def draw(self, screen): pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius) def update(self, vertices): # Apply gravity self.vy += GRAVITY # Move ball self.x += self.vx self.y += self.vy # Collision with hexagon walls for i in range(6): p1 = vertices[i] p2 = vertices[(i + 1) % 6] d = self.distance_to_line_segment(p1, p2) if d <= self.radius: self.collide_with_wall(p1, p2) break # Ball-to-ball collision for ball in balls: if ball != self and self.distance_to_ball(ball) <= self.radius + ball.radius: self.collide_with_ball(ball) def distance_to_line_segment(self, p1, p2): x0, y0 = self.x, self.y x1, y1 = p1 x2, y2 = p2 px = x2 - x1 py = y2 - y1 norm = px * px + py * py u = ((x0 - x1) * px + (y0 - y1) * py) / float(norm) if u > 1: u = 1 elif u < 0: u = 0 x = x1 + u * px y = y1 + u * py dx = x - x0 dy = y - y0 dist = math.sqrt(dx * dx + dy * dy) return dist def collide_with_wall(self, p1, p2): # Simple bounce logic (improve for more realistic physics) self.vx = -self.vx * BOUNCE_FACTOR self.vy = -self.vy * BOUNCE_FACTOR def distance_to_ball(self, other_ball): return math.sqrt((self.x - other_ball.x) ** 2 + (self.y - other_ball.y) ** 2) def collide_with_ball(self, other_ball): # Simple elastic collision dx = other_ball.x - self.x dy = other_ball.y - self.y dist = self.distance_to_ball(other_ball) if dist != 0: dx /= dist dy /= dist self.vx -= dx * (self.vx - other_ball.vx) * 0.5 self.vy -= dy * (self.vy - other_ball.vy) * 0.5 other_ball.vx += dx * (self.vx - other_ball.vx) * 0.5 other_ball.vy += dy * (self.vy - other_ball.vy) * 0.5 # Initialize balls balls = [Ball(HEX_CENTER[0], HEX_CENTER[1], BALL_RADIUS, random.choice(BALL_COLORS)) for _ in range(NUM_BALLS)] # Main loop clock = pygame.time.Clock() rotation_angle = 0 running = True while running: screen.fill(BACKGROUND_COLOR) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Update rotation angle rotation_angle += ROTATION_SPEED # Calculate hexagon vertices vertices = calculate_hexagon_vertices(HEX_CENTER, HEX_RADIUS, math.degrees(rotation_angle)) # Draw hexagon pygame.draw.polygon(screen, HEXAGON_COLOR, vertices) # Update and draw balls for ball in balls: ball.update(vertices) ball.draw(screen) pygame.display.flip() clock.tick(60) pygame.quit()Explanation
- Hexagon Rotation: The hexagon rotates around its center using a
rotation_anglethat increments over time. - Ball Physics: Balls move with gravity applied and bounce off the hexagon walls and each other.
- Collision Detection: Balls detect collisions with the hexagon walls and other balls, updating their velocities accordingly.
- Color Scheme: The hexagon, background, and balls have a modern, appealing color palette.
Customization
- Adjust
HEX_RADIUSto change the size of the hexagon. - Modify
NUM_BALLSto add more or fewer balls. - Tweak
GRAVITYandBOUNCE_FACTORfor different physics behaviors.
Run the code, and you'll see a rotating hexagon with colorful balls bouncing inside it!
- Hexagon Rotation: The hexagon rotates around its center using a
- User
larger balls and balls intearacting with eachother can slowly or sometimes fastly phase through the hexagon and ground plane
- Assistant
To create a simulation where larger balls can sometimes phase through the hexagon walls and the ground plane, we need to modify the collision detection and response logic. We'll introduce a probability-based system where balls can occasionally "phase through" the walls instead of bouncing off. Additionally, we'll increase the ball size and adjust their interactions.
Here’s the updated code:
import pygame import math import random # Initialize pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 1000, 800 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Phasing Balls in Rotating Hexagon") # Colors HEXAGON_COLOR = (44, 62, 80) # Dark gray-blue BALL_COLORS = [(26, 188, 156), (46, 204, 113), (52, 152, 219), (155, 89, 182), (241, 196, 15)] # Appealing colors BACKGROUND_COLOR = (34, 34, 34) # Dark background # Hexagon parameters HEX_RADIUS = 300 HEX_CENTER = (WIDTH // 2, HEIGHT // 2) ROTATION_SPEED = 0.01 # Ball parameters NUM_BALLS = 8 BALL_RADIUS = 20 # Larger balls GRAVITY = 0.2 BOUNCE_FACTOR = 0.8 PHASE_THROUGH_PROBABILITY = 0.1 # 10% chance to phase through walls # Hexagon vertex calculation def calculate_hexagon_vertices(center, radius, angle): vertices = [] for i in range(6): x = center[0] + radius * math.cos(math.radians(60 * i + angle)) y = center[1] + radius * math.sin(math.radians(60 * i + angle)) vertices.append((x, y)) return vertices # Ball class class Ball: def __init__(self, x, y, radius, color): self.x = x self.y = y self.radius = radius self.color = color self.vx = random.uniform(-3, 3) self.vy = random.uniform(-3, 3) def draw(self, screen): pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius) def update(self, vertices): # Apply gravity self.vy += GRAVITY # Move ball self.x += self.vx self.y += self.vy # Collision with hexagon walls for i in range(6): p1 = vertices[i] p2 = vertices[(i + 1) % 6] d = self.distance_to_line_segment(p1, p2) if d <= self.radius: if random.random() < PHASE_THROUGH_PROBABILITY: # Phase through the wall continue else: # Bounce off the wall self.collide_with_wall(p1, p2) # Ball-to-ball collision for ball in balls: if ball != self and self.distance_to_ball(ball) <= self.radius + ball.radius: self.collide_with_ball(ball) # Ground plane collision (optional, for bounding the balls) if self.y + self.radius > HEIGHT: self.y = HEIGHT - self.radius if random.random() < PHASE_THROUGH_PROBABILITY: # Phase through the ground self.y += self.radius else: # Bounce off the ground self.vy = -self.vy * BOUNCE_FACTOR def distance_to_line_segment(self, p1, p2): x0, y0 = self.x, self.y x1, y1 = p1 x2, y2 = p2 px = x2 - x1 py = y2 - y1 norm = px * px + py * py u = ((x0 - x1) * px + (y0 - y1) * py) / float(norm) u = max(0, min(1, u)) # Clamp to segment bounds x = x1 + u * px y = y1 + u * py dx = x - x0 dy = y - y0 dist = math.sqrt(dx * dx + dy * dy) return dist def collide_with_wall(self, p1, p2): # Simple bounce logic self.vx = -self.vx * BOUNCE_FACTOR self.vy = -self.vy * BOUNCE_FACTOR def distance_to_ball(self, other_ball): return math.sqrt((self.x - other_ball.x) ** 2 + (self.y - other_ball.y) ** 2) def collide_with_ball(self, other_ball): # Elastic collision dx = other_ball.x - self.x dy = other_ball.y - self.y dist = self.distance_to_ball(other_ball) if dist != 0: dx /= dist dy /= dist self.vx -= dx * (self.vx - other_ball.vx) * 0.5 self.vy -= dy * (self.vy - other_ball.vy) * 0.5 other_ball.vx += dx * (self.vx - other_ball.vx) * 0.5 other_ball.vy += dy * (self.vy - other_ball.vy) * 0.5 # Initialize balls balls = [Ball(HEX_CENTER[0], HEX_CENTER[1], BALL_RADIUS, random.choice(BALL_COLORS)) for _ in range(NUM_BALLS)] # Main loop clock = pygame.time.Clock() rotation_angle = 0 running = True while running: screen.fill(BACKGROUND_COLOR) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Update rotation angle rotation_angle += ROTATION_SPEED # Calculate hexagon vertices vertices = calculate_hexagon_vertices(HEX_CENTER, HEX_RADIUS, math.degrees(rotation_angle)) # Draw hexagon pygame.draw.polygon(screen, HEXAGON_COLOR, vertices) # Update and draw balls for ball in balls: ball.update(vertices) ball.draw(screen) pygame.display.flip() clock.tick(60) pygame.quit()Key Changes:
- Larger Balls: Increased
BALL_RADIUSto 20 for larger balls. - Phasing Through Walls: Added a probability check (
PHASE_THROUGH_PROBABILITY) for balls to phase through the hexagon walls instead of bouncing. - Ground Plane Interaction: Added optional ground plane collision detection with a similar phasing probability.
- Increased Gravity: Adjusted
GRAVITYto 0.2 for more noticeable downward acceleration.
Customization:
- Adjust
PHASE_THROUGH_PROBABILITYto control how often balls phase through walls. - Modify
BALL_RADIUSto change the size of the balls. - Tweak
GRAVITYandBOUNCE_FACTORfor different physics behaviors.
This code creates a visually appealing simulation where larger balls interact with each other and occasionally phase through the rotating hexagon and the ground plane.
- Larger Balls: Increased
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.