廖老师Python海龟画图整理


声明:所有资料都是从廖老师那里搬运来的,本人就是整理下方便大家复习,感谢廖老师的辛苦付出

海龟画图

0.基础知识

#命令                        	简化	                   说明
turtle.setup(width,height)                     设置屏幕宽高
turtle.forward(distance)	turtle.fd()	  向当前画笔方向移动distance像素长度
turtle.backward(distance)	turtle.bk()向当前画笔相反方向移动distance像素长度
turtle.right(degree)	    turtle.rt()	        顺时针移动degree°
turtle.left(degree)	        turtle.lt()	        逆时针移动degree°
turtle.pendown()	        turtle.pd()
turtle.down()	                            移动时绘制图形,缺省时也为绘制
turtle.goto(x,y)		                      将画笔移动到坐标为x,y的位置
turtle.penup()	            turtle.pu()
turtle.up()	                      提起笔移动,不绘制图形,用于另起一个地方绘制
turtle.circle(radius,extent,steps)	画圆,radius:半径为正(),表示圆心在画笔的左边(右边)画圆,extent:弧度一圈360 steps:多边形边数
turtle.pensize()            设置画笔的宽度
turtle.pencolor()           设置画笔颜色
turtle.color(color1, color2)	设置边框颜色和绘图颜色
turtle.filling()	         返回当前是否在填充状态
turtle.begin_fill()        	 准备开始填充图形
turtle.end_fill()	           填充完成
turtle.colormode(255)          切换为RGB颜色模式  
turtle.color(red,green,blue)   使用RGB颜色模式设置颜色
turtle.home()	              设置当前画笔位置为原点,朝向东
turtle.dot(直径, *color)         color默认为画笔颜色

随机模块import random

random.random()	       #用于生成一个0到1的随机浮点数: 0 <= n < 1.0
random.randint(a,b)	#返回a,b之间的整数,范围[a,b],注意:传入参数必须是整数,a一定要比b小。
random.randrange([start=0], stop[, step=1])	#返回前闭后开区间[start,stop)内的整数,可以设置step。只能传入整数。
random.choice(sequence)	#从sequence(序列,列表、元组和字符串)中随机获取一个元素。

1.画一个边长100的正方形

import turtle as t
t.setup(400,400)
for i in range(4):
      t.fd(100)
      t.left(90)

t.done()

2.绘制如图所示10个边长为100的正方形

import turtle as t
t.setup(width=400,height=400)
for x in range(10):
      for i in range(4):
            t.fd(100)
            t.left(90)
      t.left(10)
t.done()

3.正方形:边长100,线条粗5,绿色

import turtle as t
t.pensize(5)
t.pencolor("blue")
for i in range(4):
    t.fd(100)
	t.lt(90)
t.done()

4.长方形:长200宽100,红色,线条粗3,绘制速度10

import turtle as t
t.pensize(3)
t.pencolor("red")
for i in range(2):
    t.fd(200)
    t.lt(90)
    t.fd(100)
    t.lt(90)
t.done()

5.绘制如图所求图形,每个正方形的边长为50,线条粗细为5,颜色为红色,各个正文形间隔距离为10,最左侧正方形右下角的坐标为(0,0)

import turtle as t
t.pensize(5)
t.pencolor('red')
for i in range(0,5):
      t.up()
      t.goto(60*i,0)
      t.down()
      for j in range(4):
            t.fd(50)
            t.lt(90)
t.done()

5+.提升一:五个正方形的线条颜色依次显示为红色、绿色、蓝色、紫色、黄色。(red、green、blue、purple、yellow)

import turtle as t
t.pensize(5)
lst=["red","green","blue","purple","yellow"]
for i in range(0,5):
      t.up()
      t.goto(60*i,0)
      t.down()
      t.pencolor(lst[i])
      for j in range(4):
            t.fd(50)
            t.lt(90)
t.done()

5++.提升二:每个正方形的线条颜色随机产生,显示任意颜色

import turtle as t
import random
t.pensize(5)
lst=["red","green","blue","purple","yellow"]
for i in range(0,5):
      t.up()
      t.goto(60*i,0)
      t.down()
      t.pencolor(random.choice(lst))
      for j in range(4):
            t.fd(50)
            t.lt(90)
