python圣诞树编写实例详解


Posted in Python onFebruary 13, 2020

python圣诞树代码

1、简单的绘制圣诞树

新建tree1.py或者直接输入下面代码运行

#声明树的高度

height = 5

#树的雪花数,初始为1

stars = 1

#以数的高度作为循环次数

 

for i in range(height):

 print((' ' * (height - i)) + ('*' * stars))

 stars += 2

#输出树干

print((' ' * height) + '|')

python圣诞树编写实例详解

2、使用turtle绘制简单圣诞树

新建tree2py,输入以下代码

#导入turtle库

import turtle

#设置屏幕大小

screen = turtle.Screen()

screen.setup(800,600)

#获取画笔并设置一些属性:圆形、红色、快

circle = turtle.Turtle()

circle.shape('circle')

circle.color('red')

circle.speed('fastest')

#抬起画笔

circle.up()

#重新获取画笔

square = turtle.Turtle()

#重新设置画笔属性:四方形、绿色、快

square.shape('square')

square.color('green')

square.speed('fastest')

#重新抬起画笔

square.up()

#跳到指定坐标位置

circle.goto(0,280)

#复制当前图形

circle.stamp()

k = 0

for i in range(1, 17):

 y = 30*i

 for j in range(i-k):

  x = 30*j

  square.goto(x,-y+280)

  square.stamp()

  square.goto(-x,-y+280)

  square.stamp()

 if i % 4 == 0:

  x = 30*(j+1)

  circle.color('red')

  circle.goto(-x,-y+280)

  circle.stamp()

  circle.goto(x,-y+280)

  circle.stamp()

  k += 2

 if i % 4 == 3:

  x = 30*(j+1)

  circle.color('yellow')

  circle.goto(-x,-y+280)

  circle.stamp()

  circle.goto(x,-y+280)

  circle.stamp()

square.color('brown')

for i in range(17,20):

 y = 30*i

 for j in range(3):

  x = 30*j

  square.goto(x,-y+280)

  square.stamp()

  square.goto(-x,-y+280)

  square.stamp()

turtle.exitonclick()

运行:

python圣诞树编写实例详解

3、使用Turtle绘制复杂圣诞树

新建tree3.py,输入以下代码

#导入所依赖的库

from turtle import *

import random

import time

 

n = 80.0

#设置速度快

speed("fastest")

#背景颜色 海贝壳色,偏粉色

screensize(bg='seashell')

left(90)

forward(3*n)

color("orange", "yellow")

begin_fill()

left(126)

 

for i in range(5):

 forward(n/5)

 right(144)

 forward(n/5)

 left(72)

end_fill()

right(126)

 

color("dark green")

backward(n*4.8)

def tree(d, s):

 if d <= 0: return

 forward(s)

 tree(d-1, s*.8)

 right(120)

 tree(d-3, s*.5)

 right(120)

 tree(d-3, s*.5)

 right(120)

 backward(s)

tree(15, n)

backward(n/2)

 

for i in range(200):

 a = 200 - 400 * random.random()

 b = 10 - 20 * random.random()

 up()

 forward(b)

 left(90)

 forward(a)

 down()

 if random.randint(0, 1) == 0:

   color('tomato')

 else:

  color('wheat')

 circle(2)

 up()

 backward(a)

 right(90)

 backward(b)

time.sleep(60)

运行:

python圣诞树编写实例详解

以上就是python圣诞树代码的详细内容,感谢大家的学习和对三水点靠木的支持。

Python 相关文章推荐
python搭建简易服务器分析与实现
Dec 15 Python
python定时器使用示例分享
Feb 16 Python
python更新列表的方法
Jul 28 Python
用Python写一个无界面的2048小游戏
May 24 Python
windows下安装python的C扩展编译环境(解决Unable to find vcvarsall.bat)
Feb 21 Python
用python与文件进行交互的方法
Mar 01 Python
Python打开文件,将list、numpy数组内容写入txt文件中的方法
Oct 26 Python
python 使用pandas计算累积求和的方法
Feb 08 Python
python如何实现不用装饰器实现登陆器小程序
Dec 14 Python
使用jupyter notebook直接打开.md格式的文件
Apr 10 Python
python 比较字典value的最大值的几种方法
Apr 17 Python
如何在python中实现线性回归
Aug 10 Python
python如何实现复制目录到指定目录
Feb 13 #Python
Python制作简易版小工具之计算天数的实现思路
Feb 13 #Python
解决python-docx打包之后找不到default.docx的问题
Feb 13 #Python
Python更新所有已安装包的操作
Feb 13 #Python
python离线安装外部依赖包的实现
Feb 13 #Python
python获取依赖包和安装依赖包教程
Feb 13 #Python
python连接mongodb集群方法详解
Feb 13 #Python
You might like
Phpbean路由转发的php代码
2008/01/10 PHP
php获取地址栏信息的代码
2008/10/08 PHP
PHP中break及continue两个流程控制指令区别分析
2011/04/18 PHP
ECMall支持SSL连接邮件服务器的配置方法详解
2014/05/19 PHP
thinkphp下MySQL数据库读写分离代码剖析
2017/04/18 PHP
Extjs学习笔记之三 extjs form更多的表单项
2010/01/07 Javascript
jQuery入门第一课 jQuery选择符
2010/03/14 Javascript
javascript中的new使用
2010/03/20 Javascript
JavaScript实现判断图片是否加载完成的3种方法整理
2015/03/13 Javascript
js console.log打印对像与数组用法详解
2016/01/21 Javascript
教你如何终止JQUERY的$.AJAX请求
2016/02/23 Javascript
很棒的js Tab选项卡切换效果
2016/08/30 Javascript
浅谈函数调用的不同方式,以及this的指向
2017/09/17 Javascript
原生js实现省市区三级联动代码分享
2018/02/12 Javascript
JS+canvas画布实现炫酷的旋转星空效果示例
2019/02/13 Javascript
Vue实现固定定位图标滑动隐藏效果
2019/05/30 Javascript
python文件操作相关知识点总结整理
2016/02/22 Python
python自动登录12306并自动点击验证码完成登录的实现源代码
2018/04/25 Python
使用 Python 实现文件递归遍历的三种方式
2018/07/18 Python
python flask解析json数据不完整的解决方法
2019/05/26 Python
python-Web-flask-视图内容和模板知识点西宁街
2019/08/23 Python
python之array赋值技巧分享
2019/11/28 Python
CSS3制作轮播图的一种方法
2019/11/11 HTML / CSS
ProBikeKit英国:在线公路自行车之家
2017/02/10 全球购物
菲律宾酒店预订网站:Hotels.com菲律宾
2017/07/12 全球购物
Rowdy Gentleman服装和配饰:美好时光
2019/09/24 全球购物
爱情检讨书大全
2014/01/21 职场文书
法定代表人授权委托书
2014/04/04 职场文书
师德演讲稿范文
2014/05/06 职场文书
会计专业毕业生求职信
2014/07/04 职场文书
平安工地汇报材料
2014/08/19 职场文书
迁户口计划生育证明
2014/10/19 职场文书
销售经理岗位职责范本
2015/04/02 职场文书
2015年化妆品销售工作总结
2015/05/11 职场文书
晚会开场白和结束语
2015/05/29 职场文书
Python使用openpyxl批量处理数据
2021/06/23 Python