欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

用Python如何實(shí)現(xiàn)的代碼雨

不懂用Python如何實(shí)現(xiàn)的代碼雨?其實(shí)想解決這個(gè)問(wèn)題也不難,下面讓小編帶著大家一起學(xué)習(xí)怎么去解決,希望大家閱讀完這篇文章后大所收獲。

長(zhǎng)興網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,長(zhǎng)興網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為長(zhǎng)興數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的長(zhǎng)興做網(wǎng)站的公司定做!

1. 數(shù)字

首先,我們來(lái)實(shí)現(xiàn)數(shù)字雨,我們需要?jiǎng)?chuàng)建一個(gè)窗口來(lái)顯示內(nèi)容,窗口的創(chuàng)建使用 pygame 庫(kù),代碼實(shí)現(xiàn)如下:

FONT_PX = 15
pygame.init()
winSur = pygame.display.set_mode((500, 600))
font = pygame.font.SysFont('fangsong', 20)
bg_suface = pygame.Surface((500, 600), flags=pygame.SRCALPHA)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0, 0, 0, 13))
winSur.fill((0, 0, 0))
# 數(shù)字
texts = [font.render(str(i), True, (0, 255, 0)) for i in range(10)]
colums = int(500 / FONT_PX)
drops = [0 for i in range(colums)]
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    pygame.time.delay(33)
    winSur.blit(bg_suface, (0, 0))
    for i in range(len(drops)):
        text = random.choice(texts)
        winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))
        drops[i] += 1
        if drops[i] * 10 > 600 or random.random() > 0.95:
            drops[i] = 0
    pygame.display.flip()

實(shí)現(xiàn)效果如下:

用Python如何實(shí)現(xiàn)的代碼雨

2. 字母

接著,我們?cè)賮?lái)實(shí)現(xiàn)字母雨,實(shí)現(xiàn)方式基本就是將上面實(shí)現(xiàn)數(shù)字雨的數(shù)字換成字母,代碼實(shí)現(xiàn)如下:

PANEL_width = 400
PANEL_highly = 500
FONT_PX = 15
pygame.init()
# 創(chuàng)建一個(gè)窗口
winSur = pygame.display.set_mode((PANEL_width, PANEL_highly))
font = pygame.font.SysFont('123.ttf', 22)
bg_suface = pygame.Surface((PANEL_width, PANEL_highly), flags=pygame.SRCALPHA)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0, 0, 0, 28))
winSur.fill((0, 0, 0))
letter = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c',
          'v', 'b', 'n', 'm']
texts = [
    font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)
]
# 按窗口的寬度來(lái)計(jì)算可以在畫(huà)板上放幾列坐標(biāo)并生成一個(gè)列表
column = int(PANEL_width / FONT_PX)
drops = [0 for i in range(column)]
while True:
    # 從隊(duì)列中獲取事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        elif event.type == pygame.KEYDOWN:
            chang = pygame.key.get_pressed()
            if (chang[32]):
                exit()
    # 暫停給定的毫秒數(shù)
    pygame.time.delay(30)
    # 重新編輯圖像
    winSur.blit(bg_suface, (0, 0))
    for i in range(len(drops)):
        text = random.choice(texts)
        # 重新編輯每個(gè)坐標(biāo)點(diǎn)的圖像
        winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))
        drops[i] += 1
        if drops[i] * 10 > PANEL_highly or random.random() > 0.95:
            drops[i] = 0
    pygame.display.flip()

實(shí)現(xiàn)效果如下:

用Python如何實(shí)現(xiàn)的代碼雨

3. 圖片

最后,我們使用圖片來(lái)實(shí)現(xiàn)這一效果,圖片我們就使用雨滴吧,這里我們使用 tkinter 創(chuàng)建窗口,代碼實(shí)現(xiàn)如下:

# 初始雨滴縱坐標(biāo)
INIT_HEIGHT = 10
# 雨滴創(chuàng)建
def rainmake(canvas, imagefile):
    rainlist = []
    for i in range(5):
        # 根據(jù)圖片,創(chuàng)建一排雨滴
        rainlist.append(canvas.create_image(100 + 80 * i, INIT_HEIGHT, anchor=NE, image=imagefile))
    return rainlist
# 雨滴下落
def raindown(tk, canvas, imagefile, sec):
    # 線(xiàn)程間等待
    time.sleep(sec)
    rainlist = rainmake(canvas, imagefile)
    # 每個(gè)雨滴的縱坐標(biāo)值
    height = [INIT_HEIGHT] * 10
    while True:
        # 每次移動(dòng)前稍等一會(huì)
        time.sleep(0.2)
        # 5 個(gè)雨滴一起移動(dòng)
        for i in range(5):
            # 如果雨滴字到底了,則不繼續(xù)移動(dòng)
            if not height[i] == 0:
                # 設(shè)置下落步調(diào)
                rnd = random.randint(5, 50)
                canvas.move(rainlist[i], 0, rnd)
                height[i] = height[i] + rnd
                tk.update()
        for i,h in enumerate(height):
            if h > 400:
                # 當(dāng)雨滴字走到最下方,則刪除
                canvas.delete(rainlist[i])
                tk.update()
                # 清空該雨滴的 height
                height[i] = 0
                print(i,h,height)
        # 全到底,則跳出循環(huán)
        if height == [0] * 5:
            print('break:',threading.current_thread().name)
            break
def lookloop(tk, canvas, thread):
    aliveflg = False
    while True:
        # 5s 檢測(cè)一次
        time.sleep(5)
        for th in thread:
            if th.is_alive():
                aliveflg = True
            else:
                aliveflg = False
        if aliveflg == False:
            break
    canvas.create_text(100 , 200, text='雨停了...', fill='red')
    canvas.pack()
    time.sleep(5)
    tk.destroy()

實(shí)現(xiàn)效果如下:

用Python如何實(shí)現(xiàn)的代碼雨

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享用Python如何實(shí)現(xiàn)的代碼雨內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問(wèn)題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!

當(dāng)前題目:用Python如何實(shí)現(xiàn)的代碼雨
網(wǎng)頁(yè)鏈接:http://chinadenli.net/article24/pippce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、云服務(wù)器、、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司

網(wǎng)站設(shè)計(jì)公司知識(shí)