如何利用python的turtle模块绘制各种多边形

发布于:2022-10-23 ⋅ 阅读:(311) ⋅ 点赞:(0)

本实例中要求编写一个python程序,掌握对turtle模块中绘制图形方法的使用

 

我们可以查阅到官方文档中的turtle中的文档,阅读相应的英文。

Turtle graphics is a popular way for introducing programming to
kids. It was part of the original Logo programming language developed
by Wally Feurzig and Seymour Papert in 1966.

Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it
the command turtle.forward(15), and it moves (on-screen!) 15 pixels in
the direction it is facing, drawing a line as it moves. Give it the
command turtle.right(25), and it rotates in-place 25 degrees clockwise.

By combining together these and similar commands, intricate shapes and
pictures can easily be drawn.

 实例:绘制出一个多边形

import turtle
import time
i = 0
while(i<12):
    turtle.forward(100)
    turtle.right(200)
    time.sleep(2)
    i+=1

代码运行的成果:

总结:

1.turtle.forward(100)沿着箭头朝着的方向,向前移动100像素的距离,整个过程中箭头的朝向都没有发生变化。

2.turtle.right(200)箭头的朝向向右边偏移200度,没有产生位移。

3.forward方法和right()方法时turtle模块里面的一个很常用的两个方法,我们几乎可以利用它来绘制出所有的图形

就可以绘制出想要的各种多边形了,如果你还对机器学习,深度学习,数据结构和算法都很喜欢的话,可以订阅我的专栏,最后点个关注再走呗


网站公告

今日签到

点亮在社区的每一天
去签到