画蛋糕

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

import turtle as t
import math as m
import 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()
# 1
t.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()
# 2
t.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()
# 3
t.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()
# 4
t.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()
# 5
t.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()
# 6
t.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()
# 7
t.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()
# 8
t.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()
# 9
t.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()
# 10
t.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()
# 11
t.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()
# 12
t.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()
# 13
t.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()# 14
t.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()
# 15
t.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()
# 16
t.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()
# 17
t.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 pygame
from random import randint, uniform, choice
import mathvector = pygame.math.Vector2
gravity = vector(0, 0.3)
DISPLAY_WIDTH = DISPLAY_HEIGHT = 800trail_colours = [(45, 45, 45), (60, 60, 60), (75, 75, 75), (125, 125, 125), (150, 150, 150)]
dynamic_offset = 1
static_offset = 3class 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 positionsif 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 += forcedef move(self):if not self.firework:self.vel.x *= 0.8self.vel.y *= 0.8self.vel += self.accself.pos += self.velself.acc *= 0if 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 = Trueself.decay()self.trail_update()self.life += 1def 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 = Truedef 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 = dynamicif 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 = 0def 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 = Truewhile 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的环境也可以运行了

用python给朋友做生日快乐祝福相关推荐

  1. python编程入门书籍推荐-给刚入门python的朋友推荐几本书

    给刚入门python的朋友推荐几本书 1.python编程 从入门到实践 本书是一本针对所有层次的Python读者而作的Python入门书.全书分两部分:首部分介绍用Python 编程所必须了解的基本 ...

  2. python爱心代码_母亲节快到了,用Python给老妈写个祝福小程序吧~

    导 语 看到好多人留言问我咋好久没更新文章了,于是看了下上篇文章的发布日期,好吧确实挺久的,是该上线更一波文章了.想到母亲节快到了,不如就用Python给老妈写个祝福小程序吧~让我们愉快地开始吧~ 相 ...

  3. 怎么用计算机圣诞节快乐,2019圣诞节快乐祝福 最新圣诞节快乐祝福的话

    2019圣诞节到了,想好在朋友圈发点什么吗?在这里小编为大家分享一些圣诞节快乐祝福.一起来看看吧. 春生杨柳冬藏雪,平安夜后圣诞节.心有空灵因志高,从从容容是书香.十年寒窗十年苦,十年一觉十年梦.成功 ...

  4. 用Python给微信好友自动发送祝福语

    用Python给微信好友自动发送祝福语 学习了Python知识,可以做许多好玩的东西,可以用Python统计分析好友状态.好友性别.年龄.地区,以及好友签名等内容,今天咱们就用Python自动给微信好 ...

  5. 用html做祝福语朋友,有关于朋友之间的祝福语

    有关于朋友之间的祝福语 闭上眼睛,我小小的心愿会在今夜飞临到你窗前,和你新世纪所希冀的梦幻轻轻地重叠... 不是每朵浪花都为海滩而来,不是每颗星星都为夜幕而来,不是每次细雨都为麦苗而来,却只有我的信息 ...

  6. python朋友圈评论_利用Python实现朋友圈中的九宫格图片效果

    前言 大家应该经常在朋友圈看到有人发九宫格图片,其实质就是将一张图片切成九份,然后在微信中一起发这九张图即可. 说到切图,Python 就可以实现,主要用到的 Python 库为 Pillow,安装使 ...

  7. 使用python开发网页游戏_四大游戏编程网站,边玩游戏,边学Python,拒绝枯燥快乐编程...

    原标题:四大游戏编程网站,边玩游戏,边学Python,拒绝枯燥快乐编程 前言 学习编程虽然对有些人来说是件乐事,但是对大多数人来说仍然是一件比较枯燥困难的事情.当然,面临这样困惑的人,并不是只有你一个 ...

  8. python祝女朋友纪念日快乐

    python祝女朋友纪念日快乐 几天前是本菜鸡程序员与女票的纪念日,本菜鸡苦思冥想,经过两天的不断努力,终于成功做出了一个祝女票纪念日快乐的程序,现在分享给大家,这个程序名就姑且命为<奥特曼爱咸 ...

  9. CDA校区恭祝各位朋友元宵节快乐

    CDA成都校区恭祝各位朋友 元宵节快乐!

最新文章

  1. iOS下JS与OC互相调用(五)--UIWebView + WebViewJavascriptBridge
  2. 翻译器DIY它———算在英文文本中的单词数,字符和行数
  3. LeetCode-剑指 Offer 14- I. 剪绳子
  4. stm32 Bootloader设计(YModem协议)
  5. jointable 能加多个字段吗_Excel函数Countif、Countifs超级实用技巧,你真的都掌握吗?...
  6. “约见”面试官系列之常见面试题第三十四篇之事件冒泡、事件捕获、事件代理(建议收藏)
  7. 终于有人把正态分布和二八法则讲明白了
  8. 安卓系统为何这么容易被黑客入侵
  9. CCF NOI1045 元素之和
  10. warning: initialization from incompatible pointer type error, forbidden解决
  11. 移动机器人路径规划算法综述(二)
  12. c语言同构数怎么表示,C语言求同构数
  13. 从Internet上下载ActiveX(转)
  14. 输入电容及DCM-CCM-QR变压器计算
  15. 手动制作解除U盘写保护文件
  16. C14:Unity3D制作智能家居设计软件——双屏互动室内漫游
  17. UVa - 11283 - PLAYING BOGGLE
  18. 基于MATLAB的图像融合设计
  19. 为什么RocketMQ是金融核心系统消息中间件的第一选择
  20. NMS by Representative Region: Towards Crowded Pedestrian Detection by Proposal Pairing

热门文章

  1. python启发式算法学习总结
  2. 教你炒股票12:一吻消魂(3类买卖点)?
  3. 服务器vrrp协议,VRRP协议、Keepalived
  4. nginx反向代理服务contextpath的问题解决
  5. 循环语句中指针赋值出错
  6. 自动化部署、自动化运维、自动化配置
  7. MySQL拾遗之distinct用法
  8. 支付宝电脑网站支付 demo 启动过程
  9. Android第三方QQ登录,实用教程
  10. win7电脑一开机就蓝屏怎么回事?常见蓝屏代码