turtle绘制摩天轮
时间: 2023-11-13 18:21:22 浏览: 107
python绘制玫瑰(turtle).zip
5星 · 资源好评率100%
下面是用turtle绘制摩天轮的Python代码:
```python
import turtle
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor("#e9e9e9")
# 定义变量
radius = 150 # 摩天轮半径
angle = 360 / 8 # 摩天轮每个车厢之间的夹角
car_width = 50 # 车厢宽度
car_height = 80 # 车厢高度
car_color = ["#ff0000", "#ffff00", "#00ff00", "#00ffff", "#0000ff", "#ff00ff", "#9900cc", "#33ccff"] # 车厢颜色
# 绘制摩天轮车厢
def draw_car(x, y, color):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(0)
turtle.fillcolor(color)
turtle.begin_fill()
turtle.forward(car_width / 2)
turtle.left(90)
turtle.forward(car_height)
turtle.left(90)
turtle.forward(car_width)
turtle.left(90)
turtle.forward(car_height)
turtle.left(90)
turtle.forward(car_width / 2)
turtle.end_fill()
# 绘制摩天轮
def draw_ferris_wheel(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(0)
turtle.pensize(3)
turtle.color("#333333")
turtle.circle(radius)
for i in range(8):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(i * angle)
draw_car(x + radius * 0.8 * turtle.cos(i * angle), y + radius * 0.8 * turtle.sin(i * angle), car_color[i])
# 绘制摩天轮支架
def draw_support(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(0)
turtle.pensize(5)
turtle.color("#333333")
turtle.forward(radius * 1.2)
turtle.backward(radius * 0.1)
turtle.right(90)
turtle.forward(50)
turtle.backward(100)
turtle.forward(50)
turtle.left(90)
turtle.forward(radius * 0.1)
turtle.backward(radius * 1.1)
# 调用函数绘制摩天轮
draw_ferris_wheel(0, 0)
draw_support(0, -radius * 1.2)
# 隐藏海龟
turtle.hideturtle()
# 显示绘图窗口
turtle.mainloop()
```
运行代码,即可看到绘制的摩天轮:
![](https://cdn.luogu.com.cn/upload/image_hosting/gtdf7q2b.png)
阅读全文