Học python với turtle qua các ví dụ


  • administrators

    Ví dụ 1:

    import turtle
     
    tina = turtle.Turtle()
    tina.shape("turtle")
    tina.width(4)
    tina.penup()
    tina.goto(0,60)
    tina.pendown()
    tina.color("blue")
     
    tommy = turtle.Turtle()
    tommy.penup()
    tommy.shape("turtle")
    tommy.goto(-50,-90)
    tommy.left(90)
     
    for i in range(5):
        tina.right(120)
        tina.forward(130)
        tina.right(120)
        tina.forward(130)
        tina.right(120)
        tina.forward(130)
        tommy.write("%d done, %d to go!" %(i+1, 4-i), None, None, "12pt bold")
        tommy.backward(15)
        tina.right(72)
     
    tommy.goto(-55,-175)
    tommy.write("Great job, Tina!", None, None, "16pt bold")
    tina.color("purple")
    tommy.goto(-75,-175)
    tina.penup()
    tina.forward(40)
    tina.write("Thanks!", None, None, "16pt bold")
    tina.backward(40)
    

    Ví dụ 2:

    import math
    import turtle
     
    def drawPhyllotacticPattern(turtle, t, petalstart, angle = 137.508, size = 2, cspread = 4 ):
        """print a pattern of circles using spiral phyllotactic data"""
        # initialize position
        # turtle.pen(outline=1,pencolor="black",fillcolor="orange")
        turtle.color('black')
        turtle.fillcolor("orange")
        phi = angle * ( math.pi / 180.0 )
        xcenter = 0.0
        ycenter = 0.0
     
        # for loops iterate in this case from the first value until < 4, so
        for n in range (0,t):
            r = cspread * math.sqrt(n)
            theta = n * phi
           
            x = r * math.cos(theta) + xcenter
            y = r * math.sin(theta) + ycenter
     
            # move the turtle to that position and draw
            turtle.up()
            turtle.setpos(x,y)
            turtle.down()
            # orient the turtle correctly
            turtle.setheading(n * angle)
            if n > petalstart-1:
                turtle.color("yellow")
                drawPetal(turtle,x,y)
            else: turtle.stamp()
               
     
    def drawPetal(turtle, x, y ):
        turtle.penup()
        turtle.goto(x,y)
        turtle.pendown()
        turtle.color('black')
        turtle.fillcolor('yellow')
        turtle.begin_fill()
        turtle.right(20)
        turtle.forward(70)
        turtle.left(40)
        turtle.forward(70)
        turtle.left(140)
        turtle.forward(70)
        turtle.left(40)
        turtle.forward(70)
        turtle.penup()
        turtle.end_fill() # this is needed to complete the last petal
     
     
    tina = turtle.Turtle()
    tina.shape("turtle")
    tina.speed(0) # make the turtle go as fast as possible
    drawPhyllotacticPattern(tina, 200, 160, 137.508 )
    tina.penup()
    tina.forward(1000)
    # tina.exitonclick() # lets you x out of the window when outside of idle
    

    Ví dụ 3:

    import math
    import turtle
     
    def drawPhyllotacticPattern(turtle, t, petalstart, angle = 137.508, size = 2, cspread = 4 ):
        """print a pattern of circles using spiral phyllotactic data"""
        # initialize position
        # turtle.pen(outline=1,pencolor="black",fillcolor="orange")
        turtle.color('black')
        turtle.fillcolor("orange")
        phi = angle * ( math.pi / 180.0 )
        xcenter = 0.0
        ycenter = 0.0
     
        # for loops iterate in this case from the first value until < 4, so
        for n in range (0,t):
            r = cspread * math.sqrt(n)
            theta = n * phi
     
            x = r * math.cos(theta) + xcenter
            y = r * math.sin(theta) + ycenter
     
            # move the turtle to that position and draw
            turtle.up()
            turtle.setpos(x,y)
            turtle.down()
            # orient the turtle correctly
            turtle.setheading(n * angle)
            if n > petalstart-1:
                turtle.color("yellow")
                drawPetal(turtle,x,y)
            else: turtle.stamp()
       
     
    def drawPetal(turtle, x, y ):
        turtle.penup()
        turtle.goto(x,y)
        turtle.pendown()
        turtle.color('black')
        turtle.fillcolor('yellow')
        turtle.begin_fill()
        turtle.right(20)
        turtle.forward(70)
        turtle.left(40)
        turtle.forward(70)
        turtle.left(140)
        turtle.forward(70)
        turtle.left(40)
        turtle.forward(70)
        turtle.penup()
        turtle.end_fill() # this is needed to complete the last petal
     
     
    wn = turtle.Screen()
    wn.bgcolor("lightgreen")    # set the window background color
    wn.title("Hello")
     
    tina = turtle.Turtle()
    tina.shape("turtle")
    tina.speed(0) # make the turtle go as fast as possible
    drawPhyllotacticPattern(tina, 200, 160, 137.508 )
    tina.penup()
    tina.forward(1000)
    wn.exitonclick() # lets you x out of the window when outside of idle
    
    

    Ví dụ 4:

    import turtle
     
     
    wn = turtle.Screen()
    wn.bgcolor("lightgreen")
    wn.title("Tess's Spiral")
     
    tess = turtle.Turtle()
    tess.shape("turtle")
    tess.color("blue")
     
    tess.penup()                    # this is new
    size = 20
    for i in range(100):
        tess.stamp()                # leave an impression on the canvas
        size = size + 1            # increase the size on every iteration
        tess.forward(size)          # move tess along
        tess.right(24)              # and turn her
     
    wn.exitonclick()
    

    Chúc các bạn thành công


Hãy đăng nhập để trả lời
 

Có vẻ như bạn đã mất kết nối tới Cộng đồng Python Việt Nam, vui lòng đợi một lúc để chúng tôi thử kết nối lại.