开心六月综合激情婷婷|欧美精品成人动漫二区|国产中文字幕综合色|亚洲人在线成视频

    1. 
      
        <b id="zqfy3"><legend id="zqfy3"><fieldset id="zqfy3"></fieldset></legend></b>
          <ul id="zqfy3"></ul>
          <blockquote id="zqfy3"><strong id="zqfy3"><dfn id="zqfy3"></dfn></strong></blockquote>
          <blockquote id="zqfy3"><legend id="zqfy3"></legend></blockquote>
          打開(kāi)APP
          userphoto
          未登錄

          開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

          開(kāi)通VIP
          python下pygame的煙花放送及代碼解析

          煙花

          第1步:模塊導(dǎo)入

          import pygame, math, time, random, osfrom sys import exit

          第2步:定義相關(guān)初始

          win(窗口的w=寬,h=高)

          WIN_W = 2200WIN_H = 1300

          定義時(shí)間、顯示、頻率

          t1 = 0.18 #時(shí)間流速show_n = 0show_frequency = 0.0015 #煙花綻放頻率,數(shù)值越大頻率越高

          煙花顏色列表,下面隨機(jī)抽出

          color_list = [                [255, 50, 50],                [50, 255, 50],                [50, 50, 255],                [255, 255, 50],                [255, 50, 255],                [50, 255, 255],                [255, 255, 255]             ]

          初始化pygame和音樂(lè)mixer

          pygame.init()pygame.mixer.init()

          創(chuàng)建一個(gè)窗口,pygame.RESIZABLE窗口大小可調(diào)節(jié)和標(biāo)題

          screen = pygame.display.set_mode((WIN_W, WIN_H),pygame.RESIZABLE, 32)pygame.display.set_caption('五彩煙花大放送')

          背景音樂(lè),可自定義

          sound_wav = pygame.mixer.music.load('123.mp3')pygame.mixer.music.play()

          Fireworks=煙花,定義主類(lèi)

          class Fireworks():    is_show = False    x, y = 0, 0    vy = 0    p_list = []    color = [0, 0, 0]    v = 0    def __init__(self, x, y, vy, n=300, color=[0, 255, 0], v=10):        self.x = x        self.y = y        self.vy = vy        self.color = color        self.v = v        for i in range(n):            self.p_list.append([random.random() * 2 * math.pi, 0, v * math.pow(random.random(), 1 / 3)])    def again(self):        self.is_show = True        self.x = random.randint(WIN_W // 2 - 350, WIN_W // 2 + 350)        self.y = random.randint(int(WIN_H / 2), int(WIN_H * 3 / 5))        self.vy = -40 * (random.random() * 0.4 + 0.8) - self.vy * 0.2        self.color = color_list[random.randint(0, len(color_list) - 1)].copy()        n = len(self.p_list)        self.p_list = []        for i in range(n):            self.p_list.append([random.random() * 2 * math.pi, 0, self.v * math.pow(random.random(), 1 / 3)])    def run(self):        global show_n        for p in self.p_list:            p[1] = p[1] + (random.random() * 0.6 + 0.7) * p[2]            p[2] = p[2] * 0.97            if p[2] < 1.2:                self.color[0] *= 0.9999                self.color[1] *= 0.9999                self.color[2] *= 0.9999            if max(self.color) < 10 or self.y>WIN_H+p[1]:                show_n -= 1                self.is_show = False                break        self.vy += 10 * t1        self.y += self.vy * t1

          煙花列表

          fk_list = []fk_list.append(Fireworks(300, 300, -20, n=100, color=[0, 255, 0], v=10))fk_list.append(Fireworks(300, 300, -20, n=200, color=[0, 0, 255], v=11))fk_list.append(Fireworks(300, 300, -20, n=200, color=[0, 0, 255], v=12))fk_list.append(Fireworks(300, 300, -20, n=500, color=[0, 0, 255], v=12))fk_list.append(Fireworks(300, 300, -20, n=600, color=[0, 0, 255], v=13))fk_list.append(Fireworks(300, 300, -20, n=700, color=[255, 0, 0], v=15))fk_list.append(Fireworks(300, 300, -20, n=800, color=[255, 255, 0], v=18))clock = pygame.time.Clock()

          煙花放送的主循環(huán):

          # 游戲主循環(huán)while True:    if not pygame.mixer.music.get_busy():        pygame.mixer.music.play()    for event in pygame.event.get():        #pygame這個(gè)不能少,否則退出還在循環(huán)        if event.type == pygame.QUIT:            exit()    screen.fill((0, 0, 0))    # 放煙花    for i, fk in enumerate(fk_list):        if not fk.is_show:            fk.is_show = False            if random.random() < show_frequency * (len(fk_list) - show_n):                show_n += 1                fk.again()            continue        fk.run()        for p in fk.p_list:            x, y = fk.x + p[1] * math.cos(p[0]), fk.y + p[1] * math.sin(p[0])            if random.random() < 0.055:                screen.set_at((int(x), int(y)),(255,255,255))            else:                screen.set_at((int(x), int(y)), (int(fk.color[0]), int(fk.color[1]), int(fk.color[2])))    pygame.display.update()    time_passed = clock.tick(50)

          效果圖

          本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
          打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
          猜你喜歡
          類(lèi)似文章
          【python】明天情人節(jié),給大家?guī)?lái)一些編程表白小程序,給大家助助興呀~
          做硬核老爸,我用 Python
          Python|520表白神器
          python實(shí)現(xiàn)小區(qū)車(chē)牌識(shí)別計(jì)費(fèi)程序
          就是玩兒:python輕松實(shí)現(xiàn)代碼雨效果!
          Flash實(shí)例教程:漫天流星耀夜空
          更多類(lèi)似文章 >>
          生活服務(wù)
          分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
          綁定賬號(hào)成功
          后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
          如果VIP功能使用有故障,
          可點(diǎn)擊這里聯(lián)系客服!

          聯(lián)系客服