一年一度的圣诞节来啦,今年的圣诞节,你打算怎么过?不管和谁一起过,都先来看看我用python给各位准备的圣诞节礼物吧!
一、绘制成品
1、月下圣诞
2、圣诞树
3、童话圣诞
4、唯美圣诞
5、狂欢圣诞
6、两个人的圣诞
7、冰雪圣诞
二、实现代码
实现本文效果的整体思路是:加载库—选择背景图片和音乐—设置动态效果—播放音乐。
1、加载库
要想实现本文的圣诞礼物,要先在python中加载以下库。
import pygame
import random
import os
from turtle import *
from pygame.locals import *
import tkinter as tk
from PIL import Image
from tkinter import filedialog
2、选择背景图片和音乐
根据如下代码可以自定义想要的背景图片和音乐。
root = tk.Tk()
root.withdraw()
Folderpath = filedialog.askdirectory(title = '请选择图片存放的文件夹') # 获得选择好的文件夹
bgpath = filedialog.askopenfilename(title = '请选择背景图片')
mucpath = filedialog.askopenfilename(title = '请选择音乐')
os.chdir(Folderpath)
运行如上代码,会在电脑桌面弹出窗口,会提示你依次选择存放文件的文件夹、背景图片和音乐。
3、设置动态效果并播放音乐
接着用如下代码控制动态效果并播放音乐。
pygame.init() #初始化pygame
SIZE = (790, 430) #设置屏幕宽长,根据背景图片调整(或者调整图片)
bg_size = width, height = 300, 200 #设置开始和关闭界面窗口
bg_rgb = (255, 255, 255)
screen = pygame.display.set_mode(bg_size)
screen = pygame.display.set_mode(SIZE)
screen1 = pygame.display.set_mode(SIZE)
pygame.display.set_caption("圣诞节快乐")
ori_bg = Image.open(bgpath)
new_bg = ori_bg.resize((790, 430))
new_bg.save(Folderpath + '/new_bg.gif')
bg = pygame.image.load(Folderpath + '/new_bg.gif')
snow_list = [] #雪花列表
for i in range(300):
x_site = random.randrange(0, SIZE[0]) #雪花圆心位置
y_site = random.randrange(0, SIZE[1]) #雪花圆心位置
X_shift = random.randint(-1, 1) #x轴偏移量
radius = random.randint(4, 6) #半径和y周下降量
snow_list.append([x_site, y_site, X_shift, radius])
clock = pygame.time.Clock() #创建设置帧率对象
track = pygame.mixer.music.load(mucpath) # 加载音乐文件
pygame.mixer.music.play() # 开始播放音乐流
pygame.mixer.music.fadeout(600000) #设置音乐多久慢慢淡出结束
play_image = pygame.image.load(bgpath).convert_alpha() # 创建播放图片surface对象
pause_image = pygame.image.load(bgpath).convert_alpha() # 创建暂停图片surface对象
pause_rect = pause_image.get_rect() #获取播放矩形框
print(pause_rect.width,pause_rect.height) #获取暂停矩形框
pause_rect.left, pause_rect.top = (width - pause_rect.width) // 2, (height - pause_rect.height) // 2
pause = False # 定义播放标志位
while True:
# 查找队列事件
for event in pygame.event.get():
# 查找点击关闭窗口事件
if event.type == pygame.QUIT:
sys.exit()
# 查找鼠标左右击事件
if event.type == MOUSEBUTTONDOWN:
# 检测鼠标左击是否按下
if event.button == 1:
pause = not pause
# 检测鼠标右击是否按下
if event.button == 3:
pause = not pause
# 检测键是否按下
if event.type == KEYDOWN:
# 检测是否为空格键按下
if event.key == K_SPACE:
pause = not pause
# 填充界面背景
screen.fill(bg_rgb)
# 空格控制播放和暂停,并显示相应的图片
if pause:
pygame.mixer.music.pause()
screen.blit(pause_image, pause_rect)
else:
pygame.mixer.music.unpause()
screen.blit(play_image, pause_rect)
screen1.blit(bg, (0, 0)) #图片背景
j = 0
for i in range(len(snow_list)):
j += 1
if j<10:
pygame.draw.circle(screen1, (255, 255, 255), snow_list[i][:2], snow_list[i][3]-3)
elif j<20:
pygame.draw.circle(screen1, (random.randint(200, 255), random.randint(200, 255), random.randint(200, 255)), snow_list[i][:2], snow_list[i][3]-3)
elif j<30:
pygame.draw.circle(screen1, (random.randint(100, 200), random.randint(100, 200), random.randint(100, 200)), snow_list[i][:2], snow_list[i][3]-3)
elif j<40:
pygame.draw.circle(screen1, (random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)), snow_list[i][:2], snow_list[i][3]-3)
else:
pygame.draw.circle(screen1, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), snow_list[i][:2], snow_list[i][3]-3)
# 移动雪花位置(下一次循环起效)
snow_list[i][0] += snow_list[i][2]
snow_list[i][1] += snow_list[i][3]
if snow_list[i][1] > SIZE[1]: #判断雪花位置是否超出屏幕,是的话重设位置
snow_list[i][0] = random.randrange(0, SIZE[0])
snow_list[i][1] = random.randrange(0, SIZE[1])
pygame.display.flip() #刷新屏幕
clock.tick(20)
pygame.quit() #退出
注:如想获得本文全量可执行的代码和背景图片,可在公众号中回复”圣诞礼物“,即可免费获取。
点击窗口可以实现暂停播放音乐,再点击一下可以复播。若 j += 1 则表示下落的是彩色图案,若 j += 0 则表示下落的是白色图案。
你可能感兴趣:
用Python绘制皮卡丘
用Python绘制词云图
Python人脸识别—我的眼里只有你
Python画好看的星空图(唯美的背景)
【Python】情人节表白烟花(带声音和文字)
用Python中的py2neo库操作neo4j,搭建关联图谱
Python浪漫表白源码合集(爱心、玫瑰花、照片墙、星空下的告白)