Gemini 2.0 Pro Experimental Generated Code
This code is generated by Gemini 2.0 Pro Experimentalunknown
plain_text
9 months ago
3.7 kB
39
Indexable
import pygame
import pymunk
import pymunk.pygame_util
import math
# Initialize Pygame
pygame.init()
# Screen dimensions
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Bouncing Ball in Spinning Hexagon")
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# Physics setup
space = pymunk.Space()
space.gravity = (0, 981) # Gravity pointing downwards. pymunk uses a coordinate system where +y is down.
friction = 0.9 # Add some friction to the ball
elasticity = 0.8 # Control elasticity of bounces
# Function to create hexagon vertices
def hexagon_vertices(center, radius, rotation):
vertices = []
for i in range(6):
angle = math.radians(i * 60 + rotation)
x = center[0] + radius * math.cos(angle)
y = center[1] + radius * math.sin(angle)
vertices.append(pymunk.Vec2d(x, y)) # Use Vec2d for pymunk compatibility
return vertices
# Hexagon properties
hexagon_center = (width // 2, height // 2)
hexagon_radius = 150
hexagon_rotation = 0 # Initial rotation angle
hexagon_rotation_speed = 1 # Radians per second (adjust for speed)
# Create hexagon segments (dynamic for collision)
def create_hexagon_segments(space, center, radius, rotation):
vertices = hexagon_vertices(center, radius, rotation)
segments = []
for i in range(6):
body = pymunk.Body(body_type=pymunk.Body.KINEMATIC) # Use a kinematic body
segment = pymunk.Segment(body, vertices[i], vertices[(i + 1) % 6], 5)
segment.elasticity = elasticity
segment.friction = friction
space.add(body, segment)
segments.append((body, segment)) # Store body and segment
return segments
hexagon_segments = create_hexagon_segments(space, hexagon_center, hexagon_radius, hexagon_rotation)
# Ball properties
ball_radius = 20
ball = pymunk.Body(1, 100) # Mass and moment of inertia
ball.position = (width // 2, height // 4) # Start position
circle = pymunk.Circle(ball, ball_radius)
circle.friction = friction
circle.elasticity = elasticity
space.add(ball, circle)
# Function to draw the hexagon
def draw_hexagon(screen, vertices):
pygame.draw.polygon(screen, white, [(v.x, v.y) for v in vertices], 2) #Convert Vec2d to tuples
# Game loop
running = True
clock = pygame.time.Clock()
dt = 0 # Used to store the time change
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen
screen.fill(black)
# Update hexagon rotation
hexagon_rotation += hexagon_rotation_speed * dt
hexagon_rotation %= 360 # Keep rotation within 0-360 degrees
# Get hexagon vertices based on current rotation
vertices = hexagon_vertices(hexagon_center, hexagon_radius, hexagon_rotation)
# Update hexagon segment positions. Kinematic bodies are moved by setting their position/angle
for i in range(len(hexagon_segments)):
body, segment = hexagon_segments[i]
body.position = (0,0) # Kinematic bodies are moved directly
body.angle = math.radians(hexagon_rotation) # Set the rotation of segments.
segment.a = vertices[i]
segment.b = vertices[(i+1)%6]
# Draw the hexagon
draw_hexagon(screen, vertices)
# Draw the ball
pygame.draw.circle(screen, red, (int(ball.position.x), int(ball.position.y)), ball_radius)
# Update physics
space.step(dt)
# Update the display
pygame.display.flip()
# Limit frame rate
dt = clock.tick(60) / 1000.0 # Get the time change in seconds (for 60 FPS)
pygame.quit()Editor is loading...
Leave a Comment