zoukankan      html  css  js  c++  java
  • python少儿编程-turtle 基本绘图

    1.画一个正方形

    先画一个正方形,介绍forward命令和right命令

    import turtle
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    
     
    image.png

    练习:画一个三角形

    import turtle
    turtle.forward(100)
    turtle.right(120)
    turtle.forward(100)
    turtle.right(120)
    turtle.forward(100)
    

    2.用循环的方式来画刚刚的正方形

    学习for的用法

    import turtle
    for x in range(0,3):   
        turtle.forward(100)
        turtle.right(90)   
    turtle.forward(100)
    

    练习用for循环画三角形

    import turtle
    for x in range(0,3):   
       turtle.forward(100)
       turtle.right(120)   
    

    3.给三角形上色

    import turtle 
    turtle.begin_fill()
    turtle.fillcolor('red')
    for x in range(0,3):   
        turtle.forward(100)
        turtle.right(120) 
    turtle.end_fill()
    
     
    image.png

    4.换个位置开始画

    知识点:penup()、pendown()、goto(x,y)

    import turtle as t 
    t.penup()#抬起画笔
    t.goto(-100,-100)#移动到指定位置
    t.pendown()#放下画笔
    

    5.第一个项目:画一个小房子

    import turtle as t
    t.penup()
    t.goto(-130,-100)
    t.pendown()
    # 房子的主体
    t.forward(300)
    t.left(90)
    t.forward(150)
    t.left(90)
    t.forward(300)
    t.left(90)
    t.forward(150)
    #门
    t.fillcolor('blue')
    t.begin_fill()
    t.left(90)
    t.forward(200)
    t.left(90)
    t.forward(100)
    t.right(90)
    t.forward(50)
    t.right(90)
    t.forward(100)
    t.end_fill()
    #屋顶
    t.penup()
    t.goto(170,50)
    t.pendown()
    t.right(180)
    t.left(30)  
    t.forward(100)
    t.left(60)
    t.forward(200)
    t.left(60) 
    t.forward(100)
    
    #窗户
    t.fillcolor('red')
    t.begin_fill()
    t.left(120)
    t.forward(100)
    for i in range(0,3):
      t.right(90)
      t.forward(50)
    t.end_fill()
    
     
    image.png

    6.画圆圈

    主要介绍circle命令的用法,包括steps

    import turtle as t
    t.circle(50)
    #turtle.circle(radius, extent=None, steps=None)
    

    7.任务,画太极图

    import turtle 
    R=100
    
    # 开始画出半边的轮廓
    turtle.fillcolor('white')
    turtle.begin_fill()
    turtle.circle(R / 2, 180)
    turtle.circle(R, 180)
    turtle.circle(R / 2, -180)
    turtle.end_fill()  # 结束填充 上色完成
    
    # 绘制该半边的鱼眼
    turtle.penup()  # 提起画笔,移动不留痕
    turtle.goto(0, R/3)  # 移动到该半边的鱼眼的圆上 R/3*c 表示移动到哪边
    turtle.pendown()  # 放下画笔,移动留痕
     # 获取鱼眼填充色, 与该半边相反
    turtle.fillcolor('black')
    turtle.begin_fill()
    turtle.circle(-R / 6, 360)
    turtle.end_fill()
    
    # 回到原点,为下半圆的开始做准备
    turtle.penup()
    turtle.goto(0, 0)
    turtle.pendown()
    
    # 开始画出右半边的轮廓
    turtle.fillcolor('black')
    turtle.begin_fill()
    turtle.circle(R/2, 180)
    turtle.circle(R, 180)
    turtle.circle(R/2, -180)
    turtle.end_fill() 
    
    # 绘制下半边的鱼眼
    turtle.penup()  # 提起画笔,移动不留痕
    turtle.goto(0, -R/3)  # 移动到该半边的鱼眼的圆上 R/3*c 表示移动到哪边
    turtle.pendown()  # 放下画笔,移动留痕
     # 获取鱼眼填充色, 与该半边相反
    turtle.fillcolor('white')
    turtle.begin_fill()
    turtle.circle(-R / 6, 360)
    turtle.end_fill()
    
     
    image.png

    8.复习for循环:画五角星·

    import turtle as t
    t.shape('turtle')
    t.fillcolor('red')
    t.begin_fill()
    for x in range(0,5):
        t.forward(100)
        t.left(144)
    t.end_fill()
    

    9.项目:用曲线和直线作画

    import turtle as t
    t.shape('turtle')
    t.penup()
    t.goto(-100,0)
    t.pendown()
    #画车身
    t.fillcolor('blue')
    t.begin_fill()
    t.forward(250)
    t.left(90)
    t.forward(50)
    t.left(90)
    t.forward(50)
    t.right(90)
    t.forward(50)
    t.left(90)
    t.forward(150)
    t.left(90)
    t.forward(50)
    t.right(90)
    t.forward(50)
    t.left(90)
    t.forward(50)
    t.end_fill()
    #左轮子
    t.fillcolor('yellow')
    t.begin_fill()
    t.penup()
    t.goto(-80,0)
    t.pendown()
    t.circle(30)
    t.end_fill()
    #右轮子
    t.fillcolor('yellow')
    t.begin_fill()
    t.penup()
    t.goto(130,0)
    t.pendown()
    t.circle(-30)
    t.end_fill()
    
     
    image.png
     
      
    转自:https://www.jianshu.com/p/0909e3efc820 
  • 相关阅读:
    *Basic Calculator
    今天周末20190616,分享句喜欢的话,常用架构图
    Python 面向对象 --- 类和对象
    Python 面向对象(OOP)基本概念
    Python 高级变量类型 --- 函数的进阶
    Python 高级变量类型 --- 变量进级
    Python 高级变量类型 --- 综合应用
    Python 高级变量类型 --- 公共方法
    Python 高级变量类型 --- 字典
    Python 高级变量类型 --- 元组
  • 原文地址:https://www.cnblogs.com/javalinux/p/14970098.html
Copyright © 2011-2022 走看看