完成一个图形变换的程序,系统具有 4 种可选择的功能,按字母“Y”画一个用点组成的 圆;按字母“S”画一个用不同颜色填充的三角形;按字母“Z”画一个用不同颜色填充的 矩形;按“q”键退出
时间: 2023-09-17 11:12:47 浏览: 32
好的,这是一个简单的图形变换程序,你可以在命令行中输入 Y、S、Z 来选择不同的图形,输入 q 退出程序。以下是代码实现:
```python
import turtle
# 定义画圆函数
def draw_circle():
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.pencolor("black")
turtle.circle(50)
# 定义画三角形函数
def draw_triangle():
turtle.penup()
turtle.goto(-50, -50)
turtle.pendown()
turtle.pencolor("red")
turtle.fillcolor("yellow")
turtle.begin_fill()
for i in range(3):
turtle.forward(100)
turtle.left(120)
turtle.end_fill()
# 定义画矩形函数
def draw_rectangle():
turtle.penup()
turtle.goto(-50, -50)
turtle.pendown()
turtle.pencolor("blue")
turtle.fillcolor("green")
turtle.begin_fill()
for i in range(2):
turtle.forward(100)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.end_fill()
# 主函数
def main():
turtle.speed(0)
while True:
choice = input("请选择要画的图形(Y-圆形,S-三角形,Z-矩形,q-退出):")
if choice == "Y":
draw_circle()
elif choice == "S":
draw_triangle()
elif choice == "Z":
draw_rectangle()
elif choice == "q":
turtle.bye() # 退出turtle
break
else:
print("输入无效,请重新输入!")
if __name__ == "__main__":
main()
```
你可以复制以上代码到你的 Python 解释器中运行,然后输入 Y、S、Z 来选择不同的图形,输入 q 退出程序。