圣诞节礼物

        对于python小白的你,学了python没多久是不是就有了想自己动手做一个小程序的冲动呢?在这里我给大家分享一个我以前做的小程序——“圣诞节礼物”——希望能给大家带来灵感和帮助。

        话不多说,先看一下截图:

        这是一个小动画,还有背景音乐呢。

        成品我已上传到博客,可直接下载使用,名字为“圣诞节礼物”,文件名为“Merry Christmas_zhangyu_ILD"。但由于上传文件大小受限,所以使用前要先运行一下"untitled0.py”文件。

 

        以下是源代码(由于打的仓促,看起来不大好看(~_~)):

 

一、untitled0.py文件代码。因为现在的python的pygame库不支持直接导入视频播放功能,所以需要将视频按一定帧转化为图片使用。

# -*- coding: utf-8 -*-
"""
Created on Sat Dec 22 15:07:26 2018

@author: Hasee
"""
import cv2
import os

videos_src_path = r"..."  #视频地址
videos_save_path = r"..."  #图片保存地址
videos = os.listdir(videos_src_path)
videos = filter(lambda x: x.endswith('mp4'), videos)

for each_video in videos:
    frame_count = 1
    each_video_name , _ = each_video.split('.')
    os.mkdir(videos_save_path + '/' + each_video_name)
    each_video_save_full_path = os.path.join(videos_save_path, each_video_name) + '/'
    
    each_video_full_path = os.path.join(videos_src_path, each_video)
    
    cap = cv2.VideoCapture(each_video_full_path)
    
    success = True
    while success:
        success, frame = cap.read()
        print('Read a new frame: ', success)
        params = []
        params.append(1)
        
        if frame is not None and frame_count%3 == 0:
            cv2.imwrite(each_video_save_full_path + each_video_name + "%d.jpg" % frame_count, frame, params)
        frame_count = frame_count + 1
    cap.release()

 

二、绘制:

import pygame,sys,random
from pygame.transform import *

#画雪花
def Draw_snow(start_pos):
    pointlist = [start_pos,[start_pos[0]+10,start_pos[1]],[start_pos[0]-10,start_pos[1]],start_pos,[start_pos[0],start_pos[1]+10],[start_pos[0],start_pos[1]-10],
                 start_pos,[start_pos[0]+7,start_pos[1]+7],[start_pos[0]-7,start_pos[1]-7],start_pos,[start_pos[0]-7,start_pos[1]+7],[start_pos[0]+7,start_pos[1]-7]]
    pygame.draw.lines(screen,(255,255,255),False,pointlist,1)
    
#定义暂停
def Pause():
    active = True
    while active:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    active = False
                elif event.key == pygame.K_BACKSPACE:
                    sys.exit()



#指定图像文件名
background_image_filename = "image/Background.jpg"
cloth_image_filename = "image/Cloth.jpg"
icon_image_filename = "image/icon.jpg"
image_one_filename = "image/Santa1.jpg"
image_two_filename = "image/Santa2.jpg"
image_three_filename = "image/Santa3.jpg"
image_four_filename = "image/Santa4.jpg"
image_five_filename = "image/Santa5.jpg"
image_six_filename = "image/Santa6.jpg"
image_seven_filename = "image/Santa7.jpg"

#指定音乐名
background_music_filename = "music/Feuerherz - Merry Christmas.mp3"
pygame.mixer.init()
pygame.mixer.music.load(background_music_filename)
pygame.mixer.music.play(-1,0)


#输入文字
pygame.font.init()
font = pygame.font.SysFont("engravers",40)
text_surface = font.render("Merry Christmas",True,(255,0,0))

font_two = pygame.font.SysFont("engravers",15)
wish_one = font_two.render("May you not be alone at Christmas",True,(255,0,0))
wish_two = font_two.render("My love and joy to you",True,(255,0,0))
wish_three = font_two.render("It is so happy to have your experience",True,(255,0,0))
wish_four = font_two.render("Wish you a Merry Christmas",True,(255,0,0))
wish_five = font_two.render("Wish you a Merry Christmas",True,(255,0,0))
text_surface_dict = {1:wish_one,2:wish_two,3:wish_three,4:wish_four,5:wish_five}

#初始化init()及设置
pygame.init()

#设置窗口图标
icon = pygame.image.load(icon_image_filename)
icon = scale(icon,(32,32))
pygame.display.set_icon(icon)

#设置窗口大小
#Info = pygame.display.Info()
size = width, heigh = 600,400
#size = width, heigh = Info.current_w,Info.current_h
#screen = pygame.display.set_mode(size)
screen = pygame.display.set_mode(size,pygame.FULLSCREEN,32)


