python语言创意绘画-Python turtle库能画出什么好玩的东西?
【教你用Python画蛋糕】
我曾经给别人送过的生日礼物是一段Python代码。。。
用了Python的turtle package,仿照这个网站的代码:Layer Cake using Python Turtle?www.101computing.net
我自己改了一个:trinket: run code anywhere?trinket.io
可能是外网,国内小伙伴戳不进去,我这里附上代码:
第一个文件:shapes.py
# This is a custom module we've made.
# Modules are files full of code that you can import into your programs.
# This one teaches our turtle to draw various shapes.
import turtle
import math
def draw_circle(turtle, color, x, y, radius):
turtle.penup()
turtle.color(color)
turtle.fillcolor(color)
turtle.goto(x,y)
turtle.pendown()
turtle.begin_fill()
turtle.circle(radius)
turtle.end_fill()
def draw_rectangle(turtle, color, x, y, width, height):
turtle.hideturtle()
turtle.penup()
turtle.color(color)
turtle.fillcolor(color)
turtle.goto(x,y)
turtle.pendown()
turtle.begin_fill()
for i in range (2):
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.end_fill()
turtle.setheading(0)
def draw_star(turtle, color, x, y, size):
turtle.penup()
turtle.color(color)
turtle.fillcolor(color)
turtle.goto(x,y)
turtle.pendown()
turtle.begin_fill()
turtle.right(144)
for i in range(5):
turtle.forward(size)
turtle.right(144)
turtle.forward(size)
turtle.end_fill()
turtle.setheading(0)
def addIcing(turtle,color,x,y):
turtle.penup()
turtle.color(color)
turtle.fillcolor(color)
turtle.goto(-x-2,y+10)
turtle.pendown()
turtle.begin_fill()
for z in range(-x-3,x+3):
turtle.goto(z,y-10-10*math.cos((z/100)*2*math.pi))
turtle.goto(x+3,y+10)
turtle.goto(-x-3,y+10)
turtle.end_fill()
第二个文件:main.py
# Layer Cake Challenge - www.101computing.net/layer-cake/
from turtle import *
from shapes import *
myPen = Turtle()
myPen.shape("turtle")
myPen.speed(10)
myPen.hideturtle()
window = turtle.Screen()
window.bgcolor("#69D9FF")
y = -140
#Inititalise the dictionary
ingredients = {}
#Add items to the dictionary
ingredients["strawberry"]="pink"
ingredients["milk chocolate"]="#BF671F"
ingredients["matcha"]="#93c572"
ingredients["icing sugar"]="#FFFFFF"
### Now let's preview the layer cake
#let's draw the plate
draw_rectangle(turtle, "white", -150, y-10, +300, 10)
#Iterate through each layer of the list
draw_rectangle(myPen, ingredients["milk chocolate"], -120, y, 240, 30)
y+=30
draw_rectangle(myPen, ingredients["strawberry"], -120, y, 240, 35)
y+=35
addIcing(myPen, ingredients["icing sugar"],120,y)
y+=10
draw_rectangle(myPen, ingredients["milk chocolate"], -100, y, 200, 20)
y+=20
draw_rectangle(myPen, ingredients["strawberry"], -100, y, 200, 40)
y+=40
addIcing(myPen, ingredients["icing sugar"],100,y)
y+=10
draw_rectangle(myPen, ingredients["milk chocolate"], -70, y, 140, 24)
y+=24
draw_rectangle(myPen, ingredients["strawberry"], -70, y, 140, 36)
y+=36
addIcing(myPen, ingredients["icing sugar"],70,y)
y+=10
draw_rectangle(myPen, ingredients["matcha"], -4, y, 8, 60)
y+=65
draw_star(myPen, "white", 2, y, 10)
动画效果大概是这样的: />
嗯,Python Turtle还是很其强大的!
python语言创意绘画-Python turtle库能画出什么好玩的东西?相关推荐
- python库能画什么东西_Python turtle库能画出什么好玩的东西?
Python turtle来源于帕佩特的logo语言,就是小乌龟,目的是教小孩子熟悉编程.其实当你把turtle当做是画笔的的话,能够画出什么,只取决于你是不是一个号的画家了.你可以把turtle当做 ...
- python语言创意绘画-Python神笔马良案例集
<Python神笔马良绘画案例集合>是李兴球编写的一些主要由Python海龟画图模块制作的案例集.除了少数几个不是绘画或动画作品外,绝大多数都是用turtle模块制作的绘画或利用动画原理甚 ...
- python语言创意绘画-Python街机模块的draw系列绘画例子集合
""" draw系列绘画例子集合 """ import arcade import os # 设置工作目录,以python -m启动程序时才 ...
- python语言创意绘画-用python画画
广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. 那么今天就一起来看看怎样在python中画一棵美丽的樱花树-? 说到用p ...
- python语言创意绘画是什么意思_创意绘画是什么?
展开全部 创意美术是根据不2113同年龄段幼儿的生长发育特5261点,以绘4102画性游戏为载体,设计1653通过视觉.听觉.触觉和嗅觉等感觉器官,用多样化的材料,使幼儿从感知到运用点.线.面进行构图 ...
- python语言创意绘画-齐齐哈尔富裕高校邦数据科学通识课【Python基础语法】答案...
齐齐哈尔富裕高校邦数据科学通识课[Python基础语法]答案it8p 齐齐哈尔富裕高校邦数据科学通识课[Python基础语法]答案 关注公众号{帅搜}即可查询答案 支持:大学网课,智慧树,知到,超星, ...
- python语言创意绘画-校招资讯|拼多多、百度、中建六局等名企校园招聘
原标题:校招资讯|拼多多.百度.中建六局等名企校园招聘 1.[上海]拼多多 [公司简介] 拼多多是国内目前主流的手机购物APP.用户通过发起和朋友,家人,邻居等的拼团,以更低的价格,拼团购买商品.旨在 ...
- python语言创意绘画-银川童程童美Python少儿编程班价格
银川童程童美是达内教育集团旗下青少儿教育品牌,形成了以乐高创意启蒙课程.少儿编程.智能机器人编程.信息学奥赛编程等课程体系为核心,采用了线上线下相结合的授课方式,集国内外大型科技赛事.少儿资格认证考试 ...
- python语言创意绘画是什么-Python街机模块的draw系列绘画例子集合
""" draw系列绘画例子集合 """ import arcade import os # 设置工作目录,以python -m启动程序时才 ...
最新文章
- 69:shell脚本介绍 | shell脚本结构 | 执行data命令用法 | shell脚本中变量
- 2018python培训-2018python深度学习核心技术培训班
- aws java mysql_Lambda本地连接到Aurora MySql - 部署到AWS时超时
- VTK:PolyData之WarpScalar
- 关于计算机原理的知识
- 浅说动态生成Class实现MVC
- 世上的人大都只会“飞鸽传书下载”,没人开发
- Centos6.5 安装Vim7.4
- jsp之servlet模板问题
- python打印json_Python漂亮打印JSON
- 问题十七:怎么用ray tracing画多个球?
- [JNI]开发之旅(4)项目架构介绍
- 电力巡检系统无人机模块
- 14443-A 与14443-B区别
- 物联网模块选择注意事项
- 百度UEditor 图片粘贴上传,实现图文粘贴,图片自动上传
- m-audio keystation 88/61 midi键盘的走带控制器在cubase里面怎么用
- Kali 下对安卓手机的攻击
- 随机变量的相关性与独立性
- 淘宝用户购物行为分析
热门文章
- 亿美软通出席硬核桃5G开发者社区周年庆,喜获“金核桃奖”
- PTA L1-002 打印沙漏(详解)
- Java利用Jacob实现excel,ppt,word转pdf 及jacob遇到的坑
- php调试之xdebug配置详解
- python 位运算实现加减乘除四则运算
- 2014软专高级程序语言T2(用向量叉乘判断点与三角形的位置关系)
- linux vim 命令无效,【工匠大道】一些Vim(Linux)不常见但很逼格的命令(不断更新中)...
- python xlwt生成excel,列数大于256,分表
- 全国计算机二级考试内容都有什么用,2020年下半年全国计算机二级考试内容都有什么?...
- 广义表的长度和深度怎么算_C语言:数据结构-广义表的定义和图形表示