100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python海龟绘图turtle

python海龟绘图turtle

时间:2023-12-29 13:28:50

相关推荐

python海龟绘图turtle

turtle是python比较好用的画图模块,通过控制pen的移动,很方便实现画图功能。常用的函数如下表:

e.g:

#coding:utf-8import turtleimport matht = turtle.Turtle()def setpen(x,y):t.penup()t.goto(x,y)t.pendown()t.setheading(0)t.pensize(2)t.speed(10)def circle(r,color):start_x = 0start_y = -rsetpen(start_x, start_y)t.pencolor(color)t.fillcolor(color)t.begin_fill()t.circle(r)t.end_fill()def five_star(r,color):setpen(0,0)t.pencolor(color)t.setheading(162)t.forward(r)t.setheading(0)t.fillcolor(color)t.begin_fill()for i in range(5):t.forward(math.cos(math.radians(18))*2*r) #2cos18°*rt.right(144)t.end_fill()t.hideturtle()if __name__ == '__main__':circle(288,'crimson')circle(234,'snow')circle(174,'crimson')circle(114,'blue')five_star(114,'snow')input()turtle.done()

e.g:

#coding=utf-8import turtlefrom numpy import linspace,sin,cosfrom math import pidef draw_point(self,x,y):self.penup()self.goto(x,y)self.pendown()self.forward(0)if __name__ == '__main__':window = turtle.Screen()window.title('Heart')window.setworldcoordinates(-35,-35,30,30)window.bgcolor('light blue')t = turtle.Turtle()t.shape('turtle')t.color('pink','red')t.width(1)t.pencolor('red')t.pensize(10)t.begin_fill()t.getscreen().tracer(10,0)l = linspace(0,2 * pi,1000)for i in l:x = 24 * pow(sin(i),3)y = 24 * cos(i) - 8 *cos(2 * i) - 4 *cos(3 * i) - cos(4 *i)draw_point(t,x,y)t.end_fill()t.penup()window.exitonclick()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。