Python趣味挑戰(zhàn)之教你用pygame畫(huà)進(jìn)度條
一、初始化主界面
import pygame
pygame.init()
screen = pygame.display.set_mode((500,300))
pygame.display.set_caption("好看的進(jìn)度條顯示V1.0")
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_F1:
pygame.quit()
sys.exit()
screen.fill((255,255,255))
clock.tick(30)
pygame.display.flip()

二、第一種進(jìn)度條
(一)核心代碼
pygame.draw.rect(screen,(192,192,192),(5,100,490,20))
pygame.draw.rect(screen,(0,0,255),(5,100,step,20))
(二)設(shè)置步長(zhǎng),并循環(huán)遞增
step += 1
(三)完整代碼
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((500,300))
pygame.display.set_caption("好看的進(jìn)度條顯示V1.0")
clock = pygame.time.Clock()
step = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_F1:
pygame.quit()
sys.exit()
screen.fill((255,255,255))
# screen.fill((0,0,0))
pygame.draw.rect(screen,(192,192,192),(5,100,490,20))
pygame.draw.rect(screen,(0,0,255),(5,100,step % 490,20))
step += 1
clock.tick(60)
pygame.display.flip()
(四)運(yùn)行效果

三、第二種進(jìn)度條
(一)核心代碼
pygame.draw.rect(screen,(192,192,192),(5,100,490,20))
pygame.draw.rect(screen,(0,0,255),(5,100,step % 490,20))
font1 = pygame.font.Font(r'C:\Windows\Fonts\simsun.ttc', 16)
text1 = font1.render('%s %%' % str(int((step % 490)/490*100)), True, (255,0,0))
screen.blit(text1, (245, 100))
(二)完整代碼
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((500,300))
pygame.display.set_caption("好看的進(jìn)度條顯示V1.0")
clock = pygame.time.Clock()
step = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_F1:
pygame.quit()
sys.exit()
screen.fill((255,255,255))
# screen.fill((0,0,0))
pygame.draw.rect(screen,(192,192,192),(5,100,490,20))
pygame.draw.rect(screen,(0,0,255),(5,100,step % 490,20))
font1 = pygame.font.Font(r'C:\Windows\Fonts\simsun.ttc', 16)
text1 = font1.render('%s %%' % str(int((step % 490)/490*100)), True, (255,0,0))
screen.blit(text1, (245, 100))
step += 1
clock.tick(60)
pygame.display.flip()
(三)運(yùn)行結(jié)果

四、第三種進(jìn)度條
(一)核心代碼
pygame.draw.rect(screen,(192,192,192),(5,100,length+10,20))
pygame.draw.rect(screen,(0,0,255),(5,100,step % length,20))
pygame.draw.circle(screen,(0,0,255),(step % length,110),10)
font1 = pygame.font.Font(r'C:\Windows\Fonts\simsun.ttc', 16)
text1 = font1.render('%s %%' % str(int((step % length)/length*100)), True, (255,0,0))
screen.blit(text1, (245, 100))
(二)完整代碼
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((500,300))
pygame.display.set_caption("好看的進(jìn)度條顯示V1.0")
clock = pygame.time.Clock()
step = 0
length = 480
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_F1:
pygame.quit()
sys.exit()
screen.fill((255,255,255))
# screen.fill((0,0,0))
pygame.draw.rect(screen,(192,192,192),(5,100,length+10,20))
pygame.draw.rect(screen,(0,0,255),(5,100,step % length,20))
pygame.draw.circle(screen,(0,0,255),(step % length,110),10)
font1 = pygame.font.Font(r'C:\Windows\Fonts\simsun.ttc', 16)
text1 = font1.render('%s %%' % str(int((step % length)/length*100)), True, (255,0,0))
screen.blit(text1, (245, 100))
step += 1
clock.tick(60)
pygame.display.flip()
(三)運(yùn)行效果

五、第四種進(jìn)度條
(一)加載圖片資源
picture = pygame.transform.scale(pygame.image.load('score/5.png'), (20, 20))
(二)畫(huà)進(jìn)度條
pygame.draw.rect(screen,(192,192,192),(5,100,length+10,20))
pygame.draw.rect(screen,(251,174,63),(5,100,step % length,20))
(三)畫(huà)圖片資源
screen.blit(picture,(step%length,100))
(四)畫(huà)文字
font1 = pygame.font.Font(r'C:\Windows\Fonts\simsun.ttc', 16)
text1 = font1.render('%s %%' % str(int((step % length)/length*100)), True, (255,0,0))
screen.blit(text1, (245, 100))
(五)完整代碼
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((500,300))
pygame.display.set_caption("好看的進(jìn)度條顯示V1.0")
clock = pygame.time.Clock()
picture = pygame.transform.scale(pygame.image.load('score/5.png'), (20, 20))
step = 0
length = 480
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_F1:
pygame.quit()
sys.exit()
screen.fill((255,255,255))
# screen.fill((0,0,0))
pygame.draw.rect(screen,(192,192,192),(5,100,length+10,20))
pygame.draw.rect(screen,(251,174,63),(5,100,step % length,20))
screen.blit(picture,(step%length,100))
font1 = pygame.font.Font(r'C:\Windows\Fonts\simsun.ttc', 16)
text1 = font1.render('%s %%' % str(int((step % length)/length*100)), True, (255,0,0))
screen.blit(text1, (245, 100))
step += 1
clock.tick(60)
pygame.display.flip()
(六)運(yùn)行效果

