模拟蜂巢构建:使用 turtle 绘制出六边形的蜂巢结构,通过循环和角度控制,构建出复杂的蜂巢图案,模拟蜜蜂建造蜂巢的过程。
import turtle
# 设置画布和画笔
screen = turtle.Screen()
screen.bgcolor("lightyellow")
pen = turtle.Turtle()
pen.speed(0)
pen.color("brown")
pen.pensize(2)
# 定义绘制六边形的函数
def draw_hexagon(size):
for _ in range(6):
pen.forward(size)
pen.left(60)
# 定义绘制圆形蜂窝的函数
def draw_circular_honeycomb(size):
center_x = 0
# 调整起始的 y 坐标,让结构整体靠上
center_y = 150
rows = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]
vertical_spacing = size * 1.5
horizontal_spacing = size * 1.732
for i, num_hexagons in enumerate(rows):
y = center_y - i * vertical_spacing
start_x = center_x - (num_hexagons - 1) * horizontal_spacing / 2
for j in range(num_hexagons):
x = start_x + j * horizontal_spacing
pen.penup()
pen.goto(x, y)
pen.pendown()
draw_hexagon(size)
# 设置六边形大小
hexagon_size = 30
# 调用绘制圆形蜂窝的函数
draw_circular_honeycomb(hexagon_size)
# 完成绘制
turtle.done()