您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:Python趣味挑战之教你用pygame画进度条

51自学网 2021-10-30 22:36:00
  python
这篇教程Python趣味挑战之教你用pygame画进度条写得很实用,希望能帮到您。

一、初始化主界面

import pygamepygame.init()screen = pygame.display.set_mode((500,300))pygame.display.set_caption("好看的进度条显示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()

在这里插入图片描述

二、第一种进度条

(一)核心代码

 pygame.draw.rect(screen,(192,192,192),(5,100,490,20))    pygame.draw.rect(screen,(0,0,255),(5,100,step,20))

(二)设置步长,并循环递增

step += 1

(三)完整代码

import pygame,syspygame.init()screen = pygame.display.set_mode((500,300))pygame.display.set_caption("好看的进度条显示V1.0")clock = pygame.time.Clock()step = 0while 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()

(四)运行效果

在这里插入图片描述

三、第二种进度条

(一)核心代码

 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,syspygame.init()screen = pygame.display.set_mode((500,300))pygame.display.set_caption("好看的进度条显示V1.0")clock = pygame.time.Clock()step = 0while 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()

(三)运行结果

在这里插入图片描述

四、第三种进度条

(一)核心代码

 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,syspygame.init()screen = pygame.display.set_mode((500,300))pygame.display.set_caption("好看的进度条显示V1.0")clock = pygame.time.Clock()step = 0length = 480while 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()

(三)运行效果

在这里插入图片描述

五、第四种进度条

(一)加载图片资源

picture = pygame.transform.scale(pygame.image.load('score/5.png'), (20, 20))

(二)画进度条

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))

(五)完整代码

import pygame,syspygame.init()screen = pygame.display.set_mode((500,300))pygame.display.set_caption("好看的进度条显示V1.0")clock = pygame.time.Clock()picture = pygame.transform.scale(pygame.image.load('score/5.png'), (20, 20))step = 0length = 480while 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()

(六)运行效果

在这里插入图片描述

六、综合案例

(一)完整代码

import pygame,syspygame.init()screen = pygame.display.set_mode((500,300))pygame.display.set_caption("好看的进度条显示V1.0")clock = pygame.time.Clock()picture = pygame.transform.scale(pygame.image.load('score/5.png'), (20, 20))step = 0length = 480while 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()

(二)运行效果

在这里插入图片描述

OK,写完,本博文纯属科普贴,技术含量不高,入门级别,大家喜欢就好。
而且里面代码相对比较简单,也没有考虑优化,大家在实操过程中可以优化完善,并反馈给我一起进步。

到此这篇关于Python趣味挑战之教你用pygame画进度条的文章就介绍到这了,更多相关pygame画进度条内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net!


python趣味挑战之爬取天气与微博热搜并自动发给微信好友
Django实现聊天机器人
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。