python和pygame實(shí)現(xiàn)煙花特效
新年來(lái)臨之際,來(lái)一個(gè)歡慶新年煙花祝賀,需要安裝使用第三方庫(kù)pygame,關(guān)于Python中pygame游戲模塊的安裝使用可見 https://blog.csdn.net/cnds123/article/details/119514520
效果圖及源碼
先看效果圖:
源碼如下:
import pygame
import random
import math
# 初始化pygame
pygame.init()
# 設(shè)置窗口大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
# 定義顏色
black = (0, 0, 0)
red = (255, 0, 0)
# 定義煙花粒子
class Particle:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
self.radius = random.randint(2, 4)
self.angle = random.uniform(0, 2 * math.pi)
self.speed = random.uniform(1, 3)
self.gravity = 0.1
def move(self):
self.x += math.cos(self.angle) * self.speed
self.y += math.sin(self.angle) * self.speed + self.gravity
self.radius -= 0.1 # 粒子逐漸變小
def draw(self):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), int(self.radius))
# 定義煙花
class Firework:
def __init__(self, x, y):
self.x = x
self.y = y
self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
self.particles = []
self.exploded = False
self.explode_height = random.randint(100, 400) # 設(shè)置爆炸高度
self.speed = random.randint(5, 10) # 設(shè)置上升速度
self.angle = math.pi / 2 # 設(shè)置上升角度為垂直向上
def launch(self):
if not self.exploded:
self.y -= self.speed * math.sin(self.angle)
if self.y <= self.explode_height: # 到達(dá)設(shè)定高度后爆炸
self.explode()
self.exploded = True
def explode(self):
for _ in range(100): # 爆炸產(chǎn)生的粒子數(shù)量
self.particles.append(Particle(self.x, self.y, self.color))
def draw(self):
if not self.exploded:
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 5)
else:
for particle in self.particles:
particle.move()
particle.draw()
#顯示文字
#font = pygame.font.Font(None, 36) # 設(shè)置字體和大小
font = pygame.font.Font("C:\\Windows\\Fonts\\simsun.ttc", 36)
text = font.render("龍年快樂(lè)", True, red) # 渲染文本
text_rect = text.get_rect(center=(width // 2, height // 2)) # 獲取文本的矩形區(qū)域
# 主循環(huán)
fireworks = []
clock = pygame.time.Clock()
running = True
while running:
clock.tick(30) # 控制幀率
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(black)
# 繪制文本
screen.blit(text, text_rect)
# 發(fā)射煙花
if random.randint(1, 20) == 1: # 控制煙花發(fā)射頻率
fireworks.append(Firework(random.randint(0, width), height))
# 更新煙花并繪制
for firework in fireworks[:]:
firework.launch()
firework.draw()
if firework.exploded and all(p.radius <= 0 for p in firework.particles):
fireworks.remove(firework)
pygame.display.flip()
pygame.quit()
pygame在屏幕上顯示字體的方法說(shuō)明
使用pygame.font.Font
函數(shù)來(lái)設(shè)置字體和大小,然后使用font.render
函數(shù)將文本渲染為圖像。最后,使用screen.blit
函數(shù)將渲染好的文本圖像繪制到屏幕上。
pygame.font.Font(None, 字體大小)使用系統(tǒng)默認(rèn)字體,可能不支持漢字。None代表系統(tǒng)默認(rèn)字體,如pygame.font.Font(None, 36),可能不支持漢字。怎么辦?使用pygame.font. Font(“含路徑的字體名”,字體大小),指定支持漢字的字體,如:pygame.font.Font("C:\\Windows\\Fonts\\simsun.ttc", 36),simsun.ttc是宋體,字體的路徑和名稱。在windows中如何確定字體的路徑和名稱呢?參見下圖
改進(jìn):添加背景音樂(lè),增添歡樂(lè)的氣氛
在“# 主循環(huán)”部分前,添加如下代碼
# 載入背景音樂(lè)
pygame.mixer.music.load("踩著開心的節(jié)奏 - 橙光音樂(lè).mp3")
pygame.mixer.music.set_volume(0.5) ?# 設(shè)置音量
pygame.mixer.music.play(-1) ?# ?播放背景音樂(lè),-1表示循環(huán)播放
其中,pygame.mixer.music.load函數(shù)加載背景音樂(lè)文件(例如名為"踩著開心的節(jié)奏 - 橙光音樂(lè).mp3"文件,并將其放在與代碼文件相同的目錄下),然后使用pygame.mixer.music.set_volume函數(shù)設(shè)置音量大?。ǚ秶鸀?.0-1.0)。最后,使用pygame.mixer.music.play函數(shù)播放背景音樂(lè),參數(shù)-1表示循環(huán)播放。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-766261.html
請(qǐng)你使用自己背景音樂(lè)文件替換"踩著開心的節(jié)奏 - 橙光音樂(lè).mp3",只要保證正確命名文件名和路徑即可。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-766261.html
到了這里,關(guān)于python和pygame實(shí)現(xiàn)煙花特效的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!