龙空技术网

少儿学编程系列---使用python turtle画基本图形

cloudcoder 180

前言:

眼前各位老铁们对“python画图形状代码”都比较讲究,看官们都想要知道一些“python画图形状代码”的相关内容。那么小编也在网上汇集了一些关于“python画图形状代码””的相关内容,希望看官们能喜欢,大家快快来了解一下吧!

turtle简介

turtle(海龟)绘图很适合用来引导少儿学习编程,通过简单的命令,可以轻松地绘制出精美的形状和图案。

常用命令介绍

更多命令可以参见:

forward() | fd() 前进backward() | bk() | back() 后退right() | rt() 右转left() | lt() 左转goto() | setpos() | setposition() 前往/定位setx() 设置x坐标sety() 设置y坐标setheading() | seth() 设置朝向home() 返回原点circle() 画圆dot() 画点stamp() 印章clearstamp() 清除印章clearstamps() 清除多个印章undo() 撤消speed() 速度
画基本图形的效果画基本图形的代码
from turtle import *import turtlefrom math import *def draw_star():    down()    for i in range(5):        forward(100)        right(144)def draw_square():    down()    for i in range(4):        forward(90)        right(90)def draw_hexagon():    down()    num_sides = 6    side_length = 50    angle = 360.0 / num_sides    for i in range(num_sides):        forward(side_length)        right(angle)def draw_rectangle():    down()    for i in range(2):        forward(140)        right(90)        forward(100)        right(90)def draw_nested_square(size):    down()    for i in range(7):        for j in range(4):            fd(size)            right(90)            size = size - 5def draw_multi_circls():    down()    speed(50)    pensize(1)    for i in range(1, 31):        circle(2 * i)        circle(-2 * i)        left(i)    speed(2)def draw_special_pic():    down()    speed(120)    pensize(1)    colors = ["red", "purple", "blue", "green", "orange", "yellow"]    for x in range(60):        pencolor(colors[x % 6])        width(x / 12 + 1)        forward(x*2)        left(59)    speed(2)def main():    speed(20)    bgcolor("white")    pencolor("green")    pensize(8)    turtle.Screen().setup(width=800, height=600, startx=0, starty=0)    up()    goto(-380, 220)    draw_star()    up()    goto(-230, 250)    draw_square()    up()    goto(-70, 250)    draw_hexagon()    up()    goto(60, 250)    draw_rectangle()    up()    goto(-380, 50)    draw_nested_square(146)    up()    goto(-100, 15)    draw_multi_circls()    up()    goto(130, 15)    draw_special_pic()    done()if __name__ == "__main__":    hideturtle()    up()    goto(-260, 270)    write("今天头条-cloudcoder出品", align='left', font=('fangsong',14, 'normal'))    showturtle()    main()

标签: #python画图形状代码 #用python画图形