zoukankan      html  css  js  c++  java
  • Hello, little turtles!

     import turtle
     wn = turtle.Screen()
     wn.bgcolor("lightgreen")      # Set the window background color
     wn.title("Hello, Tess!")      # Set the window title
    
     tess = turtle.Turtle()
     tess.color("blue")            # Tell tess to change her color
     tess.pensize(3)               # Tell tess to set her pen width
    
     tess.forward(50)
     tess.left(120)
     tess.forward(50)
    
     wn.mainloop()

    When we run this program, this new window pops up, and will remain on the screen until we close it.

    from:http://www.openbookproject.net/thinkcs/python/english3e/hello_little_turtles.html

    import turtle
     wn = turtle.Screen()         # Set up the window and its attributes
     wn.bgcolor("lightgreen")
     wn.title("Tess & Alex")
    
     tess = turtle.Turtle()       # Create tess and set some attributes
     tess.color("hotpink")
     tess.pensize(5)
    
     alex = turtle.Turtle()       # Create alex
    
     tess.forward(80)             # Make tess draw equilateral triangle
     tess.left(120)
     tess.forward(80)
     tess.left(120)
     tess.forward(80)
     tess.left(120)               # Complete the triangle
    
     tess.right(180)              # Turn tess around
     tess.forward(80)             # Move her away from the origin
    
     alex.forward(50)             # Make alex draw a square
     alex.left(90)
     alex.forward(50)
     alex.left(90)
     alex.forward(50)
     alex.left(90)
     alex.forward(50)
     alex.left(90)
    
     wn.mainloop()

    A turtle can “stamp” its footprint onto the canvas, and this will remain after the turtle has moved somewhere else. Stamping works, even when the pen is up.

    import turtle
    wn = turtle.Screen()
    wn.bgcolor("lightgreen")
    tess = turtle.Turtle()
    tess.shape("turtle")
    tess.color("blue")
    
    tess.penup()                # This is new
    size = 20
    for i in range(30):
       tess.stamp()             # Leave an impression on the canvas
       size = size + 3          # Increase the size on every iteration
       tess.forward(size)       # Move tess along
       tess.right(24)           #  ...  and turn her
    
    wn.mainloop()

    The turtle has a lot more power than we’ve seen so far. The full documentation can be found at http://docs.python.org/py3k/library/turtle.html or within PyScripter, use Help and search for the turtle module.

    Here are a couple of new tricks for our turtles:

    • We can get a turtle to display text on the canvas at the turtle’s current position. The method to do that is alex.write("Hello").
    • We can fill a shape (circle, semicircle, triangle, etc.) with a color. It is a two-step process. First we call the method alex.begin_fill(), then we draw the shape, then we call alex.end_fill().
    • We’ve previously set the color of our turtle — we can now also set its fill color, which need not be the same as the turtle and the pen color. We usealex.color("blue","red") to set the turtle to draw in blue, and fill in red.
    def draw_bar(t, height):
        """ Get turtle t to draw one bar, of height. """
        t.begin_fill()           # Added this line
        t.left(90)
        t.forward(height)
        t.write("  "+ str(height))
        t.right(90)
        t.forward(40)
        t.right(90)
        t.forward(height)
        t.left(90)
        t.end_fill()             # Added this line
        t.forward(10)
    
    wn = turtle.Screen()         # Set up the window and its attributes
    wn.bgcolor("lightgreen")
    
    tess = turtle.Turtle()       # Create tess and set some attributes
    tess.color("blue", "red")
    tess.pensize(3)
    
    xs = [48,117,200,240,160,260,220]
    
    for a in xs:
        draw_bar(tess, a)
    
    wn.mainloop()
  • 相关阅读:
    java 深入理解jvm内存模型 jvm学习笔记
    java实体 和 xml相互转换
    clickhouse 离线/在线 安装和java通过jdbc链接
    clickhouse安装 Requires: libstdc++.so.6(GLIBCXX_3.4.19)(64bit)
    maven pom.xml详解
    elasticsearch 简单demo RestHighLevelClient LowLeveClient
    从一段时间段中获取所有日期
    hadoop 输入路径用正则表达式被默认处理为多个参数的问题
    对象变化影响map中的数据
    小技巧积累
  • 原文地址:https://www.cnblogs.com/michaely/p/3343658.html
Copyright © 2011-2022 走看看