Python趣味挑戰(zhàn)之用pygame實(shí)現(xiàn)簡(jiǎn)單的金幣旋轉(zhuǎn)效果
一、實(shí)現(xiàn)邏輯
step1、保存圖像到list列表。
step2、在主窗口每次顯示一張list列表中的對(duì)象。
呵呵,好像就這么簡(jiǎn)單。所以,主要還是要有圖片。
這里也分享一下圖片給大家。
二、核心邏輯代碼解析
(一)加載圖像到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())
(二)循環(huán)函數(shù)run實(shí)現(xiàn)
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)) # 設(shè)置背景為白色 screen.blit(bglist[i % 7], (50, 50)) print(bglist[i % 7].get_size()) i += 1 fcclock.tick(fps) pygame.display.flip() # 刷新窗口
(三)相關(guān)庫(kù)引入及變量初始化
import sys, pygame import os import random import time pygame.init() # 初始化pygame類(lèi) screen = pygame.display.set_mode((600, 600)) # 設(shè)置窗口大小 pygame.display.set_caption('金幣翻轉(zhuǎn)小游戲V1.0') # 設(shè)置窗口標(biāo)題 tick = pygame.time.Clock() fps = 10 # 設(shè)置刷新率,數(shù)字越大刷新率越高 fcclock = pygame.time.Clock() bglist = []
(四)main主入口實(shí)現(xiàn)
if __name__ == '__main__': init_image() run()
三、完整代碼
import sys, pygame import os import random import time pygame.init() # 初始化pygame類(lèi) screen = pygame.display.set_mode((600, 600)) # 設(shè)置窗口大小 pygame.display.set_caption('金幣翻轉(zhuǎn)小游戲V1.0') # 設(shè)置窗口標(biāo)題 tick = pygame.time.Clock() fps = 10 # 設(shè)置刷新率,數(shù)字越大刷新率越高 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)) # 設(shè)置背景為白色 screen.blit(bglist[i % 7], (50, 50)) print(bglist[i % 7].get_size()) i += 1 fcclock.tick(fps) pygame.display.flip() # 刷新窗口 if __name__ == '__main__': init_image() run()
四、運(yùn)行效果
OK,完成了,比較簡(jiǎn)單,大家都學(xué)會(huì)了嗎?
到此這篇關(guān)于Python趣味挑戰(zhàn)之用pygame實(shí)現(xiàn)簡(jiǎn)單的金幣旋轉(zhuǎn)效果的文章就介紹到這了,更多相關(guān)pygame實(shí)現(xiàn)金幣旋轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python趣味挑戰(zhàn)之教你用pygame畫(huà)進(jìn)度條
- Python趣味挑戰(zhàn)之pygame實(shí)現(xiàn)無(wú)敵好看的百葉窗動(dòng)態(tài)效果
- Python3+Pygame實(shí)現(xiàn)射擊游戲完整代碼
- python 基于pygame實(shí)現(xiàn)俄羅斯方塊
- python pygame 憤怒的小鳥(niǎo)游戲示例代碼
- Python3.9.0 a1安裝pygame出錯(cuò)解決全過(guò)程(小結(jié))
- python之pygame模塊實(shí)現(xiàn)飛機(jī)大戰(zhàn)完整代碼
- Python使用Pygame繪制時(shí)鐘
- Python3.8安裝Pygame教程步驟詳解
- python pygame入門(mén)教程
相關(guān)文章
Python腳本開(kāi)發(fā)中的命令行參數(shù)及傳參示例詳解
這篇文章主要為大家介紹了Python腳本開(kāi)發(fā)中的命令行參數(shù)及傳參示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07MediaPipe API實(shí)現(xiàn)骨骼識(shí)別功能分步講解流程
MediaPipe是用于構(gòu)建多模態(tài)(例如視頻、音頻或任何時(shí)間序列數(shù)據(jù))、跨平臺(tái)(即eAndroid、IOS、web、邊緣設(shè)備)應(yīng)用ML管道的框架。這篇文章主要介紹了MediaPipe API實(shí)現(xiàn)骨骼識(shí)別功能分步流程2022-09-09Python實(shí)現(xiàn)處理逆波蘭表達(dá)式示例
這篇文章主要介紹了Python實(shí)現(xiàn)處理逆波蘭表達(dá)式操作,結(jié)合實(shí)例形式分析了逆波蘭表達(dá)式的概念、原理及Python針對(duì)逆波蘭表達(dá)式的定義與計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2018-07-07使用python+Flask實(shí)現(xiàn)日志在web網(wǎng)頁(yè)實(shí)時(shí)更新顯示
日志是一種可以追蹤某些軟件運(yùn)行時(shí)所發(fā)生事件的方法,下面這篇文章主要給大家介紹了關(guān)于使用python+Flask實(shí)現(xiàn)日志在web網(wǎng)頁(yè)實(shí)時(shí)更新顯示的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Python利用FlashText算法實(shí)現(xiàn)替換字符串
FlashText算法是由?Vikash?Singh?于2017年發(fā)表的大規(guī)模關(guān)鍵詞替換算法,比正則表達(dá)式替換快M倍以上,這個(gè)M是需要替換的關(guān)鍵詞數(shù)量,關(guān)鍵詞越多,F(xiàn)lashText算法的優(yōu)勢(shì)就越明顯。本文將詳細(xì)這一算法,需要的可以參考一下2022-03-03Python+xlwings制作天氣預(yù)報(bào)表
python操作Excel的模塊,網(wǎng)上提到的模塊大致有:xlwings、xlrd、xlwt、openpyxl、pyxll等。本文將利用xlwings模塊制作一個(gè)天氣預(yù)報(bào)表,需要的可以參考一下2022-01-01Python數(shù)據(jù)庫(kù)的連接實(shí)現(xiàn)方法與注意事項(xiàng)
這篇文章主要介紹了Python數(shù)據(jù)庫(kù)的連接實(shí)現(xiàn)方法與注意事項(xiàng),需要的朋友可以參考下2016-02-02基于Python pyecharts實(shí)現(xiàn)多種圖例代碼解析
這篇文章主要介紹了基于Python pyecharts實(shí)現(xiàn)多種圖例代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08