python turtle创意作品
时间: 2023-09-27 10:03:58 浏览: 259
以下是一些Python Turtle的创意作品:
1. 绘制彩色螺旋线:使用Python Turtle绘制不同颜色的线条,创建一个漂亮的彩色螺旋图案。
```
import turtle
import random
turtle.speed(0)
turtle.bgcolor('black')
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
for i in range(150):
turtle.color(random.choice(colors))
turtle.pensize(i/10 + 1)
turtle.forward(i)
turtle.left(59)
```
2. 绘制海龟赛跑游戏:使用Python Turtle创建一个简单的海龟赛跑游戏,让玩家控制一个小海龟,尽可能快地穿过障碍物。
```
import turtle
import random
# Set up the screen
screen = turtle.Screen()
screen.setup(500, 500)
screen.title("Turtle Race")
# Create the turtles
turtles = []
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
for i in range(6):
turtle = turtle.Turtle()
turtle.shape('turtle')
turtle.color(colors[i])
turtle.penup()
turtle.goto(-230, 200 - i*50)
turtles.append(turtle)
# Create the finish line
finish = turtle.Turtle()
finish.penup()
finish.goto(230, 200)
finish.pendown()
finish.goto(230, -200)
# Start the race
while True:
for turtle in turtles:
turtle.forward(random.randint(1, 5))
if turtle.xcor() >= 230:
winner = turtle.color()[0]
print("The winner is", winner)
turtle.goto(-100, 0)
turtle.write("Winner!", font=("Arial", 16, "bold"))
break
```
3. 绘制谢尔宾斯基三角形:使用Python Turtle绘制一个谢尔宾斯基三角形,这是一个典型的分形图形。
```
import turtle
def draw_triangle(points, color, turtle):
turtle.fillcolor(color)
turtle.up()
turtle.goto(points[0][0], points[0][1])
turtle.down()
turtle.begin_fill()
turtle.goto(points[1][0], points[1][1])
turtle.goto(points[2][0], points[2][1])
turtle.goto(points[0][0], points[0][1])
turtle.end_fill()
def get_mid(p1, p2):
return ((p1[0]+p2[0])/2, (p1[1]+p2[1])/2)
def sierpinski(points, degree, turtle):
colormap = ['blue', 'red', 'green', 'white', 'yellow',
'violet', 'orange']
draw_triangle(points, colormap[degree], turtle)
if degree > 0:
sierpinski([points[0],
get_mid(points[0], points[1]),
get_mid(points[0], points[2])],
degree-1, turtle)
sierpinski([points[1],
get_mid(points[0], points[1]),
get_mid(points[1], points[2])],
degree-1, turtle)
sierpinski([points[2],
get_mid(points[2], points[1]),
get_mid(points[0], points[2])],
degree-1, turtle)
turtle.setup(500, 500)
turtle.title("Sierpinski Triangle")
my_turtle = turtle.Turtle()
my_turtle.speed(0)
my_points = [[-200, -100], [0, 200], [200, -100]]
sierpinski(my_points, 3, my_turtle)
turtle.done()
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)