无我

在小学尝试以Python为载体培养学生计算思维

试了一下递归二叉树算法:

from turtle import *

import random

tracer(0)

setup(800,800)

up()

goto(0,-400)

left(90)

down()

pensize(20)

shuColor="tomato3"

guanColor="MediumPurple2"

bgcolor("lightblue")

color(shuColor)

fd(300)


def twoTree(level,chang,jiao,size):

    if  level==0:

        return


    if level<=3:

        color(guanColor)

    else:

        color(shuColor)

    pensize(size)

    

    lt(jiao/2)

    fd(chang)

    

    twoTree(level-1,chang*(random.randint(6,10)/10),jiao*0.9,size*0.6)


    up()

    bk(chang)

    rt(jiao/2)

    down()

    

    if level<=3:

        color(guanColor)

    else:

        color(shuColor)

    pensize(size)

    rt(jiao/2)

    fd(chang)

    

    twoTree(level-1,chang*(random.randint(6,10)/10),jiao*0.9,size*0.6)

    

    up()

    bk(chang)

    down()

    lt(jiao/2)

    

twoTree(12,100,60,20*0.6)    

评论 ( 1 )
热度 ( 1 )

© 无我 | Powered by LOFTER