六、綜合案例
(一)完整代碼
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((500,300))
pygame.display.set_caption("好看的進(jìn)度條顯示V1.0")
clock = pygame.time.Clock()
picture = pygame.transform.scale(pygame.image.load('score/5.png'), (20, 20))
step = 0
length = 480
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_F1:
pygame.quit()
sys.exit()
screen.fill((255,255,255))
# screen.fill((0,0,0))
# 第一種
pygame.draw.rect(screen,(192,192,192),(5,100,490,20))
pygame.draw.rect(screen,(0,0,255),(5,100,step % 490,20))
# 第二種
pygame.draw.rect(screen,(192,192,192),(5,150,490,20))
pygame.draw.rect(screen,(0,0,255),(5,150,step % 490,20))
font1 = pygame.font.Font(r'C:\Windows\Fonts\simsun.ttc', 16)
text1 = font1.render('%s %%' % str(int((step % 490)/490*100)), True, (255,0,0))
screen.blit(text1, (245, 150))
# 第三種
pygame.draw.rect(screen,(192,192,192),(5,200,length+10,20))
pygame.draw.rect(screen,(0,0,255),(5,200,step % length,20))
pygame.draw.circle(screen,(0,0,255),(step % length,210),10)
font1 = pygame.font.Font(r'C:\Windows\Fonts\simsun.ttc', 16)
text1 = font1.render('%s %%' % str(int((step % length)/length*100)), True, (255,0,0))
screen.blit(text1, (245, 200))
# 第四種
pygame.draw.rect(screen,(192,192,192),(5,250,length+10,20))
pygame.draw.rect(screen,(251,174,63),(5,250,step % length,20))
screen.blit(picture,(step%length,250))
font1 = pygame.font.Font(r'C:\Windows\Fonts\simsun.ttc', 16)
text1 = font1.render('%s %%' % str(int((step % length)/length*100)), True, (255,0,0))
screen.blit(text1, (245, 250))
step += 1
clock.tick(60)
pygame.display.flip()
(二)運(yùn)行效果

OK,寫(xiě)完,本博文純屬科普貼,技術(shù)含量不高,入門(mén)級(jí)別,大家喜歡就好。
而且里面代碼相對(duì)比較簡(jiǎn)單,也沒(méi)有考慮優(yōu)化,大家在實(shí)操過(guò)程中可以?xún)?yōu)化完善,并反饋給我一起進(jìn)步。
到此這篇關(guān)于Python趣味挑戰(zhàn)之教你用pygame畫(huà)進(jìn)度條的文章就介紹到這了,更多相關(guān)pygame畫(huà)進(jìn)度條內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python趣味挑戰(zhàn)之pygame實(shí)現(xiàn)無(wú)敵好看的百葉窗動(dòng)態(tài)效果
- Python趣味挑戰(zhàn)之用pygame實(shí)現(xiàn)簡(jiǎn)單的金幣旋轉(zhuǎn)效果
- 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)文章
Python3 執(zhí)行系統(tǒng)命令并獲取實(shí)時(shí)回顯功能
這篇文章主要介紹了Python3 執(zhí)行系統(tǒng)命令并獲取實(shí)時(shí)回顯功能,文中通過(guò)兩種方法給大家介紹了Python執(zhí)行系統(tǒng)命令并獲得輸出的方法,需要的朋友可以參考下2019-07-07
python利用faker庫(kù)批量生成測(cè)試數(shù)據(jù)
小編經(jīng)常需要批量測(cè)試一些數(shù)據(jù),有時(shí)候測(cè)試環(huán)境又暫時(shí)沒(méi)數(shù)據(jù),特意找了一下,發(fā)現(xiàn)有一個(gè)可批量生成數(shù)據(jù)的python庫(kù)—-faker,現(xiàn)在就介紹一下它的使用方法,如果你不想一行一行輸入代碼,小編提供了完整測(cè)試代碼,見(jiàn)文末代碼章節(jié)。2020-10-10
通過(guò)實(shí)例了解python__slots__使用方法
這篇文章主要介紹了通過(guò)實(shí)例了解python__slots__使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
python安裝后無(wú)法打開(kāi)IDLE?Subprocess?Connection?Error的解決方法
有朋友在安裝了Python之后發(fā)現(xiàn)不能正常使用,就說(shuō)明安裝過(guò)程出了問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于python安裝后無(wú)法打開(kāi)IDLE?Subprocess?Connection?Error的解決方法,需要的朋友可以參考下2023-01-01
對(duì)變量賦值的理解--Pyton中讓兩個(gè)值互換的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Pyton中讓兩個(gè)值互換的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
python中使用urllib2獲取http請(qǐng)求狀態(tài)碼的代碼例子
這篇文章主要介紹了python中使用urllib2獲取http請(qǐng)求狀態(tài)碼的代碼例子,需要的朋友可以參考下2014-07-07

