利用python繪制動態(tài)圣誕下雪圖
運(yùn)行圖片
代碼
import pygame import random # 初始化Pygame pygame.init() # 創(chuàng)建窗口 width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption('Christmas Tree') # 定義顏色 GREEN = (34, 139, 34) RED = (255, 0, 0) WHITE = (255, 255, 255) # 繪制圣誕樹 def draw_christmas_tree(): triangle_height = 300 triangle_width = 200 # 繪制樹干 pygame.draw.rect(screen, pygame.Color('brown'), (width // 2 - 25, height - 200, 50, 200)) # 繪制樹冠 for i in range(triangle_height): pygame.draw.polygon(screen, GREEN, [(width // 2 - triangle_width // 2 - i, height - 50 - i), (width // 2 + triangle_width // 2 + i, height - 50 - i), (width // 2, height - 50 - i - triangle_height)]) # 隨機(jī)生成雪花 def generate_snowflakes(): snowflakes = [] for _ in range(100): x = random.randint(0, width) y = random.randint(0, height) size = random.randint(1, 5) snowflakes.append((x, y, size)) return snowflakes # 繪制雪花 def draw_snowflakes(snowflakes): for flake in snowflakes: pygame.draw.circle(screen, WHITE, (flake[0], flake[1]), flake[2]) # 繪制圣誕老人 def draw_santa(): santa_image = pygame.Surface((120, 120), pygame.SRCALPHA) # 頭部和身體 pygame.draw.circle(santa_image, RED, (60, 60), 50) pygame.draw.polygon(santa_image, RED, [(0, 60), (120, 60), (60, 120)]) # 眼睛和嘴巴 pygame.draw.circle(santa_image, WHITE, (45, 45), 10) pygame.draw.circle(santa_image, WHITE, (75, 45), 10) pygame.draw.arc(santa_image, WHITE, (30, 45, 60, 60), 3.14 / 2, 3 * 3.14 / 2, 3) # 鼻子 pygame.draw.polygon(santa_image, pygame.Color('orange'), [(60, 50), (70, 70), (50, 70)]) # 胡須 pygame.draw.line(santa_image, WHITE, (55, 85), (20, 90), 2) pygame.draw.line(santa_image, WHITE, (65, 85), (100, 90), 2) # 帽子 pygame.draw.polygon(santa_image, RED, [(30, 0), (90, 0), (60, 60)]) screen.blit(santa_image, (width // 2 - 60, height - 200 - 120)) # 游戲主循環(huán) running = True clock = pygame.time.Clock() snowflakes = generate_snowflakes() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((0, 0, 0)) # 清空屏幕 draw_christmas_tree() draw_santa() draw_snowflakes(snowflakes) # 雪花下落 for i in range(len(snowflakes)): x, y, size = snowflakes[i] snowflakes[i] = (x, y + size, size) if y + size > height: snowflakes[i] = (random.randint(0, width), 0, size) pygame.display.flip() # 刷新屏幕 clock.tick(30) # 控制幀率 # 退出游戲 pygame.quit()
到此這篇關(guān)于利用python繪制動態(tài)圣誕下雪圖的文章就介紹到這了,更多相關(guān)python動態(tài)圣誕下雪內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
玩數(shù)據(jù)必備Python庫之numpy使用詳解
NumPy提供了許多高級的數(shù)值編程工具,如矩陣數(shù)據(jù)類型、矢量處理,以及精密的運(yùn)算庫,下面這篇文章主要給大家介紹了關(guān)于玩數(shù)據(jù)必備Python庫之numpy使用的相關(guān)資料,需要的朋友可以參考下2022-02-02Django+Uwsgi+Nginx如何實(shí)現(xiàn)生產(chǎn)環(huán)境部署
這篇文章主要介紹了Django+Uwsgi+Nginx如何實(shí)現(xiàn)生產(chǎn)環(huán)境部署,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Python基于PycURL實(shí)現(xiàn)POST的方法
這篇文章主要介紹了Python基于PycURL實(shí)現(xiàn)POST的方法,涉及Python實(shí)現(xiàn)curl傳遞post數(shù)據(jù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Python使用requests xpath 并開啟多線程爬取西刺代理ip實(shí)例
這篇文章主要介紹了Python使用requests xpath 并開啟多線程爬取西刺代理ip實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03python如何實(shí)現(xiàn)遞歸轉(zhuǎn)非遞歸
這篇文章主要介紹了python如何實(shí)現(xiàn)遞歸轉(zhuǎn)非遞歸,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-02-02python數(shù)據(jù)操作之lambda表達(dá)式詳情
這篇文章主要介紹了python數(shù)據(jù)操作之lambda表達(dá)式詳情,文章基于python的相關(guān)資料展開lambda表達(dá)式具體的內(nèi)容,感興趣的小伙伴可以參考一下2022-05-05python 讀取.csv文件數(shù)據(jù)到數(shù)組(矩陣)的實(shí)例講解
今天小編就為大家分享一篇python 讀取.csv文件數(shù)據(jù)到數(shù)組(矩陣)的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06