0
点赞
收藏
分享

微信扫一扫

Python —— turtle画图

左小米z 2022-04-05 阅读 144

目录

一、画圆圈

1、绘制一个圆

2、绘制多个圆(通过改变海龟的方向)

3、绘制更复杂的圆形组合

4、奥运五环

二、美国盾牌

三、气球飘飘

四、繁星满天


一、画圆圈

调快速度:t.speed(0)

颜色:t.color('red')

1、绘制一个圆

#召唤海龟先生
import turtle as t
#画一个圆
t.circle(100)

2、绘制多个圆(通过改变海龟的方向)

t.left(90)   #向左转90度
t.right(90)  #向右转90度
import turtle as t
t.circle(100)
t.left(90)
t.circle(100)
t.left(90)
t.circle(100)
t.left(90)
t.circle(100)

3、绘制更复杂的圆形组合

(利用循环语句)

import turtle as t
t.speed(0)
for i in range(50):
	t.right(95)
	t.circle(100)

效果展示:

4、奥运五环

画笔粗细   t.pensize(9)
移动海龟位置,海龟初始位置(0,0)
t.penup()#抬笔
t.goto(-100,0)#海龟移动
t.pendown()#落笔
t.circle(100)

#奥运五环
import turtle as t
t.speed(0)
t.pensize(9)
t.color('black')
t.circle(75)
t.penup()
t.goto(-160,0)
t.pendown()
t.color('blue')
t.circle(75)
t.penup()
t.goto(160,0)
t.pendown()
t.color('red')
t.circle(75)
t.penup()
t.goto(-80,-110)
t.pendown()
t.color('yellow')
t.circle(75)
t.penup()
t.goto(80,-110)
t.pendown()
t.color('green')
t.circle(75)

效果展示:

二、美国盾牌

先移动海龟确保画的圆居中

填色命令
t.begin_fill()
t.circle(200)
t.end_fill()

走直线命令
t.forword(80)

隐藏海龟
t.hideturtle()

import turtle as t
t.speed(0)

t.penup()
t.goto(0,-200)
t.pendown()
t.color('red')
t.begin_fill()
t.circle(200)
t.end_fill()

t.penup()
t.goto(0,-150)
t.pendown()
t.color('white')
t.begin_fill()
t.circle(150)
t.end_fill()

t.penup()
t.goto(0,-100)
t.pendown()
t.color('red')
t.begin_fill()
t.circle(100)
t.end_fill()

t.penup()
t.goto(0,-50)
t.pendown()
t.color('blue')
t.begin_fill()
t.circle(50)
t.end_fill()

t.penup()
t.goto(-40,12)
t.pendown()

t.color('white')
t.begin_fill()
for i in range(5):
	t.forward(80)
	t.right(144)
t.end_fill()

t.hideturtle()

效果展示:

三、气球飘飘

思路:

  • 绘制一个气球
  • 填充随机颜色
  • 出现在随机位置
  • 绘制多少个气球

用到随机函数,调用随机函数

import turtle as t
t.speed(0)#调整速度

for i in range(20):#绘制20个气球
	import random#调用随机函数

	x=random.randint(-220,220)#在这个区间内随机丢一个色子
	y=random.randint(-100,220)

	t.penup()
	t.goto(x,y)#出现随机位置
	t.pendown()

	t.colormode(255)#使用rgb颜色
	red=random.randint(0,255)
	green=random.randint(0,255)
	blue=random.randint(0,255)

	t.color(red,green,blue)#随机颜色
	t.begin_fill()
	t.circle(30)
	t.end_fill()
	t.right(90)
	t.forward(10)
	t.left(90)

 效果展示:

 

四、繁星满天

更换背景颜色

t.bgcolor('black')

import turtle as t
t.speed(0)
#更换背景颜色
t.bgcolor('black')
#画有层次的背景
#用特粗画笔画
t.pensize(100)
t.colormode(255)
for i in range(5):
	t.color(25+25*i,25+25*i,25+25*i)
	t.penup()
	t.goto(-350,132-88*i)
	t.pendown()
	t.forward(700)

#画小星星
for j in range(20):
	import random
	x=random.randint(-250,250)
	y=random.randint(0,250)
	z=random.randint(-180,180)

	q=random.randint(5,20)

	t.color('yellow')
	t.begin_fill()
	t.penup()
	t.goto(x,y)
	t.pendown()
	t.pensize(0.2*q)

	t.left(z)
	t.forward(q)
	t.right(120)
	t.forward(q)
	t.left(30)
	t.forward(q)
	t.right(120)
	t.forward(q)
	t.left(30)
	t.forward(q)
	t.right(120)
	t.forward(q)
	t.left(30)
	t.forward(q)
	t.right(120)
	t.forward(q)	

	t.end_fill()

效果展示:

举报

相关推荐

0 条评论