画蛋糕

主要使用python的turtle库进行绘制
speed控制画笔的速度,大小在(0, 10),0是最快的
代码如下

import turtle as timport math as mimport random as rdef drawX(a, i):angle = m.radians(i)return a * m.cos(angle)def drawY(b, i):angle = m.radians(i)return b * m.sin(angle)t.bgcolor("#d3dae8")t.setup(width=900, height=600, startx=0, starty=0)t.title("姓名,祝你生日快乐!")t.speed(1)t.penup()t.goto(150, 0)t.pendown()# 1t.pencolor("white")t.begin_fill()for i in range(360):x = drawX(150, i)y = drawY(60, i)t.goto(x, y)t.fillcolor("#fef5f7")t.end_fill()# 2t.begin_fill()for i in range(180):x = drawX(150, -i)y = drawY(70, -i)t.goto(x, y)for i in range(180, 360):x = drawX(150, i)y = drawY(60, i)t.goto(x, y)t.fillcolor("#f2d7dd")t.end_fill()# 3t.pu()t.goto(120, 0)t.pd()t.begin_fill()for i in range(360):x = drawX(120, i)y = drawY(48, i)t.goto(x, y)t.fillcolor("#cbd9f9")t.end_fill()# 4t.begin_fill()t.pencolor("#fee48c")for i in range(540):x = drawX(120, i)y = drawY(48, i) + 70t.goto(x, y)t.goto(-120, 0)t.fillcolor("#cbd9f9")t.end_fill()# 5t.pu()t.goto(120, 70)t.pd()t.pencolor("#fff0f3")t.begin_fill()for i in range(360):x = drawX(120, i)y = drawY(48, i) + 70t.goto(x, y)t.fillcolor("#fff0f3")t.end_fill()# 6t.pu()t.goto(110, 70)t.pd()t.pencolor("#fff9fb")t.begin_fill()for i in range(360):x = drawX(110, i)y = drawY(44, i) + 70t.goto(x, y)t.fillcolor("#fff9fb")t.end_fill()# 7t.pu()t.goto(120, 0)t.pd()t.begin_fill()t.pencolor("#ffa79d")for i in range(180):x = drawX(120, -i)y = drawY(48, -i) + 10t.goto(x, y)t.goto(-120, 0)for i in range(180, 360):x = drawX(120, i)y = drawY(48, i)t.goto(x, y)t.fillcolor("#ffa79d")t.end_fill()# 8t.pu()t.goto(120, 70)t.pd()t.begin_fill()t.pensize(4)t.pencolor("#fff0f3")for i in range(1800):x = drawX(120, 0.1 * i)y = drawY(-18, i) + 10t.goto(x, y)t.goto(-120, 70)t.pensize(1)for i in range(180, 360):x = drawX(120, i)y = drawY(48, i) + 70t.goto(x, y)t.fillcolor("#fff0f3")t.end_fill()# 9t.pu()t.goto(80, 70)t.pd()t.begin_fill()t.pencolor("#6f3732")t.goto(80, 120)for i in range(180):x = drawX(80, i)y = drawY(32, i) + 120t.goto(x, y)t.goto(-80, 70)for i in range(180, 360):x = drawX(80, i)y = drawY(32, i) + 70t.goto(x, y)t.fillcolor("#6f3732")t.end_fill()# 10t.pu()t.goto(80, 120)t.pd()t.pencolor("#ffaaa0")t.begin_fill()for i in range(360):x = drawX(80, i)y = drawY(32, i) + 120t.goto(x, y)t.fillcolor("#ffaaa0")t.end_fill()# 11t.pu()t.goto(70, 120)t.pd()t.pencolor("#ffc3be")t.begin_fill()for i in range(360):x = drawX(70, i)y = drawY(28, i) + 120t.goto(x, y)t.fillcolor("#ffc3be")t.end_fill()# 12t.pu()t.goto(80, 120)t.pd()t.begin_fill()t.pensize(3)t.pencolor("#ffaaa0")for i in range(1800):x = drawX(80, 0.1 * i)y = drawY(-12, i) + 80t.goto(x, y)t.goto(-80, 120)t.pensize(1)for i in range(180, 360):x = drawX(80, i)y = drawY(32, i) + 120t.goto(x, y)t.fillcolor("#ffaaa0")t.end_fill()# 13t.pu()t.goto(64, 120)t.pd()t.pencolor("#b1c9e9")t.begin_fill()for i in range(360):x = drawX(4, i) + 60y = drawY(1, i) + 120t.goto(x, y)t.goto(64, 170)for i in range(540):x = drawX(4, i) + 60y = drawY(1, i) + 170t.goto(x, y)t.goto(56, 120)t.fillcolor("#b1c9e9")t.end_fill()t.pencolor("white")t.pensize(2)for i in range(1, 6):t.goto(64, 120 + 10 * i)t.pu()t.goto(56, 120 + 10 * i)t.pd()t.pu()t.goto(60, 170)t.pd()t.goto(60, 180)t.pensize(1)#t.pu()t.goto(64, 190)t.pd()t.pencolor("#f1add1")t.begin_fill()for i in range(360):x = drawX(4, i) + 60y = drawY(10, i) + 190t.goto(x, y)t.fillcolor("#f1add1")t.end_fill()# 14t.pu()t.goto(-56, 120)t.pd()t.pencolor("#b1c9e9")t.begin_fill()for i in range(360):x = drawX(4, i) - 60y = drawY(1, i) + 120t.goto(x, y)t.goto(-56, 170)for i in range(540):x = drawX(4, i) - 60y = drawY(1, i) + 170t.goto(x, y)t.goto(-64, 120)t.fillcolor("#b1c9e9")t.end_fill()t.pencolor("white")t.pensize(2)for i in range(1, 6):t.goto(-56, 120 + 10 * i)t.pu()t.goto(-64, 120 + 10 * i)t.pd()t.pu()t.goto(-60, 170)t.pd()t.goto(-60, 180)t.pensize(1)#t.pu()t.goto(-56, 190)t.pd()t.pencolor("#f1add1")t.begin_fill()for i in range(360):x = drawX(4, i) - 60y = drawY(10, i) + 190t.goto(x, y)t.fillcolor("#f1add1")t.end_fill()# 15t.pu()t.goto(0, 130)t.pd()t.pencolor("#b1c9e9")t.begin_fill()for i in range(360):x = drawX(4, i)y = drawY(1, i) + 130t.goto(x, y)t.goto(4, 180)for i in range(540):x = drawX(4, i)y = drawY(1, i) + 180t.goto(x, y)t.goto(-4, 130)t.fillcolor("#b1c9e9")t.end_fill()t.pencolor("white")t.pensize(2)for i in range(1, 6):t.goto(4, 130 + 10 * i)t.pu()t.goto(-4, 130 + 10 * i)t.pd()t.pu()t.goto(0, 180)t.pd()t.goto(0, 190)t.pensize(1)#t.pu()t.goto(4, 200)t.pd()t.pencolor("#f1add1")t.begin_fill()for i in range(360):x = drawX(4, i)y = drawY(10, i) + 200t.goto(x, y)t.fillcolor("#f1add1")t.end_fill()# 16t.pu()t.goto(30, 110)t.pd()t.pencolor("#b1c9e9")t.begin_fill()for i in range(360):x = drawX(4, i) + 30y = drawY(1, i) + 110t.goto(x, y)t.goto(34, 160)for i in range(540):x = drawX(4, i) + 30y = drawY(1, i) + 160t.goto(x, y)t.goto(26, 110)t.fillcolor("#b1c9e9")t.end_fill()t.pencolor("white")t.pensize(2)for i in range(1, 6):t.goto(34, 110 + 10 * i)t.pu()t.goto(26, 110 + 10 * i)t.pd()t.pu()t.goto(30, 160)t.pd()t.goto(30, 170)t.pensize(1)#t.pu()t.goto(34, 180)t.pd()t.pencolor("#f1add1")t.begin_fill()for i in range(360):x = drawX(4, i) + 30y = drawY(10, i) + 180t.goto(x, y)t.fillcolor("#f1add1")t.end_fill()# 17t.pu()t.goto(-30, 110)t.pd()t.pencolor("#b1c9e9")t.begin_fill()for i in range(360):x = drawX(4, i) - 30y = drawY(1, i) + 110t.goto(x, y)t.goto(-26, 160)for i in range(540):x = drawX(4, i) - 30y = drawY(1, i) + 160t.goto(x, y)t.goto(-34, 110)t.fillcolor("#b1c9e9")t.end_fill()t.pencolor("white")t.pensize(2)for i in range(1, 6):t.goto(-26, 110 + 10 * i)t.pu()t.goto(-34, 110 + 10 * i)t.pd()t.pu()t.goto(-30, 160)t.pd()t.goto(-30, 170)t.pensize(1)#t.pu()t.goto(-26, 180)t.pd()t.pencolor("#f1add1")t.begin_fill()for i in range(360):x = drawX(4, i) - 30y = drawY(10, i) + 180t.goto(x, y)t.fillcolor("#f1add1")t.end_fill()###随机color = ["#e28cb9", "#805a8c", "#eaa989", "#6e90b7", "#b8b68f", "#e174b5", "#cf737c", "#7c8782"]for i in range(80):t.pu()x = r.randint(-120, 120)y = r.randint(-25, 30)t.goto(x, y)t.pd()t.dot(r.randint(2, 5), color[r.randint(0, 7)])for i in range(40):t.pu()x = r.randint(-90, 90)y = r.randint(-35, 10)t.goto(x, y)t.pd()t.dot(r.randint(2, 5), color[r.randint(0, 7)])for i in range(40):t.pu()x = r.randint(-80, 80)y = r.randint(60, 90)t.goto(x, y)t.pd()t.dot(r.randint(2, 5), color[r.randint(0, 7)])for i in range(30):t.pu()x = r.randint(-50, 50)y = r.randint(45, 70)t.goto(x, y)t.pd()t.dot(r.randint(2, 5), color[r.randint(0, 7)])for i in range(50):t.pu()x = r.randint(-500, 500)y = r.randint(120, 300)t.goto(x, y)t.pd()t.dot(r.randint(3, 5), color[r.randint(0, 7)])t.seth(90)t.pu()t.goto(0, 0)t.fd(210)t.left(90)t.fd(170)t.pd()t.write("Happy Birthday", font=("Curlz MT", 50))t.color('blue')t.penup()t.goto(-400, 210)t.pendown()t.write('致:好友名字', font=('楷体', 32, 'bold'))t.color('red')t.penup()t.goto(-300, 50)t.pendown()t.write('祝 你 生 日 快 乐!前 程 似 锦!', font=('楷体', 30, 'bold'))t.color('blue')t.penup()t.goto(100, -220)t.pendown()t.done()

