Pygame制作唯美雪花特效

# 导入需要的库包
import pygame
import random
import math
import sys
import time
from pygame.locals import *
from pygame import mixer

# 初始化
mixer.init()
pygame.init()
snow = []
SIZE = (729, 511)
RGB = (255, 255, 255)
PI = math.pi
sin = math.sin(PI/8)
cos = math.cos(PI/8)
next = ([1.6, 0], [0, -1.6], [1.6 ** 0.5, -1.6 ** 0.5], [-1.6 **0.5, -1.6 ** 0.5], [sin, -cos],[cos, -sin],[-sin, -cos], [-cos, -sin])
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Snow Picture Video")

# 模拟随机雪花飘落场景
for i in range(400):
    x = random.randrange(0, SIZE[0])
    y = random.randrange(0, SIZE[1])
    speedx = random.randint(-100, 101)/100
    speedy = random.randint(50, 380)/100
    snow.append([x, y, speedx, speedy])

# 定义主体函数
def Play(image):
    # 导入背景图片
    background = pygame.image.load(image)
    for k in range(3*10**2):
        # 点击关闭按按钮结束程序运行
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
        # 在缓冲区中绘制图像
        screen.blit(background, (0, 0))
        for i in range(len(snow)):
            for j in range(8):
                r = snow[i][3]
                beg = (snow[i][0] + r*next[j][0], snow[i][1] + r*next[j][1])
                end = (snow[i][0] + r*next[j][0], snow[i][1] + r*next[j][1])
                pygame.draw.line(screen, RGB, beg, end, 1)
                snow[i][0] += snow[i][2]/1.5
                snow[i][1] += snow[i][3] / 1.5
                if snow[i][1] > SIZE[1]:
                    snow[i][0] = random.randrange(0, SIZE[0])
                    snow[i][1] = random.randrange(-50, -10)
                pygame.display.flip()
        return False

# 加载音乐
mixer.music.load('F:/music.mp3')
mixer.music.play()

# Pygame游戏开发最小框架
while True:
    # 遍历要播放的图片
    for i in range(3):
        Play('F:/' + str(i) + '.PNG')

mixer.music.stop()
pygame.quit()

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荣仔!最靓的仔!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值