您的位置:首頁技術文章
文章詳情頁

Python趣味挑戰之用pygame實現簡單的金幣旋轉效果

瀏覽:87日期:2022-06-18 08:29:55
一、實現邏輯

step1、保存圖像到list列表。step2、在主窗口每次顯示一張list列表中的對象。

呵呵,好像就這么簡單。所以,主要還是要有圖片。這里也分享一下圖片給大家。

Python趣味挑戰之用pygame實現簡單的金幣旋轉效果

二、核心邏輯代碼解析

(一)加載圖像到list列表

def init_image(): path = ’./score/’ files = [] dirs = os.listdir(path) for diretion in dirs:files.append(path + diretion) for file in files:bglist.append(pygame.image.load(file).convert_alpha())

(二)循環函數run實現

def run(): i = 0 while True:for event in pygame.event.get(): if event.type == pygame.QUIT or event.type == pygame.K_F1:pygame.quit()sys.exit() if event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit()screen.fill((0, 0, 0)) # 設置背景為白色screen.blit(bglist[i % 7], (50, 50))print(bglist[i % 7].get_size())i += 1fcclock.tick(fps)pygame.display.flip() # 刷新窗口

(三)相關庫引入及變量初始化

import sys, pygameimport osimport randomimport timepygame.init() # 初始化pygame類screen = pygame.display.set_mode((600, 600)) # 設置窗口大小pygame.display.set_caption(’金幣翻轉小游戲V1.0’) # 設置窗口標題tick = pygame.time.Clock()fps = 10 # 設置刷新率,數字越大刷新率越高fcclock = pygame.time.Clock()bglist = []

(四)main主入口實現

if __name__ == ’__main__’: init_image() run()三、完整代碼

import sys, pygameimport osimport randomimport timepygame.init() # 初始化pygame類screen = pygame.display.set_mode((600, 600)) # 設置窗口大小pygame.display.set_caption(’金幣翻轉小游戲V1.0’) # 設置窗口標題tick = pygame.time.Clock()fps = 10 # 設置刷新率,數字越大刷新率越高fcclock = pygame.time.Clock()bglist = []def init_image(): path = ’./score/’ files = [] dirs = os.listdir(path) for diretion in dirs:files.append(path + diretion) for file in files:bglist.append(pygame.image.load(file).convert_alpha())def run(): i = 0 while True:for event in pygame.event.get(): if event.type == pygame.QUIT or event.type == pygame.K_F1:pygame.quit()sys.exit() if event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit()screen.fill((0, 0, 0)) # 設置背景為白色screen.blit(bglist[i % 7], (50, 50))print(bglist[i % 7].get_size())i += 1fcclock.tick(fps)pygame.display.flip() # 刷新窗口if __name__ == ’__main__’: init_image() run()四、運行效果

Python趣味挑戰之用pygame實現簡單的金幣旋轉效果

OK,完成了,比較簡單,大家都學會了嗎?

到此這篇關于Python趣味挑戰之用pygame實現簡單的金幣旋轉效果的文章就介紹到這了,更多相關pygame實現金幣旋轉內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
国产综合久久一区二区三区