Python画四个等距的正方形
时间: 2023-09-25 07:13:02 浏览: 44
可以使用Python的turtle库来绘制四个等距的正方形,代码如下:
```python
import turtle
# 定义画正方形的函数
def draw_square(length):
for i in range(4):
turtle.forward(length)
turtle.left(90)
# 设置初始位置
start_x, start_y = -200, 0
# 循环画四个正方形
for i in range(4):
turtle.penup()
turtle.goto(start_x + i * 100, start_y)
turtle.pendown()
draw_square(80)
# 隐藏海龟
turtle.hideturtle()
# 等待用户关闭窗口
turtle.done()
```
这段代码定义了一个名为draw_square的函数,用于绘制正方形。然后通过循环调用这个函数,画出四个等距的正方形。最后隐藏海龟,等待用户关闭窗口。
相关问题
python画四个小正方形并填充颜色
你可以使用Python的turtle库来画四个小正方形并填充颜色,具体代码如下:
```python
import turtle
# 设置画笔大小和颜色
turtle.pensize(5)
turtle.pencolor('blue')
# 画第一个小正方形
turtle.begin_fill()
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.end_fill()
# 画第二个小正方形
turtle.penup()
turtle.goto(70, 0)
turtle.pendown()
turtle.begin_fill()
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.end_fill()
# 画第三个小正方形
turtle.penup()
turtle.goto(0, 70)
turtle.pendown()
turtle.begin_fill()
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.end_fill()
# 画第四个小正方形
turtle.penup()
turtle.goto(70, 70)
turtle.pendown()
turtle.begin_fill()
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 等待用户关闭窗口
turtle.done()
```
上述代码中,我们使用了turtle库的各种函数来绘制四个小正方形,并使用begin_fill()和end_fill()函数来填充颜色。你可以根据需要修改画笔大小、颜色和填充颜色等参数。
用Python画一个无角正方形
要用Python画一个无角正方形,可以使用turtle库来实现。以下是一个示例代码:
```python
import turtle
# 创建一个画布和画笔
canvas = turtle.Screen()
pen = turtle.Turtle()
# 设置画笔的形状和颜色
pen.shape("turtle")
pen.color("blue")
# 移动画笔并绘制正方形
side_length = 100 # 正方形的边长
for _ in range(4):
pen.forward(side_length)
pen.right(90)
# 关闭画布
canvas.exitonclick()
```
这段代码使用turtle库创建了一个画布和一个画笔,然后设置了画笔的形状为海龟,并将颜色设置为蓝色。接下来,通过循环移动画笔并绘制四条边,每条边的长度都是100个像素,从而完成了无角方形的绘制。最后,通过点击画布来关闭程序。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)