這是雪程式的1.1版本,
上個版本的文章---看這里:
忙活半天只為了看雪--送給大家的冬至禮物
https://blog.csdn.net/qq_54554848/article/details/121873955?spm=1001.2014.3001.5501(下述代碼基于上個版本)
上次我發布了--冬至禮物的博客,獲得了很多的瀏覽量,讓我甚是欣慰,首先在這里要感謝大家的支持,也感謝部分私信我的提出的疑問和反饋,尤其是對-----為什么我的冬至下雪程式安裝包這么大的問題的提出(打包后大于500M),讓我很是堪憂,恰巧,最近瀏覽了一篇大佬文章,讓我茅塞頓開,下面附上大佬文章鏈接:
解決 Python打包成exe 檔案過大問題的一些方法_老鷹的博客-CSDN博客_python打包exe太大
https://blog.csdn.net/weixin_42277380/article/details/112648319
還是要感謝大佬的文章,讓我快要爆滿的磁盤騰出了大部分空間,保住了寫過代碼的小命,現在介紹的1.1版本打包后僅僅七十多兆,不扯了,下面開始介紹原理:
先看一下本次程式效果:

上代碼:
這次代碼較上個版本有略微的修改:
體現在main.py檔案上,添加了啟動圣誕樹的按鈕
# 用戶視窗
import os
import tkinter
from PIL import ImageTk,Image
import snow_draw
import commands
import drawtree
root = tkinter.Tk()
size = 500, 400
root.geometry(f'{size[0]}x{size[1]}')
root.resizable(False, False)
# root.config(bg='silver')
# 我們將定義如下功能:開始、調整雪花個數、視窗自適應圖片大小比例、匯入別的圖片、修改圖片透明度、修改背景音樂、修復注冊機
# 如上功能由按鈕事件觸發的,將會隨著視窗變化而改變按鈕自適應(由于源代碼開源,使用者可能考慮修改用戶界面的大小,這里我因此靈活點)
# 先匯入界面背景圖片
image = Image.open('snow.jpg')
bg_image = ImageTk.PhotoImage(image)
width = bg_image.width()
height = bg_image.height()
root.geometry('%dx%d+0+0' % (width, height))
background_label = tkinter.Label(root, image=bg_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
# 開始按鈕
button10 = tkinter.Button(root, text='看雪', font=('華文行楷', 14),
bg='silver', fg='red', width=int(float(size[0] * 0.02)), height=int(float(0.0025 * size[1])),
command=snow_draw.main)
button100 = tkinter.Button(root, text='Christmas', font=('華文行楷', 14),
bg='silver', fg='yellow', width=int(float(size[0] * 0.02)), height=int(float(0.0025 * size[1])),
command=drawtree.main)
button10.place(x=int(float(size[0] * 0.8)), y=int(float(0.05 * size[1])))
button100.place(x=int(float(size[0] * 0.55)), y=int(float(0.05 * size[1])))
# 調整雪花個數按鈕
button11 = tkinter.Button(root, text='雪花個數', font=('華文行楷', 14),
bg='silver', fg='blue', width=int(float(size[0] * 0.02)), height=int(float(0.0025 * size[1])),
command=commands.snow_num)
button11.place(x=int(float(size[0] * 0.8)), y=int(float(0.20 * size[1])))
# 雪花視窗圖片自適應圖片按鈕
button12 = tkinter.Button(root, text='自適應', font=('華文行楷', 14),
bg='silver', fg='black', width=int(float(size[0] * 0.02)), height=int(float(0.0025 * size[1])),
command=commands.fit)
button12.place(x=int(float(size[0] * 0.8)), y=int(float(0.35 * size[1])))
# 匯入別的圖片
button13 = tkinter.Button(root, text='匯入圖片', font=('華文行楷', 14),
bg='silver', fg='yellow', width=int(float(size[0] * 0.02)), height=int(float(0.0025 * size[1])),
command=commands.import_image)
button13.place(x=int(float(size[0] * 0.8)), y=int(float(0.5 * size[1])))
# 匯入別的背景音樂
button14 = tkinter.Button(root, text='匯入音樂', font=('華文行楷', 14),
bg='silver', fg='indigo', width=int(float(size[0] * 0.02)), height=int(float(0.0025 * size[1])),
command=commands.music)
button14.place(x=int(float(size[0] * 0.8)), y=int(float(0.65 * size[1])))
# 修改圖片透明度
button15 = tkinter.Button(root, text='透明度', font=('華文行楷', 14),
bg='silver', fg='green', width=int(float(size[0] * 0.02)), height=int(float(0.0025 * size[1])),
command=commands.alpha)
button15.place(x=int(float(size[0] * 0.8)), y=int(float(0.8 * size[1])))
# 選單
menu = tkinter.Menu(root)
root['menu'] = menu
f1 = tkinter.Menu(menu, tearoff=False)
f1.add_command(label='重置注冊機', command=commands.reg)
f1.add_command(label='關于')
menu.add_cascade(label='選單', menu=f1)
root.mainloop()
效果如圖:


(右圖為圣誕樹界面效果)
另外,之前提供的重新注冊機沒有變動,
新添功能代碼:
處理圖片,生成鏡像的程式 ---image_cope.py
# 實作圖片鏡像對稱
from PIL import Image
i1_another = Image.open('tree/1.png')
i1_another.transpose(Image.FLIP_LEFT_RIGHT).save('tree/11.png')
i2_another = Image.open('tree/2.png')
i2_another.transpose(Image.FLIP_LEFT_RIGHT).save('tree/22.png')
i3_another = Image.open('tree/3.png')
i3_another.transpose(Image.FLIP_LEFT_RIGHT).save('tree/33.png')
實作人物移動的程式
(這兩個檔案的靈感源于此站的某位大佬,但是這是很久之前學習的,現在找不到了,文章很好,找到我會分享給大家)
---core.py
這個是控制人物移動
step = 2 # 每幀移動的像素
class Sprite:
"""
用于繪制精靈圖的工具類
"""
@staticmethod
def draw(dest, source, x, y, cell_x, cell_y, cell_w=128, cell_h=128):
"""
繪制精靈圖中,指定x,y的影像
:param dest: surface型別,要繪制到的目標surface
:param source: surface型別,來源surface
:param x: 繪制影像在dest中的坐標
:param y: 繪制影像在dest中的坐標
:param cell_x: 在精靈圖中的格子坐標
:param cell_y: 在精靈圖中的格子坐標
:param cell_w: 單個精靈的寬度
:param cell_h: 單個精靈的高度
:return:
"""
dest.blit(source, (x, y), (cell_x * cell_w, cell_y * cell_h, cell_w, cell_h))
class CharWalk:
"""
人物行走類 char是character的縮寫
"""
DIR_DOWN = 0
DIR_RIGHT = 1
DIR_UP = 2
DIR_LEFT = 3
DIR_DANCE = 4
DIR_FAINT = 5 # 暈倒
def __init__(self, hero_surf, dir, mx, my,step=2):
"""
:param hero_surf: 精靈圖的surface
:param dir: 角色方向
:param mx: 角色所在的小格子坐標
:param my: 角色所在的小格子坐標
"""
self.hero_surf = hero_surf
self.dir = dir
self.mx = mx
self.my = my
# 相對
self.is_walking = False # 角色是否正在移動
self.frame = 0 # 角色當前幀
# 角色下一步需要去的格子
self.next_mx = 0
self.next_my = 0
# 步長
self.step = step # 每幀移動的像素
def draw(self, screen_surf):
cell_x = int(self.frame)
cell_y = self.dir
Sprite.draw(screen_surf, self.hero_surf, self.mx, self.my, cell_x, cell_y)
def goto(self, x, y):
"""
:param x: 目標點
:param y: 目標點
"""
self.next_mx = x
self.next_my = y
# 設定人物面向
if self.next_mx > self.mx:
self.dir = CharWalk.DIR_RIGHT
elif self.next_mx < self.mx:
self.dir = CharWalk.DIR_LEFT
elif self.next_my > self.my:
self.dir = CharWalk.DIR_DOWN
elif self.next_my < self.my:
self.dir = CharWalk.DIR_UP
self.is_walking = True
def move(self):
if not self.is_walking:
return
dest_x = self.next_mx
dest_y = self.next_my
# 向目標位置靠近
if self.mx < dest_x:
self.mx += self.step
if self.mx >= dest_x:
self.mx = dest_x
self.goto(self.next_mx, self.next_my)
elif self.mx > dest_x:
self.mx -= self.step
if self.mx <= dest_x:
self.mx = dest_x
self.goto(self.next_mx, self.next_my)
elif self.my < dest_y:
self.my += self.step
if self.my >= dest_y:
self.my = dest_y
elif self.my > dest_y:
self.my -= self.step
if self.my <= dest_y:
self.my = dest_y
# 改變當前幀
self.frame = (self.frame + 0.1) % 4
"""此種方法控制幀數更新速度"""
# 到達了目標點
if self.mx == dest_x and self.my == dest_y:
self.frame = 0
self.is_walking = False
---move_ren.py
這個是生成人物實列
from core import CharWalk
import pygame
class Game:
def __init__(self, screen, start_x, start_y, end_x, end_y, heroes, fps=60):
self.screen = screen
self.fps = fps
self.heroes = pygame.image.load(heroes).convert_alpha()
self.start_x = start_x
self.start_y = start_y
self.end_x = end_x
self.end_y = end_y
self.__init_game()
def __init_game(self):
self.heroes = pygame.transform.scale(self.heroes, (128 * 4, 128 * 4))
# self.heroes = pygame.transform.scale(self.heroes, (16 * 4, 48*4))
self.role = CharWalk(self.heroes, CharWalk.DIR_DOWN, self.start_x, self.start_y) # 讀取出發坐標
self.role.goto(self.end_x, self.end_y) # 讀取到達坐標
再來看圣誕樹與圣誕老人---drawtree.py
代碼很像最原始的下雪程式,沒錯,原理就是把會動的人添加在原來效果上,背景重新渲 染,功能保留了原來的風格,
import sys
import pygame
import random
import os.path as path
from move_ren import Game
def main():
# 初始化pygame
pygame.init()
width = 400
height = 365
SIZE = width * 2, height * 2
screen = pygame.display.set_mode(SIZE, pygame.NOFRAME)
# 根據背景圖片的大小,設定螢屏長寬
image = pygame.image.load('tree/sds.jpg')
image.set_alpha(150)
i1 = pygame.image.load('tree/1.png')
i1.set_alpha(200)
# i1的鏡像翻轉
i11 = pygame.image.load('tree/11.png')
i11.set_alpha(180)
i2 = pygame.image.load('tree/2.png')
i2.set_alpha(200)
# i1的鏡像翻轉
i22 = pygame.image.load('tree/22.png')
i22.set_alpha(180)
i3 = pygame.image.load('tree/3.png')
i3.set_alpha(200)
# i1的鏡像翻轉
i33 = pygame.image.load('tree/33.png')
i33.set_alpha(180)
# 雪花串列
snow_list = []
# 初始化雪花:(x坐標, y坐標), x軸速度, y軸速度
for i in range(200):
x = random.randrange(0, SIZE[0])
y = random.randrange(0, SIZE[1])
# 讓雪有兩種下落趨勢--左下或者右下
speed_x = random.randint(-1, 1)
speed_y = random.randint(1, 4)
snow_list.append([x, y, speed_x, speed_y])
# 重繪幀率,控制速度
clock = pygame.time.Clock()
# 背景音樂
pygame.mixer.init()
music = pygame.mixer.Sound('tree/ddd.mp3')
music.play(-1)
# 動態人物
ren = Game(screen, start_x=750, start_y=90, end_x=80, end_y=320, heroes='tree/6.png')
ren1 = pygame.image.load('tree/7.png')
# 引線--準時跳出圖片
x1 = -50
y1 = 300
# 游戲主回圈
while True:
screen.fill((0, 0, 0))
# 重影效果
# screen.blit(pygame.transform.scale(image2, SIZE), (-width/2, 0))
screen.blit(pygame.transform.scale(image, SIZE), (0, 0))
screen.blit(image, (0, 0))
screen.blit(i1, (430, 190))
screen.blit(i11, (220, 350))
screen.blit(i1, (500, 455))
screen.blit(i2, (500, 650))
screen.blit(i33, (610, 640))
screen.blit(i22, [100, 650])
screen.blit(i33, (350, 655))
screen.blit(i3, (180, 620))
# screen.blit()
# 事件檢測
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
# 按q鍵退出
if event.key == event.key == pygame.K_q:
sys.exit()
# 按s鍵截圖
if event.key == pygame.K_s:
list_file = []
list_ooo = list(range(1000))
for num_in in list_ooo:
if path.isfile('picture/picture' + str(num_in) + '.jpg'):
continue
else:
list_file.append(num_in)
pygame.image.save(screen, 'picture/picture' + str(list_file[0]) + '.jpg')
# 圣誕老人奔跑
ren.role.move()
ren.role.draw(screen)
pygame.display.update()
# 發放禮物
if x1 < 60:
x1 += 0.23
else:
screen.blit(ren1, (x1, y1))
# 隨機下雪
for i in range(len(snow_list)):
# a = (192, 192, 192)
a = (255, 255, 255)
pygame.draw.circle(
# 顯示
screen,
# 顏色
[int(f) for f in a],
# 降落點
snow_list[i][:2],
# 雪花半徑
snow_list[i][3],
# 充實雪花顆粒
0
)
# 移動雪花位置(下一次回圈起效)
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][1] = random.randrange(-50, -10)
snow_list[i][0] = random.randrange(0, SIZE[0])
# 重繪螢屏
pygame.display.flip()
clock.tick(20)
if __name__ == '__main__':
main()
其他的,如操作說明,bug表現等等等我就不多說了,全部放在了目錄下的author.txt檔案里了,都附在下面的下載鏈接里了,
歐克,到這里詳解就差不多了(當然了,這里我沒有提供相關的素材),如果你在你的環境中運行不成功,你可以通過我在下方提供的鏈接進行下載,如需轉載,請備上轉載地址哦,如果你還有什么疑問,請在下方留言哦,請多多指教,
最后, 在這里跟大家分享一下我個人的交流群,歡迎各位各階段友友們進群,一起討論交流,群里還會不定時進行資源分享和抽獎哦,以下為QQ群號:71956064
Github鏈接:
https://github.com/PythonnotJava/Snow-1.1
完整安裝包阿里云盤下載(約70M):
https://www.aliyundrive.com/s/ZvvJGewJHKJ
若鏈接失效請留言或者私信, 我們下期再會,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/398694.html
標籤:python
