编程海龟图三角形怎么画

时间:2025-01-25 16:24:12 游戏攻略

方法一:使用基本命令

```python

import turtle

创建画笔

pen = turtle.Pen()

画三角形

pen.forward(100) 前进100像素

pen.left(120) 向左转120度

pen.forward(100) 再前进100像素

pen.left(120) 再向左转120度

pen.forward(100) 再前进100像素

结束绘制

turtle.done()

```

方法二:使用循环

```python

import turtle

创建画笔

t = turtle.Pen()

使用循环画三角形

for _ in range(3):

t.forward(100) 前进100像素

t.right(120) 向右转120度

结束绘制

turtle.done()

```

方法三:指定三个点绘制并填充三角形

```python

import turtle

定义三个点的坐标

points = [(-200, -100), (0, 200), (200, -100)]

设置填充颜色

color = "orange"

绘制并填充三角形

turtle.color(color)

turtle.penup()

turtle.goto(points)

turtle.pendown()

turtle.begin_fill()

turtle.goto(points)

turtle.goto(points)

turtle.goto(points)

turtle.end_fill()

结束绘制

turtle.done()

```

方法四:绘制等腰三角形

```python

import turtle

设置标题和背景颜色

turtle.title('画一个等腰三角形')

turtle.bgcolor('red')

设置画布大小和画笔粗细

turtle.screensize(800, 800)

turtle.pensize(10)

隐藏画笔

turtle.hideturtle()

画等腰三角形

turtle.pendown()

turtle.color('blue', 'yellow')

turtle.begin_fill()

turtle.forward(300)

turtle.left(120)

turtle.forward(300)

turtle.left(120)

turtle.forward(300)

turtle.end_fill()

结束绘制

turtle.done()

```

这些方法都可以用来绘制三角形,你可以根据自己的需求和喜好选择合适的方法。