t.done()

6.填充色的长方形:长200宽100,红色,线条粗3,填充蓝色,绘制速度10

import turtle as t
t.setup(400,400)
t.pu()
t.goto(-100,100)
t.pd()
t.pensize(3)
t.speed(10)
t.color('red','blue')
t.begin_fill()
for i in range(1, 5):
    if i%2==1:
        t.fd(200)
    else:
        t.fd(100)
    t.right(90)
t.end_fill()
t.hideturtle()
t.done()

7.绘制如下图形,长200宽100,线条蓝色,下方填充红色,上方填充绿色

import turtle as t
t.setup(400,400)
t.pu()
t.goto(-100,0)
t.pd()
t.pensize(3)
t.color('blue','red')
t.begin_fill()
for i in range(1, 5):
    if i%2==1:
        t.fd(200)
    else:
        t.fd(100)
    t.right(90)
t.end_fill()
t.color('blue','green')
t.begin_fill()
for i in range(1, 5):
    if i%2==1:
        t.fd(200)
    else:
        t.fd(100)
    t.left(90)
t.end_fill()
t.done()

8.绘制两个圆,圆的半径为100,窗体大小(600,600)

import turtle as t
t.setup(600,600)
t.lt(90)
t.circle(-100) #画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆
t.circle(100)
t.done()

9.将上述两个图形分别填充为红色和绿色两种颜色

import turtle as t
t.setup(600,600)
t.lt(90)
t.fillcolor("red")
t.begin_fill()
t.circle(-100)
t.end_fill()
t.fillcolor("green")
t.begin_fill()
t.circle(100)
t.end_fill()
t.done()

10.绘制一朵太阳花,红色花瓣由四个圆形图案组合而成,圆的半径为100,中间花心部分有一个半径为50的,黄色小圆构成

import turtle as t

t.setup(600,600)
t.speed(0)

for i in range(4):
    t.color('red','red')
    t.begin_fill()
    t.circle(100)
    t.end_fill()
    t.lt(90)

t.goto(0,-50)
t.color('yellow','yellow')
t.begin_fill()
t.circle(50)
t.end_fill()

t.hideturtle()
t.done()

11.绘制随机颜色圆环(不同心)

import turtle as t
import random

t.setup(600,600)
t.speed(0)
t.pensize(10)
t.colormode(255)

for i in range(30,120,10):
    red = random.randint(0,255)
    green = random.randint(0,255)
    blue = random.randint(0,255)
    t.color(red,green,blue)
    t.circle(i)

11+.11.绘制随机颜色圆环(同心)

import turtle as t
import random

t.setup(600,600)
t.speed(0)
t.pensize(10)
t.colormode(255)

# (0,0) (0,10) (0,20)
y = 0
for i in range(110,20,-10):
    red = random.randint(0,255)
    green = random.randint(0,255)
    blue = random.randint(0,255)
    t.color(red,green,blue)
    t.circle(i)
    y += 10
    t.goto(0,y)

t.done()

12.绘制十字架

import turtle as t

for j in range(4):
    for i in range(2):
        t.fd(50)
        t.rt(90)
    t.fd(50)
    t.lt(90)

t.done()

13.绘制奥运五环图。要求:圆的半径为100,颜色分别为:蓝、黑、红、黄、绿。每个圆环交叉点在圆弧中间位置。【’blue’,’black’,’red’,’yellow’,’green’】

import turtle as t

t.setup(1000,1000)
t.speed(0)
t.pensize(10)

def draw_circle(color,x,y):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.color(color)
    t.circle(100)

draw_circle('black',0,0)
draw_circle('red',200,0)
draw_circle('blue',-200,0)
draw_circle('green',100,-100)

t.done()

14.画发散线条

import turtle as t
import random

t.setup(400,400)
t.pensize(5)
t.colormode(255)

for i in range(72):
    red = random.randint(0,255)
    green = random.randint(0,255)
    blue = random.randint(0,255)
    t.color(red,green,blue)
    t.fd(random.randint(50,180))
    t.goto(0,0)
    t.lt(random.randint(0,360))

