python
import pygame
import math
import sys
# 初始化 Pygame
pygame.init()
# 设置屏幕参数
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("旋转六边形内的弹跳球")
# 颜色定义
BACKGROUND_COLOR = (255, 255, 255)
HEXAGON_COLOR = (0, 0, 0)
BALL_COLOR = (255, 0, 0)
# 六边形参数
HEX_RADIUS = 200 # 六边形外接圆半径
HEX_CENTER = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
ROTATION_SPEED = 1 # 旋转速度(度/帧)
# 小球参数
BALL_RADIUS = 20
BALL_MASS = 1.0
GRAVITY = 0.5 # 重力加速度(向下)
RESTITUTION = 0.8 # 碰撞弹性系数(0-1)
FRICTION = 0.99 # 摩擦系数
class RotatingHexagon:
def __init__(self, center, radius):
self.center = center
self.radius = radius
self.angle = 0 # 当前旋转角度(度)
def get_vertices(self):
"""计算当前旋转角度下的六个顶点坐标"""
return [
(
self.center[0] + self.radius * math.cos(math.radians(60 * i + self.angle)),
self.center[1] + self.radius * math.sin(math.radians(60 * i + self.angle))
)
for i in range(6)
]
def rotate(self):
"""更新旋转角度"""
self.angle = (self.angle + ROTATION_SPEED) % 360
class Ball:
def __init__(self, position, radius):
self.pos = list(position)
self.vel = [0.0, 0.0]
self.radius = radius
def apply_force(self, force):
"""应用外力(如重力)"""
self.vel[0] += force[0] / BALL_MASS
self.vel[1] += force[1] / BALL_MASS
def update_position(self):
"""更新位置"""
self.pos[0] += self.vel[0]
self.pos[1] += self.vel[1]
def closest_point_on_segment(p, a, b):
"""计算点 p 到线段 ab 的最近点"""
ap = (p[0] - a[0], p[1] - a[1])
ab = (b[0] - a[0], b[1] - a[1])
t = (ap[0] * ab[0] + ap[1] * ab[1]) / (ab[0] ** 2 + ab[1] ** 2 + 1e-6)
t = max(0, min(1, t))
return (
a[0] + t * ab[0],
a[1] + t * ab[1]
)
def resolve_collision(ball, hex_vertices):
"""处理小球与六边形边的碰撞"""
for i in range(6):
# 获取当前边和法向量
a = hex_vertices[i]
b = hex_vertices[(i + 1) % 6]
# 计算线段方向向量
edge_dir = (b[0] - a[0], b[1] - a[1])
edge_length = math.hypot(edge_dir[0], edge_dir[1])
if edge_length < 1e-6:
continue
# 计算法向量(指向六边形内部)
normal = (-edge_dir[1] / edge_length, edge_dir[0] / edge_length)
# 计算小球中心到线段的最短距离
closest = closest_point_on_segment(ball.pos, a, b)
distance = math.hypot(ball.pos[0] - closest[0], ball.pos[1] - closest[1])
# 如果发生穿透
if distance < BALL_RADIUS:
# 计算穿透深度
penetration = BALL_RADIUS - distance
# 修正位置:将小球推回内部
ball.pos[0] += normal[0] * penetration
ball.pos[1] += normal[1] * penetration
# 计算速度的垂直分量(沿法线方向)
vel_normal = ball.vel[0] * normal[0] + ball.vel[1] * normal[1]
# 如果速度方向朝外,则进行反弹
if vel_normal < 0:
# 应用弹性碰撞
ball.vel[0] -= (1 + RESTITUTION) * vel_normal * normal[0]
ball.vel[1] -= (1 + RESTITUTION) * vel_normal * normal[1]
# 初始化对象
hexagon = RotatingHexagon(HEX_CENTER, HEX_RADIUS)
ball = Ball(HEX_CENTER, BALL_RADIUS)
# 主循环
clock = pygame.time.Clock()
running = True
while running:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 物理更新
ball.apply_force((0, GRAVITY * BALL_MASS)) # 应用重力
ball.update_position() # 更新小球位置
ball.vel[0] *= FRICTION # 应用摩擦力
ball.vel[1] *= FRICTION
hexagon.rotate() # 旋转六边形
current_vertices = hexagon.get_vertices() # 获取当前六边形顶点
resolve_collision(ball, current_vertices) # 处理碰撞
# 图形绘制
screen.fill(BACKGROUND_COLOR)
pygame.draw.polygon(screen, HEXAGON_COLOR, current_vertices, 2) # 绘制六边形
pygame.draw.circle(screen, BALL_COLOR,
(int(ball.pos[0]), int(ball.pos[1])),
BALL_RADIUS) # 绘制小球
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()