中式花格是一个由随机长方形,正方形,圆形组成的图案,请用python程序编写一个中式花格图案。
时间: 2024-05-07 17:23:53 浏览: 219
中式花格图库
好的,我可以使用Python中的turtle模块来绘制中式花格图案。以下是示例代码:
```python
import turtle
import random
# 随机生成长方形、正方形、圆形
def draw_shape(length, shape):
turtle.pendown()
turtle.color('black', 'white')
turtle.begin_fill()
if shape == 'rectangle':
turtle.forward(length)
turtle.right(90)
turtle.forward(length/2)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(length/2)
elif shape == 'square':
turtle.forward(length)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(length)
elif shape == 'circle':
turtle.circle(length/2)
turtle.end_fill()
turtle.penup()
# 绘制中式花格图案
def draw_pattern(width, height):
turtle.speed(0)
turtle.hideturtle()
turtle.penup()
turtle.setpos(-width/2, -height/2)
shapes = ['rectangle', 'square', 'circle']
for x in range(0, width, 50):
for y in range(0, height, 50):
shape = random.choice(shapes)
draw_shape(50, shape)
turtle.setpos(-width/2 + x, -height/2 + y)
turtle.done()
# 调用函数绘制中式花格图案
draw_pattern(500, 500)
```
该程序使用turtle模块绘制随机生成的长方形、正方形、圆形来组成中式花格图案。运行后,程序将生成一个500x500的中式花格图案。
阅读全文