python画七彩圆圈_python内置库turtle绘制有趣图形
一:绘制六边形
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
sides = 6
colors = ["red", "yellow", "green", "blue", "orange", "purple"]
for x in range(360):
t.pencolor(colors[x % sides])
t.forward(x * 3 / sides + x)
t.left(360 / sides + 1)
t.width(x * sides / 200)
效果:
二:自定义边框
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
sides = eval(input("输入要绘制的边的数目(2-6)!"))
colors = ["red", "yellow", "green", "blue", "orange", "purple"]
for x in range(100):
t.pencolor(colors[x % sides])
t.forward(x * 3 / sides + x)
t.left(360 / sides + 1)
t.width(x * sides / 200)
效果:
三:带有文字的图形绘制
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
my_name = turtle.textinput("输入你的姓名", "你的名字?")
colors = ["red", "yellow", "purple", "blue"]
for x in range(100):
t.pencolor(colors[x % 4])
t.penup()
t.forward(x * 4)
t.pendown()
t.write(my_name, font=("Arial", int((x + 4) / 4), "bold"))
t.left(92)
效果:
四:绘制雪花
from turtle import *
from random import *
def ground():
hideturtle()
speed(100)
for i in range(400):
pensize(randint(5, 10))
x = randint(-400, 350)
y = randint(-280, -1)
r = -y / 280
g = -y / 280
b = -y / 280
pencolor(r, g, b)
penup()
goto(x, y)
pendown()
forward(randint(40, 100))
def snow():
hideturtle()
speed(100)
pensize(2)
for i in range(100):
r = random()
g = random()
b = random()
pencolor(r, g, b)
penup()
setx(randint(-350, 350))
sety(randint(1, 270))
pendown()
dens = randint(8, 12)
snowsize = randint(10, 14)
for j in range(dens):
forward(snowsize)
backward(snowsize)
right(360 / dens)
def main():
setup(800, 600, 0, 0)
tracer(False)
bgcolor("black")
snow()
ground()
tracer(True)
mainloop()
main()
效果:
五:绘制科赫雪花
import turtle
def coch(size, n):
if n == 0:
turtle.fd(size)
else:
for angle in [0, 60, -120, 60]:
turtle.left(angle)
coch(size / 3, n - 1)
def main():
turtle.setup(600, 600)
turtle.penup()
turtle.bgcolor("white")
turtle.pensize(2)
turtle.goto(-200, 100)
turtle.pendown()
level = 3
coch(400, level)
turtle.right(120)
coch(400, level)
turtle.right(120)
coch(400, level)
turtle.hideturtle()
main()
效果:
六:绘制七彩线条
import turtle as t
t.setup(1000, 1000)
t.pen(shown=False, pendown=False, pensize=10, speed=0)
colorlist = [(255, 0, 0), (255, 165, 0), (255, 255, 0), (0, 255, 0), (0, 255, 255), (0, 0, 255), (139, 0, 255)]
colorlist.reverse()
t.fd(-250)
t.seth(-40)
t.colormode(255)
t.pendown()
for color in colorlist[:-1]:
t.pencolor(color)
t.circle(30, 80)
t.circle(-30, 80)
t.pencolor(colorlist[-1])
t.circle(30, 80 / 2)
t.fd(40)
t.circle(25, 180)
t.fd(40 * 2 / 3)
t.done()
效果:
七:绘制太极图
import turtle
# 绘制太极图函数
def draw_TJT(R):
turtle.screensize(800, 600, "blue") # 画布长、宽、背景色 长宽单位为像素
turtle.pensize(1) # 画笔宽度
turtle.pencolor('black') # 画笔颜色
turtle.speed(10) # 画笔移动速度
TJT_color = {1: 'white', -1: 'black'} # 太极图填充色 1 白色 -1 黑色
color_list = [1, -1]
"""
先画半边,再画另一边
"""
for c in color_list:
turtle.fillcolor(TJT_color.get(c)) # 获取该半边的填充色
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 * c) # 移动到该半边的鱼眼的圆上 R/3*c 表示移动到哪边
turtle.pendown() # 放下画笔,移动留痕
turtle.fillcolor(TJT_color.get(-c)) # 获取鱼眼填充色, 与该半边相反
turtle.begin_fill()
turtle.circle(-R / 6, 360)
turtle.end_fill()
# 回到原点,为下一循环的开始做准备
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
# 绘制文本
turtle.penup()
turtle.goto(0, -R - 50)
turtle.pendown()
turtle.write("太极图 made by kjshen", font=('Arial', 12, 'normal'))
if __name__ == '__main__':
R = 100 # 太极图半径
draw_TJT(R)
input('Press Enter to exit...') # 防止程序运行完成后就自动关闭窗口
效果:
八:绘制五角星
import turtle
turtle.setup(2000, 2000, 0, 0)
turtle.pensize(20)
turtle.pencolor("black")
turtle.seth(0)
turtle.fd(400)
turtle.seth(-144)
turtle.fd(400)
turtle.seth(-144 - 144)
turtle.fd(400)
turtle.seth(-144 - 144 - 144)
turtle.fd(400)
turtle.seth(-144 - 144 - 144 - 144)
turtle.fd(400)
input("")
效果:
九:绘制奥运五环
import turtle
p = turtle
p.pensize(3)
p.color("blue")
p.circle(30, 360)
p.pu()
p.goto(60, 0)
p.pd()
p.color("black")
p.circle(30, 360)
p.pu()
p.goto(120, 0)
p.pd()
p.color("red")
p.circle(30, 360)
p.pu()
p.goto(90, -30)
p.pd()
p.color("green")
p.circle(30, 360)
p.pu()
p.goto(30, -30)
p.pd()
p.color("yellow")
p.circle(30, 360)
p.done()
效果:
python画七彩圆圈_python内置库turtle绘制有趣图形相关推荐
- python内置库turtle绘制有趣图形
一:绘制六边形 import turtlet = turtle.Pen() turtle.bgcolor("black") sides = 6 colors = ["re ...
- [转载] python自带sqlite库_Python内置库SQlite3使用指南
参考链接: 使用Python和SQLite的SQL 2 如果你是软件开发人员,相信你一定知道或者曾经使用过一个非常轻量级的数据库--SQLite.它具有作为关系数据库所需的几乎所有功能,但是这个数据库 ...
- python内置库有哪些_python 内置库
广告关闭 回望2020,你在技术之路上,有什么收获和成长么?对于未来,你有什么期待么?云+社区年度征文,各种定制好礼等你! version: 0. 2.0, configurations: }, de ...
- [转载] python支持complex吗_Python 内置函数complex详解
参考链接: Python complex() 英文文档: class complex([real[, imag]]) Return a complex number with the value re ...
- python中dir用法_Python内置函数dir详解
1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: >>> help(dir) Help on built-in function dir in mo ...
- python十进制转八进制_Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)...
使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x) Convert an integer num ...
- 如何用python画七彩蟒蛇_Python实现七彩蟒蛇绘制实例代码
本文主要研究的是Python编程turtle的实例,绘制一个七彩蟒蛇..具体如下. 第2周的课后练习里,有一道题目,要求修改"蟒蛇绘制"程序,对Python 蟒蛇的每个部分采用不同 ...
- python自带笔记本电脑_Python内置常用模块
time和datatime Range os sys hashlib XML json & picle 1.time和datetime time和datetime都是python处理时间和日期 ...
- python的globals函数_Python内置函数之globals()与locals()
Python内置函数globals() 函数与locals()函数简介. 在Python中,函数拥有自己的作用域,或者称为命名空间.在函数内访问某个变量会优先在函数自己的命名空间中进行访问. 而要想知 ...
最新文章
- Wamp与IIS同时在Windows7下运行解决方法
- 获取ServerManager
- Homebrew--MacOSX下的套件管理器
- [LeetCode]119.Pascal#39;s Triangle II
- 细数近年来机器学习研究的几大怪现状
- bs架构 erp 进销存_从应用架构看生鲜电商信息化建设
- 字符串(string)的常用语法和常用函数
- SELinux系列(三)——SELinux的三种工作模式
- [转]Oracle - 数据库的实例、表空间、用户、表之间关系
- 各位有没有遇到过这样的问题?
- 计算机录屏幕和声音的软件是什么,哪个录屏软件可以录内部声音?分享开启与调节的方法...
- 菜鸟的LEETCODE的MYSQL做题总结2
- 密码协议(二)仲裁协议
- Linux系统下强制关闭程序
- Headless Services无头服务
- 在window下间接使用meteorite的包管理功能
- 情人节python代码_情人节 ,能否表白成功就看这段Python代码了
- Nginx相应原理及简单使用和配置
- Win系统 - 压缩包part1与part2之间的关系
- JavaScript的输入输出语句