Python趣味挑戰(zhàn)之用pygame實現(xiàn)簡單的金幣旋轉(zhuǎn)效果
一、實現(xiàn)邏輯
step1、保存圖像到list列表。
step2、在主窗口每次顯示一張list列表中的對象。
呵呵,好像就這么簡單。所以,主要還是要有圖片。
這里也分享一下圖片給大家。

二、核心邏輯代碼解析
(一)加載圖像到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實現(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)庫引入及變量初始化
import sys, pygame
import os
import random
import time
pygame.init() # 初始化pygame類
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主入口實現(xiàn)
if __name__ == '__main__':
init_image()
run()
三、完整代碼
import sys, pygame
import os
import random
import time
pygame.init() # 初始化pygame類
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()
四、運行效果

OK,完成了,比較簡單,大家都學(xué)會了嗎?
到此這篇關(guān)于Python趣味挑戰(zhàn)之用pygame實現(xiàn)簡單的金幣旋轉(zhuǎn)效果的文章就介紹到這了,更多相關(guān)pygame實現(xiàn)金幣旋轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python腳本開發(fā)中的命令行參數(shù)及傳參示例詳解
這篇文章主要為大家介紹了Python腳本開發(fā)中的命令行參數(shù)及傳參示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07
MediaPipe API實現(xiàn)骨骼識別功能分步講解流程
MediaPipe是用于構(gòu)建多模態(tài)(例如視頻、音頻或任何時間序列數(shù)據(jù))、跨平臺(即eAndroid、IOS、web、邊緣設(shè)備)應(yīng)用ML管道的框架。這篇文章主要介紹了MediaPipe API實現(xiàn)骨骼識別功能分步流程2022-09-09
使用python+Flask實現(xiàn)日志在web網(wǎng)頁實時更新顯示
日志是一種可以追蹤某些軟件運行時所發(fā)生事件的方法,下面這篇文章主要給大家介紹了關(guān)于使用python+Flask實現(xiàn)日志在web網(wǎng)頁實時更新顯示的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08
Python利用FlashText算法實現(xiàn)替換字符串
FlashText算法是由?Vikash?Singh?于2017年發(fā)表的大規(guī)模關(guān)鍵詞替換算法,比正則表達式替換快M倍以上,這個M是需要替換的關(guān)鍵詞數(shù)量,關(guān)鍵詞越多,F(xiàn)lashText算法的優(yōu)勢就越明顯。本文將詳細這一算法,需要的可以參考一下2022-03-03
Python數(shù)據(jù)庫的連接實現(xiàn)方法與注意事項
這篇文章主要介紹了Python數(shù)據(jù)庫的連接實現(xiàn)方法與注意事項,需要的朋友可以參考下2016-02-02
基于Python pyecharts實現(xiàn)多種圖例代碼解析
這篇文章主要介紹了基于Python pyecharts實現(xiàn)多種圖例代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08

