下面汇总了一些用Python画的圣诞树,文末有打包文件供大家下载
更多精彩欢迎前往我们的社区交流
- Python基础学习 https://www.cvv-goods.com/forum/4439
- 10天零基础搞定Python
版本一
截图展示
![图片[1]-马上圣诞节了,教你用Python画个圣诞树-FancyPig's blog](https://www.cvv-goods.com/wp-content/uploads/2023/03/20211218101504147.webp)
相关代码
import turtle
screen = turtle.Screen()
screen.setup(375, 700)
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, 13):
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 += 3
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(13, 17):
y = 30 * i
for j in range(2):
x = 30 * j
square.goto(x, -y + 280)
square.stamp()
square.goto(-x, -y + 280)
square.stamp()
运行效果
以下图片需要打开观看动图图片效果
![图片[2]-马上圣诞节了,教你用Python画个圣诞树-FancyPig's blog](https://www.cvv-goods.com/wp-content/uploads/2023/03/20211218101307732.gif)
版本二
截图展示
![图片[3]-马上圣诞节了,教你用Python画个圣诞树-FancyPig's blog](https://www.cvv-goods.com/wp-content/uploads/2023/03/20211218102811229.webp)
相关代码
import turtle
# 定义圣诞树的绿叶函数
def tree(d, s):
if d <= 0:
return
turtle.forward(s)
tree(d - 1, s * .8)
turtle.right(120)
tree(d - 3, s * .5)
turtle.right(120)
tree(d - 3, s * .5)
turtle.right(120)
turtle.backward(s)
n = 100
""" 设置绘图速度
'fastest' : 0
'fast' : 10
'normal' : 6
'slow' : 3
'slowest' : 1
"""
turtle.speed('fastest') # 设置速度
turtle.left(90)
turtle.forward(3 * n)
turtle.color("orange", "yellow")
turtle.left(126)
# turtle.begin_fill()
for i in range(5):
turtle.forward(n / 5)
turtle.right(144)
turtle.forward(n / 5)
turtle.left(72)
turtle.end_fill()
turtle.right(126)
turtle.color("dark green")
turtle.backward(n * 4.8)
# 执行函数
tree(15, n)
turtle.backward(n / 5)
运行效果
以下图片需要打开观看动图图片效果
![图片[4]-马上圣诞节了,教你用Python画个圣诞树-FancyPig's blog](https://www.cvv-goods.com/wp-content/uploads/2023/03/20211218102646804.gif)
版本三
截图展示
![图片[5]-马上圣诞节了,教你用Python画个圣诞树-FancyPig's blog](https://www.cvv-goods.com/wp-content/uploads/2023/03/20211218113053110-1024x308-1.webp)
相关代码
# Python Program to Generate Christmas Tree Pattern
# Generating Triangle Shape
def triangleShape(n):
for i in range(n):
for j in range(n-i):
print(' ', end=' ')
for k in range(2*i+1):
print('*',end=' ')
print()
# Generating Pole Shape
def poleShape(n):
for i in range(n):
for j in range(n-1):
print(' ', end=' ')
print('* * *')
# Input and Function Call
row = int(input('猪头问你想画几行圣诞树: '))
triangleShape(row)
triangleShape(row)
poleShape(row)
运行效果
以下图片需要打开观看动图图片效果
![图片[6]-马上圣诞节了,教你用Python画个圣诞树-FancyPig's blog](https://www.cvv-goods.com/wp-content/uploads/2023/03/20211218112848516.gif)
版本四
截图展示
![图片[7]-马上圣诞节了,教你用Python画个圣诞树-FancyPig's blog](https://www.cvv-goods.com/wp-content/uploads/2023/03/20211218113611405.gif)
停止运行代码会有彩蛋哦!
相关代码
import argparse
import os
import random
import time
BALL = '⏺'
COLOR = {
'blue': '\033[94m',
'yellow': '\033[93m',
'cyan': '\033[96m',
'green': '\033[92m',
'magenta': '\033[95m',
'white': '\033[97m',
'red': '\033[91m'
}
STAR = '★'
def random_change_char(string, value):
indexes = random.sample(range(0, len(string)), value)
string = list(string)
for idx in indexes:
if string[idx] != ' ' and string[idx] == '_':
string[idx] = BALL
return ''.join(string)
def tree(height=13, screen_width=80):
star = (STAR, 3*STAR)
if height % 2 != 0:
height += 1
body = ['/_\\', '/_\_\\']
trunk = '[___]'
begin = '/'
end = '\\'
pattern = '_/'
j = 5
for i in range(7, height + 1, 2):
middle = pattern + (i - j) * pattern
line = ''.join([begin, middle[:-1], end])
body.append(line)
middle = middle.replace('/', '\\')
line = ''.join([begin, middle[:-1], end])
body.append(line)
j += 1
return [line.center(screen_width) for line in (*star, *body, trunk)]
def balls(tree):
for idx, _ in enumerate(tree[:-3], 2):
tree[idx] = random_change_char(tree[idx], len(tree[idx])//8)
return tree
def colored_stars_balls(tree):
for idx, _ in enumerate(tree):
string = list(tree[idx])
for pos, _ in enumerate(string):
if string[pos] == STAR:
string[pos] = ''.join([COLOR['yellow'], STAR, '\033[0m'])
elif string[pos] == BALL:
string[pos] = ''.join([random.choice(list(COLOR.values())), BALL, '\033[0m'])
tree[idx] = ''.join(string)
return tree
def cli():
parser = argparse.ArgumentParser(prog="Python Christmas Tree by Chico Lucio from Ciencia Programada",
epilog="Ctrl-C interrupts the Christmas :-(")
parser.add_argument('-s', '--size', default=13, type=int,
help="Tree height. If even it will be subtracted 1. If less than 7, considered 5. Default: 13")
parser.add_argument('-w', '--width', default=80, type=int,
help="Screen width. Used to center the tree. Default: 80")
parser.add_argument('-t', '--terminal', action='store_true',
help="Uses the terminal size to center the tree. -s and -w will be ignored")
args = parser.parse_args()
if args.terminal:
screen_width, height = os.get_terminal_size()
height -= 2
else:
height = args.size
screen_width = args.width
while True:
try:
time.sleep(random.uniform(.1, 1))
os.system('cls' if os.name == 'nt' else 'clear')
print('\n'.join(colored_stars_balls(balls(tree(height, screen_width)))))
except KeyboardInterrupt:
os.system('cls' if os.name == 'nt' else 'clear')
print(f"\n{'自由者联盟们圣诞节快乐哟':^{screen_width}}", end='\n\n')
break
if __name__ == '__main__':
cli()
运行效果
![图片[8]-马上圣诞节了,教你用Python画个圣诞树-FancyPig's blog](https://www.cvv-goods.com/wp-content/uploads/2023/03/20211218113710249.gif)
版本五
截图展示
![图片[9]-马上圣诞节了,教你用Python画个圣诞树-FancyPig's blog](https://www.cvv-goods.com/wp-content/uploads/2023/03/20211218120858269.webp)
相关代码
- 需要提前安装依赖
pip install colorama
pip install termcolor
- Python相关代码
import termcolor
import random
import time, datetime
import sys, os
from colorama import init
from termcolor import colored
def clear():
if sys.platform == 'win32': # if windows
return os.system('cls')
else: # if *nix
return os.system('clear')
colors = [
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white']
yellowlight = termcolor.colored('o', 'yellow')
magentalight = termcolor.colored('o', 'magenta')
cyanlight = termcolor.colored('o', 'cyan')
lightlist = [yellowlight, cyanlight, magentalight]
init()
while True: # exit with ctrl+C
print('要退出记得ctrl+C退出')
for i in range(1, 30, 2): # tree
tree = ''
for j in range(i): # make lights
randNum = random.randint(0, 500)
if (randNum <= 750) and (randNum >= 250):
tree += lightlist[random.randint(0, 2)]
else:
tree += termcolor.colored('*', 'green')
string = '_' * (15 - int(i / 2)) + tree + '_' * (15 - int(i / 2)) + '\n'
print(string, end='')
trunk = colored('mWm', 'yellow')
for k in range(3): # trunk
outbuffer = '_' * 14 + trunk + '_' * 14 + '\n'
print(outbuffer, end='')
merry_Christmas = termcolor.colored('自由者联盟们圣诞节快乐哟', colors[random.randint(0, len(colors) - 1)])
outbuffer2 = '_' * 8 + merry_Christmas + '_' * 8 + '\n'
print(outbuffer2, end='')
time.sleep(0.4)
clear()
运行效果
![图片[10]-马上圣诞节了,教你用Python画个圣诞树-FancyPig's blog](https://www.cvv-goods.com/wp-content/uploads/2023/03/20211218120833322.gif)
打包下载
© 版权声明
THE END
暂无评论内容