这里是运行效果图

画烟花

主要使用pygame实现烟花效果
代码如下

background = pygame.image.load("happy birthday.jpg") # 背景

其中这个是烟花的背景图,“happy birthday”是图片的名字,这个图片需要和.py文件放在一个文件夹里面

import pygamefrom random import randint, uniform, choiceimport math vector = pygame.math.Vector2gravity = vector(0, 0.3)DISPLAY_WIDTH = DISPLAY_HEIGHT = 800 trail_colours = [(45, 45, 45), (60, 60, 60), (75, 75, 75), (125, 125, 125), (150, 150, 150)]dynamic_offset = 1static_offset = 3 class Firework:def __init__(self):# 随机颜色self.colour = (randint(0, 255), randint(0, 255), randint(0, 255))self.colours = ((randint(0, 255), randint(0, 255), randint(0, 255)),(randint(0, 255), randint(0, 255), randint(0, 255)),(randint(0, 255), randint(0, 255), randint(0, 255)))self.firework = Particle(randint(0, DISPLAY_WIDTH), DISPLAY_HEIGHT, True, self.colour)# Creates the firework particleself.exploded = Falseself.particles = []self.min_max_particles = vector(100, 225) def update(self, win):# called every frameif not self.exploded:self.firework.apply_force(gravity)self.firework.move()for tf in self.firework.trails:tf.show(win) self.show(win) if self.firework.vel.y >= 0:self.exploded = Trueself.explode()else:for particle in self.particles:particle.apply_force(vector(gravity.x + uniform(-1, 1) / 20, gravity.y / 2 + (randint(1, 8) / 100)))particle.move()for t in particle.trails:t.show(win)particle.show(win) def explode(self):# amount 数量amount = randint(self.min_max_particles.x, self.min_max_particles.y)for i in range(amount):self.particles.append(Particle(self.firework.pos.x, self.firework.pos.y, False, self.colours)) def show(self, win):pygame.draw.circle(win, self.colour, (int(self.firework.pos.x), int(self.firework.pos.y)), self.firework.size) def remove(self):if self.exploded:for p in self.particles:if p.remove is True:self.particles.remove(p) if len(self.particles) == 0:return Trueelse:return Falseclass Particle:def __init__(self, x, y, firework, colour):self.firework = fireworkself.pos = vector(x, y)self.origin = vector(x, y)self.radius = 20self.remove = Falseself.explosion_radius = randint(5, 18)self.life = 0self.acc = vector(0, 0)self.trails = []# stores the particles trail objectsself.prev_posx = [-10] * 10# stores the 10 last positionsself.prev_posy = [-10] * 10# stores the 10 last positions if self.firework:self.vel = vector(0, -randint(17, 20))self.size = 5self.colour = colourfor i in range(5):self.trails.append(Trail(i, self.size, True))else:self.vel = vector(uniform(-1, 1), uniform(-1, 1))self.vel.x *= randint(7, self.explosion_radius + 2)self.vel.y *= randint(7, self.explosion_radius + 2)# 向量self.size = randint(2, 4)self.colour = choice(colour)# 5 个 tails总计for i in range(5):self.trails.append(Trail(i, self.size, False)) def apply_force(self, force):self.acc += force def move(self):if not self.firework:self.vel.x *= 0.8self.vel.y *= 0.8self.vel += self.accself.pos += self.velself.acc *= 0 if self.life == 0 and not self.firework:# check if particle is outside explosion radiusdistance = math.sqrt((self.pos.x - self.origin.x) ** 2 + (self.pos.y - self.origin.y) ** 2)if distance > self.explosion_radius:self.remove = True self.decay() self.trail_update() self.life += 1 def show(self, win):pygame.draw.circle(win, (self.colour[0], self.colour[1], self.colour[2], 0), (int(self.pos.x), int(self.pos.y)), self.size) def decay(self):# random decay of the particlesif 50 > self.life > 10:# early stage their is a small chance of decayran = randint(0, 30)if ran == 0:self.remove = Trueelif self.life > 50:ran = randint(0, 5)if ran == 0:self.remove = True def trail_update(self):self.prev_posx.pop()self.prev_posx.insert(0, int(self.pos.x))self.prev_posy.pop()self.prev_posy.insert(0, int(self.pos.y)) for n, t in enumerate(self.trails):if t.dynamic:t.get_pos(self.prev_posx[n + dynamic_offset], self.prev_posy[n + dynamic_offset])else:t.get_pos(self.prev_posx[n + static_offset], self.prev_posy[n + static_offset])class Trail:def __init__(self, n, size, dynamic):self.pos_in_line = nself.pos = vector(-10, -10)self.dynamic = dynamic if self.dynamic:self.colour = trail_colours[n]self.size = int(size - n / 2)else:self.colour = (255, 255, 200)self.size = size - 2if self.size < 0:self.size = 0 def get_pos(self, x, y):self.pos = vector(x, y) def show(self, win):pygame.draw.circle(win, self.colour, (int(self.pos.x), int(self.pos.y)), self.size)def update(win, fireworks):for fw in fireworks:fw.update(win)if fw.remove():fireworks.remove(fw) pygame.display.update()def main():pygame.init()pygame.font.init()pygame.display.set_caption("Happy birthday to you!") # 标题background = pygame.image.load("happy birthday.jpg") # 背景win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))clock = pygame.time.Clock() fireworks = [Firework() for i in range(2)]# create the first fireworksrunning = True while running:clock.tick(60)for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.KEYDOWN:# Change game speed with number keysif event.key == pygame.K_1: # 按下 1fireworks.append(Firework())if event.key == pygame.K_2: # 按下 2 加入10个烟花for i in range(10):fireworks.append(Firework())win.fill((20, 20, 30))# draw backgroundwin.blit(background,(0,0))if randint(0, 20) == 1:# create new fireworkfireworks.append(Firework()) update(win, fireworks)pygame.quit()quit() main()

运行结果截图如下

python第三方库的导入

导入第三方库的时候,可以在命令行输入pip install (要导入的库的名字),如
pip install pygame
pip install pyinstaller

用pyinstaller打包成exe文件

写完的python想给别人用,但是别人电脑上又没有环境,怎么运行?

这时候,pyinstaller就开始表演了。Python生成可执行文件(exe ),即打包
先安装pyinstaller包

pip install pyinstaller

在终端上输入

 py -3.10 -m PyInstaller -F '.\happy birthday.py'

或者

python -m PyInstaller -v

具体的”-m”, “-v”含义如下图

这是打包后的exe文件样子,把这个转发给好朋友,即使没有python的环境也可以运行了