python绘制红绿灯图像代码
时间: 2023-07-22 18:06:52 浏览: 234
红绿灯编写程序
以下是使用Python绘制红绿灯的代码示例:
```python
import turtle
# 设置画布大小
turtle.setup(500, 500)
# 画圆函数
def draw_circle(color, radius, x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(radius)
turtle.end_fill()
# 画红灯
def draw_red_light():
draw_circle("red", 50, 0, 0)
# 画黄灯
def draw_yellow_light():
draw_circle("yellow", 50, 0, -100)
# 画绿灯
def draw_green_light():
draw_circle("green", 50, 0, -200)
# 画红绿灯框架
def draw_traffic_light():
turtle.speed(0)
turtle.hideturtle()
turtle.pensize(3)
turtle.penup()
turtle.goto(-50, 250)
turtle.pendown()
turtle.goto(-50, -250)
turtle.goto(50, -250)
turtle.goto(50, 250)
turtle.goto(-50, 250)
# 测试
draw_traffic_light()
draw_red_light()
turtle.done()
```
你可以在上面的代码中根据需要进行更改,例如修改灯的颜色或位置等。
阅读全文