这个主要是通过turtle库去实现的一个画不重复圆的的功能,欢迎大家来了解,如果对你的学习有帮助的话,请留下一个赞和收藏给笔者鼓励一下哈
import turtle
import random
# 设置画布尺寸为1600*800
turtle.setup(100, 100)
# 创建Turtle对象
t = turtle.Turtle()
# 设置画笔速度和形状
t.speed(0)
t.shape("circle")
# 定义函数来生成随机圆形
def draw_circle():
# 生成随机半径和颜色
radius = random.randint(min(15, 40), max(15, 40))
color = random.choice(["red", "blue", "green", "yellow", "orange", "purple"])
# 在随机位置绘制圆形
x = random.randint(-740, 740)
y = random.randint(-340, 340)
t.penup()
t.goto(x, y)
t.pendown()
t.fillcolor(color)
t.begin_fill()
t.circle(radius)
t.end_fill()
# 统计最多可以填充多少个不重叠的圆形
count = 0
while True:
# 检查圆形是否重叠
overlapping = False
# 绘制圆形
draw_circle()
count += 1
for c in t.getscreen().turtles():
if c != t and c.distance(t) < 2 * max(15, 40):
overlapping = True
break
# 如果圆形重叠,则停止绘制
if overlapping:
break
# 输出最多可以填充的圆形数量
print("最多可以填充的圆形数量:", count - 1)
# 点击关闭窗口退出程序
turtle.done()