本文主要是介绍实战 | 教你用Python画各种版本的圣诞树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目介绍
大家好呀,这是一份迟到的圣诞节Python专辑项目。
我们一起看看如何用Python做出超级炫酷的圣诞树吧~
1.入门版本
height = 5
stars = 1
for i in range(height):print((' ' * (height - i)) + ('*' * stars))stars += 2
print((' ' * height) + '|')
2.进阶版
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*ifor j in range(i-k):x = 30*jsquare.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 += 2if 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*ifor j in range(3):x = 30*jsquare.goto(x,-y+280)square.stamp()square.goto(-x,-y+280)square.stamp()
turtle.exitonclick()
3.高级版本
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: returnforward(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)
gif:
4.炫酷版本
import os
import sys
import platform
import random
import time
import numpy as np
class UI(object):def __init__(self):os_name = platform.uname()[0]self.IS_WIN = os_name == 'Windows'self.IS_MAC = os_name == 'Darwin'if self.IS_WIN:self.RED = 0x0Cself.GREY = 0x07self.BLUE = 0x09self.CYAN = 0x0Bself.LINK = 0x30self.BLACK = 0x0self.GREEN = 0x0Aself.WHITE = 0x0Fself.PURPLE = 0x0Dself.YELLOW = 0x0Eelse:self.RED = '\033[1;31m'self.GREY = '\033[38m'self.BLUE = '\033[1;34m'self.CYAN = '\033[36m'self.LINK = '\033[0;36;4m'self.BLACK = '\033[0m'self.GREEN = '\033[32m'self.WHITE = '\033[37m'self.PURPLE = '\033[35m'self.YELLOW = '\033[33m'self.p = self.win_print if self.IS_WIN else self.os_printdef clear(self):os.system('cls' if self.IS_WIN else 'clear')return selfdef win_reset(self, color):from ctypes import windllhandler = windll.kernel32.GetStdHandle(-11)return windll.kernel32.SetConsoleTextAttribute(handler, color)def win_print(self, msg, color, enter=True):color = color or self.BLACKself.win_reset(color | color | color)sys.stdout.write(('%s\n' if enter else '%s') % msg)self.win_reset(self.RED | self.GREEN | self.BLUE)return selfdef os_print(self, msg, color, enter=True):color = color or self.BLACKsys.stdout.write(('%s%s%s\n' if enter else '%s%s%s') % (color, msg, self.BLACK))return self
def tree(ui, level=3):a = [i for i in range(0, (level + 1) * 4, 2)]b = a[0:2]for i in range(2, len(a) - 2, 2):b.append(a[i])b.append(a[i + 1])b.append(a[i])b.append(a[i + 1])b.append(a[-2])b.append(a[-1])light = Truewhile True:ui.clear()ui.p(u'\t圣诞节快乐!\n\t\t\t覃秉丰 2018', ui.RED)printlight = not lightlamp(ui, b, light)for i in range(2, len(b)):ui.p('%s/' % (' ' * b[len(b) - i - 1]), ui.GREEN, enter=False)neon(ui, 2 * b[i] + 1)ui.p('\\', ui.GREEN, enter=True)time.sleep(0.2)
def neon(ui, space_len):colors = [ui.RED, ui.GREY, ui.BLUE, ui.CYAN, ui.YELLOW]for i in range(space_len):if random.randint(0, 16) == 5:ui.p('o', colors[random.randint(0, len(colors) - 1)], enter=False)else:ui.p(' ', ui.RED, enter=False)
def lamp(ui, tree_arr, light):colors = [ui.WHITE, ui.BLUE]if not light:colors.reverse()ui.p(' ' * (tree_arr[-1] + 1), ui.BLACK, enter=False)ui.p('|', colors[1])ui.p(' ' * tree_arr[-1], ui.BLACK, enter=False)ui.p('\\', colors[1], enter=False)ui.p('|', colors[0], enter=False)ui.p('/', colors[1])ui.p(' ' * tree_arr[-2], ui.BLACK, enter=False)ui.p('-', colors[0], enter=False)ui.p('-', colors[1], enter=False)ui.p('=', colors[0], enter=False)ui.p('O', colors[1], enter=False)ui.p('=', colors[0], enter=False)ui.p('-', colors[1], enter=False)ui.p('-', colors[0], enter=True)ui.p(' ' * tree_arr[-1], ui.BLACK, enter=False)ui.p('/', colors[1], enter=False)ui.p('|', colors[0], enter=False)ui.p('\\', colors[1])ui.p(' ' * tree_arr[-2], ui.BLACK, enter=False)ui.p('/ ', ui.GREEN, enter=False)ui.p('|', colors[1], enter=False)ui.p(' \\', ui.GREEN, enter=True)
def main():ui = UI()max_rows = 4tree(ui, max_rows)
if __name__ == '__main__':main()
在命令行执行可以看到动态效果:
作者介绍
这篇关于实战 | 教你用Python画各种版本的圣诞树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!