Python趣味挑战之教你用pygame画进度条


Posted in Python onMay 31, 2021

一、初始化主界面

import pygame

pygame.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()

Python趣味挑战之教你用pygame画进度条

二、第一种进度条

(一)核心代码

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,sys

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

(四)运行效果

Python趣味挑战之教你用pygame画进度条

三、第二种进度条

(一)核心代码

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("好看的进度条显示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()

(三)运行结果

Python趣味挑战之教你用pygame画进度条

四、第三种进度条

(一)核心代码

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("好看的进度条显示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()

(三)运行效果

Python趣味挑战之教你用pygame画进度条

五、第四种进度条

(一)加载图片资源

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,sys

pygame.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 = 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()

(六)运行效果

Python趣味挑战之教你用pygame画进度条

六、综合案例

(一)完整代码

import pygame,sys

pygame.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 = 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()

(二)运行效果

Python趣味挑战之教你用pygame画进度条

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

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

Python 相关文章推荐
纯Python开发的nosql数据库CodernityDB介绍和使用实例
Oct 23 Python
python常见数制转换实例分析
May 09 Python
python实现文本文件合并
Dec 29 Python
Python实现简单登录验证
Apr 13 Python
Python松散正则表达式用法分析
Apr 29 Python
python探索之BaseHTTPServer-实现Web服务器介绍
Oct 28 Python
Python xlwt设置excel单元格字体及格式
Apr 18 Python
python获取代码运行时间的实例代码
Jun 11 Python
Python如何使用BeautifulSoup爬取网页信息
Nov 26 Python
Python如何将函数值赋给变量
Apr 28 Python
python中pop()函数的语法与实例
Dec 01 Python
python RSA加密的示例
Dec 09 Python
Python趣味挑战之用pygame实现简单的金币旋转效果
May 31 #Python
解决pytorch读取自制数据集出现过的问题
Python爬虫基础初探selenium
只用40行Python代码就能写出pdf转word小工具
pytorch 如何把图像数据集进行划分成train,test和val
May 31 #Python
Python图片检索之以图搜图
写一个Python脚本下载哔哩哔哩舞蹈区的所有视频
You might like
PHP中的加密功能
2006/10/09 PHP
约瑟夫环问题的PHP实现 使用PHP数组内部指针操作函数
2010/10/12 PHP
基于Zend的Config机制的应用分析
2013/05/02 PHP
cakephp常见知识点汇总
2017/02/24 PHP
jQuery代码优化之基本事件
2011/11/01 Javascript
jquery拖动插件(jquery.drag)使用介绍
2013/06/18 Javascript
SyntaxHighlighter 3.0.83使用笔记
2015/01/26 Javascript
JS动态改变表格边框宽度的方法
2015/03/31 Javascript
Bootstrap表单布局样式源代码
2016/07/04 Javascript
AngularJs Understanding the Controller Component
2016/09/02 Javascript
微信js-sdk预览图片接口及从拍照或手机相册中选图接口用法示例
2016/10/13 Javascript
VUE利用vuex模拟实现新闻点赞功能实例
2017/06/28 Javascript
详解vue+vuex+koa2开发环境搭建及示例开发
2018/01/22 Javascript
js运算符的一些特殊用法
2018/07/29 Javascript
angular2组件中定时刷新并清除定时器的实例讲解
2018/08/31 Javascript
Python 冒泡,选择,插入排序使用实例
2015/02/05 Python
python使用socket向客户端发送数据的方法
2015/04/29 Python
Python创建模块及模块导入的方法
2015/05/27 Python
Python中的self用法详解
2019/08/06 Python
python dataframe NaN处理方式
2019/12/26 Python
Python实现检测文件的MD5值来查找重复文件案例
2020/03/12 Python
python 调用Google翻译接口的方法
2020/12/09 Python
澳洲的服装老品牌:SABA
2018/02/06 全球购物
Expedia丹麦:全球领先的旅游网站
2018/03/18 全球购物
网站域名和主机:Domain.com
2019/04/01 全球购物
沃达丰英国有限公司:Vodafone英国
2019/04/16 全球购物
Oasis服装官网:时尚女装在线
2020/07/09 全球购物
一些PHP的面试题
2015/05/06 面试题
汽车专业毕业生推荐信
2013/11/12 职场文书
小学门卫岗位职责
2013/12/17 职场文书
红色故事演讲稿
2014/05/22 职场文书
大学生个人求职信例文
2014/07/07 职场文书
李白故里导游词
2015/02/12 职场文书
升职自荐信怎么写
2015/03/05 职场文书
调任通知
2015/04/21 职场文书
《最后一头战象》教学反思
2016/02/16 职场文书