使用Python的pygame庫實現(xiàn)下雪效果的示例代碼
使用Python的pygame庫實現(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()下面給出改進版
效果圖:

使用一張背景圖片(我這里文件名: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庫實現(xiàn)下雪效果的示例代碼的詳細內(nèi)容,更多關(guān)于Python pygame下雪效果的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python中encode和encoding的區(qū)別小結(jié)
encode和encoding在Python中雖然都與字符編碼相關(guān),但它們的角色和用途是不同的,本文主要介紹了Python中encode和encoding的區(qū)別小結(jié),具有一定的參考價值,感興趣的可以了解一下2024-03-03
Python調(diào)用Windows API函數(shù)編寫錄音機和音樂播放器功能
這篇文章主要介紹了Python調(diào)用Windows API函數(shù)編寫錄音機和音樂播放器功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
將tensorflow.Variable中的某些元素取出組成一個新的矩陣示例
今天小編就為大家分享一篇將tensorflow.Variable中的某些元素取出組成一個新的矩陣示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
python+django+mysql開發(fā)實戰(zhàn)(附demo)
本文主要介紹了python+django+mysql開發(fā)實戰(zhàn)(附demo),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01

