import pygame
from moviepy.editor import *
import sys
import os
import random
import pickle
#pygame.init() 初始化pygame,由于在moviepy中初始化了,所以不用重复
pygame.mixer.init()
pygame.display.set_caption("七夕礼物鸭")
icon = pygame.image.load('data_qixi\\ico.ico')
pygame.display.set_icon(icon)
def bgm_init():
pygame.mixer.music.load(work_path+'七夕.mp3') # 加载歌曲
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1) # 播放
#数据区begin
#=================常量=============================
reminder = "click the Heart" #设置提醒语
sec_tick = 30 #设置时间
size = width, height = 1280, 820 # 设置窗口大小
color = [0,0,0] # 设置颜色
dict_photo = {}
work_path = os.getcwd()+'\\data_qixi\\'
speed = [random.randint(1,9), random.randint(1,9)] # 设置移动的X轴、Y轴
#------------文件列表
with open(work_path+"doors_file.pkl",'rb') as picklefile:
doors_file = pickle.load(picklefile)
doors_inter = 0#迭代器
#==============pygame区===================
#------------球(心)--------------------
screen = pygame.display.set_mode(size) # 显示窗口
ball = pygame.image.load(work_path+'ball.png') # 加载图片
ballrect = ball.get_rect()
ballrect = ballrect.move([random.randint(0,width-ballrect.w),random.randint(0,height-ballrect.h)]) # 获取矩形区域
#------------门--------------------
#++++++++图像对象++++++++++
door = pygame.image.load(work_path+'door.png')
doors = []
doors_index = 0
for i in range(4):
doors.append(pygame.image.load(work_path+'door{0}.png'.format(i+1)))
#++++++++++位置对象++++++++
doorrect = door.get_rect()# 获取矩形区域
doorrect = doorrect.move(width//2-doorrect.w//2,height-doorrect.h-1)#居中
#------------箭头-------------------
#++++++++图像对象++++++++++
rarrow = pygame.image.load(work_path+'rarrow.png')
larrow = pygame.image.load(work_path+'larrow.png')
#++++++++++位置对象++++++++
rarrowrec = rarrow.get_rect()
larrowrec = larrow.get_rect()
arr_wid = rarrowrec.w
arr_hei = rarrowrec.h
larrowrec = larrowrec.move(width//8-arr_wid//2,height//2-arr_hei//2+20)
rarrowrec = rarrowrec.move(7*(width//8)-arr_wid//2,height//2-arr_hei//2+20)
#===============功能=================
clock = pygame.time.Clock() #设置时钟
bgm_init()
sound_open = pygame.mixer.Sound(work_path+"sound_open.mp3")#开门音效
sound_open.set_volume(0.2)
sound_change = pygame.mixer.Sound(work_path+"change.mp3")#转头音效
sound_change.set_volume(0.6)
sound_b = pygame.mixer.Sound(work_path+"boom.mp3")#点“心”音效
font = pygame.font.SysFont("Arial", 50)
#================flag标志===============================
flag_click1 = True #心是否被点击
flag_click2 = True #第一道白门是否被点击
flag_door = False #门是否被点击:进入照片事件
change_flag = 0 #颜色切换模式
#数据区end
#函数区begin
def change_color(trans_color,speed = 4):
if speed>0:
min_ = 0
max_ = 240
else:
min_ = 10
max_ = 255
if min_<=trans_color[0]<=max_:
for i in range(3):
trans_color[i] += speed
if(trans_color[0]>max_):
for i in range(3):
trans_color[i] = max_
elif(trans_color[0]<min_):
for i in range(3):
trans_color[i] = min_
else:
pass
screen.fill(trans_color)
return trans_color
def arrow_click_check(x,y,rect,change,doors_index):
if rect.left<=x<=rect.right and rect.top<=y<=rect.bottom:#向右箭头被点击
sound_change.play()
doors_index += change
if doors_index>=len(doors):
doors_index -= len(doors)
if doors_index<0:
doors_index += len(doors)
return 2,doors_index
return 0,doors_index
def show_photo(photo_name,dict_photo = {}):
if dict_photo == {}:
photo = pygame.image.load(work_path+photo_name)
rect = photo.get_rect()# 获取矩形区域
wid = rect.w
hei = rect.h
rect = rect.move(width//2-wid//2,height//2-hei//2)#居中
dict_photo["rect"] = rect
dict_photo["photo"] = photo
screen.blit(dict_photo["photo"],dict_photo["rect"])
pygame.display.flip()
return dict_photo
#函数区end
while True: # 死循环确保窗口一直显示
clock.tick(sec_tick) #时钟每秒执行30(sec_tick)次
if not flag_door:
if flag_click1:
for event in pygame.event.get(): # 遍历所有事件
if event.type == pygame.QUIT: # 如果单击关闭窗口,则退出
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mx,my = event.pos
if ballrect.left<=mx<=ballrect.right\
and ballrect.top<=my<=ballrect.bottom:
sound_b.play()
flag_click1 = False
ballrect = ballrect.move(speed) # 移动心
# 碰到左右边缘
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
# 碰到上下边缘
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(color) # 填充颜色
screen.blit(font.render(reminder, -1, (0,72,102)), (width//2-9*len(reminder), 100))
screen.blit(ball, ballrect) # 将图片画到窗口上
elif color[0]<=230 and flag_click2:
color = change_color(color)
for event in pygame.event.get(): # 遍历所有事件
if event.type == pygame.QUIT: # 如果单击关闭窗口,则退出
sys.exit()
elif flag_click2:
screen.blit(door, doorrect)
for event in pygame.event.get(): # 遍历所有事件
if event.type == pygame.QUIT: # 如果单击关闭窗口,则退出
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mx,my = event.pos
if doorrect.left<=mx<=doorrect.right\
and doorrect.top<=my<=doorrect.bottom:
flag_click2 = False
sound_open.play()
else:
if change_flag == 0:
screen.blit(doors[doors_index], doorrect)
screen.blit(rarrow,rarrowrec)
screen.blit(larrow,larrowrec)
for event in pygame.event.get(): # 遍历所有事件
if event.type == pygame.QUIT: # 如果单击关闭窗口,则退出
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:#点击事件
mx,my = event.pos
if doorrect.left<=mx<=doorrect.right\
and doorrect.top<=my<=doorrect.bottom:#门被点击
sound_open.play()
flag_door = True
door_time = pygame.time.get_ticks()
change_flag1,doors_index = arrow_click_check(mx,my,rarrowrec,1,doors_index)
change_flag2,doors_index = arrow_click_check(mx,my,larrowrec,-1,doors_index)#左右箭头点击检查
change_flag = max(change_flag1,change_flag2)
elif change_flag == 2:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
color = change_color(color,-12)
if color[0]<=11:
change_flag = 1
elif change_flag == 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
color = change_color(color,12)
if color[0]>=239:
change_flag = 0
pygame.display.flip()
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill(color)
try:
file = '\\doors_com\\'+doors_file[doors_index][doors_inter]
road,ext = os.path.splitext(file)
if ext =='.mp4':
pygame.mixer.music.fadeout(1000)
vedio = VideoFileClip(work_path+file)
vedio.preview()
door_time = pygame.time.get_ticks()
doors_inter += 1
screen = pygame.display.set_mode(size) # 显示窗口
bgm_init()
else:
if dict_photo:
pass
else:
dict_photo = show_photo(file,dict_photo)
screen.blit(dict_photo["photo"],dict_photo["rect"])
pygame.display.flip()
if (pygame.time.get_ticks()-door_time)//(10*sec_tick)>3:
door_time = pygame.time.get_ticks()
dict_photo = {}
doors_inter += 1
except IndexError:
screen.fill(color)
flag_door = False
dict_photo = {}
doors_inter = 0
pygame.display.flip()
# if (pygame.time.get_ticks()-door_time)//sec_tick>sep:
# flag_door = False
# screen.fill(color)
# 更新全部显示
pygame.quit() # 退出pygame
打包的exe就不放上来了,有私人图片(笑),还有pkl。