使用Python的pygame庫實(shí)現(xiàn)下雪效果的示例代碼
使用Python的pygame庫實(shí)現(xiàn)下雪的效果
關(guān)于Python中pygame游戲模塊的安裝使用可見 Python中pygame游戲模塊的用法詳解_python_腳本之家 (jb51.net)
先給出效果圖:
源碼如下:
import pygame import random # 初始化pygame pygame.init() # 設(shè)置屏幕尺寸 width, height = 800, 600 screen = pygame.display.set_mode((width, height)) # 設(shè)置雪花屬性 snowflakes = [] for i in range(50): x = random.randrange(0, width) y = random.randrange(0, height) speed = random.uniform(1, 3) size = random.randint(3, 6) # 雪花大小參數(shù) snowflakes.append([x, y, speed, size]) # 循環(huán)直到用戶關(guān)閉窗口 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 填充屏幕顏色 screen.fill((200, 200, 200)) # (0, 0, 0)黑色;(200, 200, 200)陰天 # 繪制雪花 for flake in snowflakes: pygame.draw.circle(screen, (255, 255, 255), (int(flake[0]), int(flake[1])), flake[3]) # 使用大小參數(shù)繪制雪花 flake[1] += flake[2] # 移動雪花 if flake[1] > height: flake[1] = random.randrange(-50, -10) flake[0] = random.randrange(0, width) # 更新屏幕 pygame.display.flip() # 控制幀率 pygame.time.Clock().tick(30) # 退出pygame pygame.quit()
下面給出改進(jìn)版
效果圖:
使用一張背景圖片(我這里文件名:snow_background.jpg),和代碼文件放在同一目錄下
源碼如下:
import pygame import random # 初始化pygame pygame.init() # 設(shè)置屏幕尺寸 width, height = 800, 600 screen = pygame.display.set_mode((width, height)) # 加載背景圖片 background = pygame.image.load('snow_background.jpg') background = pygame.transform.scale(background, (width, height)) # 設(shè)置雪花屬性 snowflakes = [] for i in range(50): x = random.randrange(0, width) y = random.randrange(0, height) speed = random.uniform(1, 3) size = random.randint(3, 6) # 雪花大小參數(shù) snowflakes.append([x, y, speed, size]) # 循環(huán)直到用戶關(guān)閉窗口 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 填充屏幕顏色 #screen.fill((200, 200, 200)) # (0, 0, 0)黑色;(200, 200, 200)陰天 # 繪制背景圖片 screen.blit(background, (0, 0)) # 移動雪花并重新繪制 for flake in snowflakes: pygame.draw.circle(screen, (255, 255, 255), (int(flake[0]), int(flake[1])), flake[3]) # 使用大小參數(shù)繪制雪花 flake[1] += flake[2] # 移動雪花 if flake[1] > height: flake[1] = random.randrange(-50, -10) flake[0] = random.randrange(0, width) # 更新屏幕 pygame.display.flip() # 控制幀率 pygame.time.Clock().tick(30) # 退出pygame pygame.quit()
附:RGB 顏色表 https://www.codeeeee.com/color/rgb.html
以上就是使用Python的pygame庫實(shí)現(xiàn)下雪效果的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python pygame下雪效果的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用 tf.nn.dynamic_rnn 展開時(shí)間維度方式
今天小編就為大家分享一篇使用 tf.nn.dynamic_rnn 展開時(shí)間維度方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python實(shí)現(xiàn)批量提取PPT中的文字
這篇文章主要為大家詳細(xì)介紹了如何使用Python中的pptx和docx庫來將PPT中的文字提取到Word中,文中的示例代碼講解詳細(xì),有需要的可以參考下2024-03-03django Model層常用驗(yàn)證器及自定義驗(yàn)證器詳解
這篇文章主要介紹了django Model層常用驗(yàn)證器及自定義驗(yàn)證器詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python中encode和encoding的區(qū)別小結(jié)
encode和encoding在Python中雖然都與字符編碼相關(guān),但它們的角色和用途是不同的,本文主要介紹了Python中encode和encoding的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03Python調(diào)用Windows API函數(shù)編寫錄音機(jī)和音樂播放器功能
這篇文章主要介紹了Python調(diào)用Windows API函數(shù)編寫錄音機(jī)和音樂播放器功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01將tensorflow.Variable中的某些元素取出組成一個(gè)新的矩陣示例
今天小編就為大家分享一篇將tensorflow.Variable中的某些元素取出組成一個(gè)新的矩陣示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python實(shí)現(xiàn)zip分卷壓縮的詳細(xì)方法
WinHex 開始16進(jìn)制一個(gè)一個(gè)文件對比 WinRar 創(chuàng)建的分卷壓縮和單個(gè) zip 文件的差異,這篇文章主要介紹了python實(shí)現(xiàn)zip分卷壓縮的詳細(xì)方法,需要的朋友可以參考下2024-02-02python+django+mysql開發(fā)實(shí)戰(zhàn)(附demo)
本文主要介紹了python+django+mysql開發(fā)實(shí)戰(zhàn)(附demo),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01