#加载并转换图像
background_primary = pygame.image.load(background_image_filename).convert()#使用convert,blit的速度会快很多
cloth_primary = pygame.image.load(cloth_image_filename).convert()
image_one_primary = pygame.image.load(image_one_filename).convert_alpha()
image_two_primary = pygame.image.load(image_two_filename).convert_alpha()
image_three_primary = pygame.image.load(image_three_filename).convert_alpha()
image_four_primary = pygame.image.load(image_four_filename).convert_alpha()
image_five_primary = pygame.image.load(image_five_filename).convert_alpha()
image_six_primary = pygame.image.load(image_six_filename).convert_alpha()
image_seven_primary = pygame.image.load(image_seven_filename).convert_alpha()

#缩放图片
background = scale(background_primary,size)
cloth = scale(cloth_primary,size)

#设置Santa的刷新大小
update_size = (240,160)
image_one = scale(image_one_primary,update_size)
image_two = scale(image_two_primary,update_size)
image_three = scale(image_three_primary,update_size)
image_four = scale(image_four_primary,update_size)
image_five = scale(image_five_primary,update_size)
image_six = scale(image_six_primary,update_size)
image_seven = scale(image_seven_primary,update_size)

#设置游戏名称
pygame.display.set_caption("Merry Christmas")

#设置游戏运行速度
fclock = pygame.time.Clock()
fps = 10
speed = [10,10]
begin = 1
count = 1
snow = 0
snow_dis = []

done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                Pause()
            elif event.key == pygame.K_BACKSPACE:
                done = True
    if begin < 86:
        image_gather = {'1':image_one,'2':image_two,'3':image_three,'4':image_four,'5':image_five,'6':image_six,'0':image_seven}
        image_choice = image_gather[str(begin%7)]
        x = count*10-240
        x1 = count*10-240

        #Santa和画布移动
        if x < 0 :
            x = 0
        screen.blit(background,(0,0))
        pygame.display.update((0,0),(x,400))
        screen.blit(cloth,(x,0))
        pygame.display.update((x,0),(600-x,400))#x是rante图案左侧的位置
        screen.blit(image_choice,(x1,140))
        pygame.display.update((x1,140),update_size)
    
    else :
        done = True
    begin = begin + 1
    count = count + 1
    fclock.tick(fps)
    
done = False

#获得图片
file_num_list_p = [ i for i in range(1866) if i%3 == 0]
file_num_list_p.remove(0)
file_num_list = file_num_list_p
filename = []
for i in range(len(file_num_list)):
    file = "image/Wish/Wish" + str(file_num_list[i]) + ".jpg"
    filename.append(file)
obj = 0
begin = 0
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                Pause()
            elif event.key == pygame.K_BACKSPACE:
                done = True
    screen.blit(background,(0,0))
    pygame.display.update((0,0),size)  
    if True:
        #screen.blit(background,(0,0))
        #pygame.display.update((0,0),size)  
        #画雪
        snow_number = random.randint(5,8)
        for i in range(abs(snow_number)):
            xlabel = random.randint(1,600)
            ylabel = 0
            Draw_snow([xlabel,ylabel])
            snow = snow+1
            snow_dis.append([xlabel,ylabel])
        snow = snow-1
        n = snow
        m = 0
        for unit in range(abs(n)):
            xl = snow_dis[unit][0]
            yl = snow_dis[unit][1] + 10
            Draw_snow([xl,yl])
            if yl == 300:
                m = m+1
            else :
                snow_dis[unit] = [xl,yl]
        if m > 0:
            for j in range(m-1):
                snow = snow-1
                del snow_dis[0]
        screen.blit(text_surface,(45,30))
        pygame.display.flip()
    #加载视频图片    
    if begin >= 10 and count <= len(file_num_list)-1:
        video = pygame.image.load(filename[obj]).convert_alpha()
        video_obj = scale(video,(240,120))
        screen.blit(video_obj,(200,260))
        #pygame.display.update((150,260),(100,50))
        pygame.display.update()
        count = count + 1
    else:
        if count-len(file_num_list) >= 30:
            screen.blit(text_surface_dict[1],(45,100))
        if count-len(file_num_list) >= 60:
            screen.blit(text_surface_dict[2],(45,160))
        if count-len(file_num_list) >= 90:
            screen.blit(text_surface_dict[3],(45,220))
        if count-len(file_num_list) >= 120 :
            screen.blit(text_surface_dict[4],(45,280))
        if count-len(file_num_list) >= 150:
            screen.blit(text_surface_dict[5],(45,340))
        pygame.display.flip()
        count = count + 1
    begin = begin + 1
    obj = obj+1
    fclock.tick(5)
pygame.quit()

这样就OK了。

(其中一些视频和音乐资料都在我上传“Merry Christmas_zhangyu_ILD"文件里。)

三、用pyinstaller打包成可执行程序即可。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值