用python的turtle画一个黄色的长方形
时间: 2023-10-26 18:07:15 浏览: 106
好的,可以使用Python自带的turtle库来画图,下面是实现代码:
```python
import turtle
# 创建画布和画笔
canvas = turtle.Screen()
pen = turtle.Turtle()
# 设置画笔颜色和填充颜色
pen.color("yellow")
pen.fillcolor("yellow")
# 开始填充
pen.begin_fill()
# 画长方形
pen.forward(200)
pen.left(90)
pen.forward(100)
pen.left(90)
pen.forward(200)
pen.left(90)
pen.forward(100)
# 结束填充
pen.end_fill()
# 隐藏画笔
pen.hideturtle()
# 点击窗口关闭程序
canvas.exitonclick()
```
运行代码后,会弹出一个画布,上面会画出一个黄色的长方形。
相关问题
用python的turtle在黄色的长方形里画一个简易房子
好的,以下是Python代码实现:
```python
import turtle
# 创建画布和画笔
screen = turtle.Screen()
pen = turtle.Turtle()
# 设置画笔的颜色和填充颜色
pen.color('black', 'yellow')
# 开始填充长方形
pen.begin_fill()
# 绘制长方形
pen.forward(600)
pen.right(90)
pen.forward(400)
pen.right(90)
pen.forward(600)
pen.right(90)
pen.forward(400)
# 结束填充
pen.end_fill()
# 绘制房子
pen.penup()
pen.goto(150, 0)
pen.pendown()
pen.color('black', 'white')
pen.begin_fill()
pen.forward(300)
pen.right(90)
pen.forward(200)
pen.right(90)
pen.forward(300)
pen.right(90)
pen.forward(200)
pen.end_fill()
# 绘制屋顶
pen.penup()
pen.goto(150, 200)
pen.pendown()
pen.color('black', 'red')
pen.begin_fill()
pen.right(45)
pen.forward(212)
pen.right(90)
pen.forward(212)
pen.end_fill()
pen.right(135)
# 绘制门和窗户
pen.penup()
pen.goto(220, -50)
pen.pendown()
pen.color('black', 'brown')
pen.begin_fill()
pen.forward(60)
pen.right(90)
pen.forward(100)
pen.right(90)
pen.forward(60)
pen.end_fill()
pen.penup()
pen.goto(270, -100)
pen.pendown()
pen.color('black', 'light blue')
pen.begin_fill()
pen.circle(30)
pen.end_fill()
# 隐藏画笔
pen.hideturtle()
# 完成绘制
turtle.done()
```
运行以上代码,你就可以在窗口中看到绘制的黄色长方形和一个简易房子。
用python的turtle画一个宽400长600的黄色长方形
好的,以下是Python代码实现:
```python
import turtle
# 创建画布和画笔
screen = turtle.Screen()
pen = turtle.Turtle()
# 设置画笔的颜色和填充颜色
pen.color('black', 'yellow')
# 开始填充长方形
pen.begin_fill()
# 绘制长方形
pen.forward(600)
pen.right(90)
pen.forward(400)
pen.right(90)
pen.forward(600)
pen.right(90)
pen.forward(400)
# 结束填充
pen.end_fill()
# 隐藏画笔
pen.hideturtle()
# 完成绘制
turtle.done()
```
运行以上代码,你就可以在窗口中看到绘制的黄色长方形了。
阅读全文