#!usr/bin/env python
# coding=utf-8
import os
import pygame
import sys
import random
import math
# FPS(每秒帧数)控制器
fps = pygame.time.Clock()
# 界面大小
screen_size = (1280, 800)
# pygame初始化
pygame.init()
# 设置颜色
white = (255, 255, 255)
black = (0, 0, 0)
# 创建一个窗口
screen = pygame.display.set_mode(screen_size, 0, 32)
pygame.display.set_caption('New Year fireworks')
# 背景填充
screen.fill(black)
def flower_bloom(pos_x, pos_y, color):
"""
烟花绽放
:param pos_x:烟花绽放的横坐标
:param pos_y:烟花绽放的纵坐标
:param color:烟花颜色
:return:
"""
bloom_length = 1 # 随机到的烟花长度
bloom_num = random.randint(50, 80) # 随机到的烟花绽放数目
flower_x = [pos_x for i in range(bloom_num + 2)] # 起始x点
flower_y = [pos_y for i in range(bloom_num + 2)] # 起始y点
end_x = [pos_x for i in range(bloom_num + 2)] # 结束值x点
end_y = [pos_y for i in range(bloom_num + 2)] # 结束值y点
t = 1
while True:
screen.fill(black)
# 分别朝n个方向射出烟花
for i in range(bloom_num + 2):
# 求出烟花绽放的最终值
end_x[i] = pos_x + bloom_length * t * math.cos(math.radians(i * (360 // bloom_num)))
end_y[i] = pos_y - bloom_length * t * math.sin(math.radians(i * (360 // bloom_num)))
pygame.draw.aaline(screen, color, (flower_x[i], flower_y[i]), (end_x[i], end_y[i]))
flower_x[i] = end_x[i]
flower_y[i] = end_y[i]
t += 1
pygame.display.update()
fps.tick(200)
if t > 300:
break
def Firing(pos_y, high, stop_location, color):
"""
烟花发射
:param pos_y: 发射点
:param high: 烟花长度
:param color: 烟花颜色
:param stop_location: 烟花停止的位置
:return:
"""
pos_x = 780
while True:
screen.fill(black)
pygame.draw.rect(screen, color, [pos_y, pos_x, 1, high], 0)
pos_x -= 21
pygame.display.update()
fps.tick(10)
if pos_x < stop_location:
flower_bloom(pos_y, stop_location, color)
break
while True:
screen.fill(black)
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
for event in pygame.event.get():
# 按下关闭按钮,退出程序
if event.type == pygame.QUIT:
sys.exit()
# if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_UP:
# Firing()
Firing(random.randint(30, 1000), random.randint(1, 100), random.randint(50, 360), color)
pygame.display.update()
# fps.tick(1)