t.done()

15.画风车

import turtle as t

color_list = ['red','yellow','blue','green']
#               0      1        2      3

t.setup(600,600)

for i in range(4):
    t.color('black',color_list[i])
    t.begin_fill()
    t.fd(200)
    t.lt(90)
    t.circle(100,180)
    t.end_fill()
t.done()

16.画四色圆

import turtle as t

color_list = ['red','yellow','blue','green']
#               0      1        2      3

t.setup(600,600)

for i in range(4):
    t.color('black',color_list[i])
    t.begin_fill()
    t.fd(100)
    t.lt(90)
    t.circle(100,90)
    t.end_fill()
    t.goto(0,0)
    t.rt(90)

t.done()

17.画奔驰车标

import turtle as t

t.pensize(5)
t.color('red')

t.penup()
t.goto(0,-100)
t.pendown()
t.circle(100)

t.lt(90)
for i in range(3):
    t.penup()
    t.goto(0,0)
    t.pendown()
    t.fd(100)
    t.lt(120)

t.done()

18.画同心三角形

import turtle as t

for i in range(10,200,10):
    t.circle(i,360,6)
    t.penup()
    t.goto(0,-i)
    t.pendown()

t.done()

19.画圆锥

import turtle as t
import random

t.speed(0)
t.colormode(255)

for i in range(100,-3,-3):
    t.fillcolor(100+i,100-i,0)
    # t.fillcolor(random.randint(0,255),random.randint(0,255),random.randint(0,255))
    # 移动位置
    t.penup()
    t.goto(-i,0)
    t.pendown()
    # 画圆
    t.begin_fill()
    t.circle(i,360)
    t.end_fill()

t.done()

20.画扇子

import turtle as t
t.speed(0)
t.pensize(5)

t.fillcolor('yellow')
t.begin_fill()
t.fd(250)
t.lt(90)
t.circle(250,120)
t.home()
t.end_fill()

for i in range(11):
    t.color('red')
    t.goto(0,0)
    t.lt(10)
    t.fd(250)

t.done()

21.画太极

import turtle as t

t.pensize(5)
t.speed(0)
t.penup()
t.goto(0,-200)
t.pendown()

t.fillcolor('black')
t.begin_fill()
t.circle(200,180)
t.circle(100,180)
t.circle(-100,180)
t.end_fill()

t.circle(-200,180)

t.penup()
t.goto(0,-100)
t.pendown()
t.dot(100)

t.penup()
t.goto(0,100)
t.pendown()
t.dot(100,'white')

t.done()

22.画六芒星

import turtle as t

t.bgcolor('gray') # 设置背景颜色为灰色
t.pensize(5)      # 设置笔的宽度

# 将笔抬起来之后再移动到-100,100这个点再放下
t.penup()         
t.goto(-100,100)
t.pendown()

for i in range(8):
    t.fd(150)      # 往前移动150
    t.lt(225)      # 向左转225度

t.done()

23.画四个相连的五角星

13.画棋盘格

利用Python中自带Turtle库绘制如下黑白棋盘。要求:画布大小宽800高600,窗体显示500500,初始位置在(300,300),画出如下1010的黑白格子,每个格子大小为50*50

import turtle as t
t.screensize(800,600,'white') # 设置画布宽、高、背景颜色
t.speed(0) # 设置海龟的速度,0是最快

# 让海龟去x,y坐标点
def goto(x,y):
    t.penup()
    t.goto(x,y)
    t.pendown()

# 画一个正方形
def draw_zhengfang(color):
    t.color('black',color)
    t.begin_fill()
    for i in range(4):
        t.fd(50)
        t.lt(90)
    t.end_fill()

goto(250,-250)
count = 0
for i in range(-250,250,50):
    for j in range(250,-250,-50):
        goto(i,j)
        if count%2==1:
            draw_zhengfang('white')
        else:
            draw_zhengfang('black')
        count += 1
    count+=1

t.done()

版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 廖老师、彭韦浩(整理) !
  目录