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

二、核心邏輯代碼解析
(一)加載圖像到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)庫引入及變量初始化
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主入口實(shí)現(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()
四、運(yùn)行效果

OK,完成了,比較簡單,大家都學(xué)會(huì)了嗎?
到此這篇關(guān)于Python趣味挑戰(zhàn)之用pygame實(shí)現(xiàn)簡單的金幣旋轉(zhuǎn)效果的文章就介紹到這了,更多相關(guān)pygame實(shí)現(xiàn)金幣旋轉(zhuǎn)內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信