一、實現邏輯
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())
(二)循環函數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 += 1
fcclock.tick(fps)
pygame.display.flip() # 刷新窗口
(三)相關庫引入及變量初始化
import sys, pygame
import os
import random
import time
pygame.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, pygame
import os
import random
import time
pygame.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 += 1
fcclock.tick(fps)
pygame.display.flip() # 刷新窗口
if __name__ == '__main__':
init_image()
run()
四、運行效果

OK,完成了,比較簡單,大家都學會了嗎?
到此這篇關于Python趣味挑戰之用pygame實現簡單的金幣旋轉效果的文章就介紹到這了,更多相關pygame實現金幣旋轉內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python趣味挑戰之教你用pygame畫進度條
- Python趣味挑戰之pygame實現無敵好看的百葉窗動態效果
- Python3+Pygame實現射擊游戲完整代碼
- python 基于pygame實現俄羅斯方塊
- python pygame 憤怒的小鳥游戲示例代碼
- Python3.9.0 a1安裝pygame出錯解決全過程(小結)
- python之pygame模塊實現飛機大戰完整代碼
- Python使用Pygame繪制時鐘
- Python3.8安裝Pygame教程步驟詳解
- python pygame入門教程