溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中怎么利用pygame制作動畫跑馬燈

發(fā)布時間:2021-07-10 13:49:55 來源:億速云 閱讀:273 作者:Leah 欄目:大數(shù)據(jù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Python中怎么利用pygame制作動畫跑馬燈,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

import pygame
from random import randint, choice

screen_length = 700
screen_width = 500
# 模擬彩帶飄落的類,掉落的詞作為彩帶
class Word_drop(pygame.sprite.Sprite):

    # 設(shè)置屬性:包括字體、下落速度、彩帶來源、彩帶框的屬性
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.SysFont(name='幼圓', size=10, bold=True, italic=True)
        self.speed = randint(15, 30)
        self.word = self.getWord()
        self.image = self.font.render(self.word, True,
                                      (randint(0, 255), randint(0, 255), randint(0, 255)))
        self.image = pygame.transform.rotate(self.image, randint(87, 93))
        self.rect = self.image.get_rect()
        self.rect.topleft = (randint(0, screen_length), -20)

    # 獲取掉落的詞
    def getWord(self):
        length = randint(1, 8)
        word = ''
        for i in range(length):
            word += choice('qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM')
        return word

    # 設(shè)置彩帶更新的條件
    def update(self, *args):
        self.rect = self.rect.move(0, self.speed)
        if self.rect.top > screen_length:
            self.kill()

# 實(shí)現(xiàn)"跑馬燈"效果的函數(shù)
def word_translate(jx, ztw1, ztw2, screen_length, text):
    max_ztw = max(ztw1, ztw2)
    jx.x -= 5
    if jx.x < 0 - max_ztw:
        jx.x = (screen_length + 10)
    screen.blit(text, [jx.x, jx.y])

if __name__ == '__main__':

    # 初始化工作
    pygame.init()
    pygame.font.init()

    # 渲染字體,兩行字
    a = pygame.font.SysFont(name='幼圓', size=50, bold=True, italic=True)

    word1 = ">

上述就是小編為大家分享的Python中怎么利用pygame制作動畫跑馬燈了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI