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 获取本机ip地址的两个方法
Feb 25 Python
python使用xmlrpc实例讲解
Dec 17 Python
Python转换HTML到Text纯文本的方法
Jan 15 Python
解决Python3中的中文字符编码的问题
Jul 18 Python
python设置随机种子实例讲解
Sep 12 Python
解决python彩色螺旋线绘制引发的问题
Nov 23 Python
使用Python第三方库pygame写个贪吃蛇小游戏
Mar 06 Python
tensorflow中tf.reduce_mean函数的使用
Apr 19 Python
使用python实现CGI环境搭建过程解析
Apr 28 Python
Python3爬虫中Splash的知识总结
Jul 10 Python
Python unittest生成测试报告过程解析
Sep 08 Python
Python排序函数的使用方法详解
Dec 11 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 调试环境(IIS+PHP+MYSQL)
2007/01/10 PHP
php array_flip() 删除数组重复元素
2009/01/14 PHP
php获取本周开始日期和结束日期的方法
2015/03/09 PHP
PHP实现Google plus的好友拖拽分组效果
2016/10/21 PHP
使用SMB共享来绕过php远程文件包含的限制执行RFI的利用
2019/05/31 PHP
PHP copy函数使用案例代码解析
2020/09/01 PHP
通过JAVAScript实现页面自适应
2007/01/19 Javascript
Jquery操作js数组及对象示例代码
2014/05/11 Javascript
Jquery全选与反选点击执行一次的解决方案
2015/08/14 Javascript
jquery zTree异步加载简单实例讲解
2016/02/25 Javascript
jquery获取input type=text中的值的各种方式(总结)
2016/12/02 Javascript
gulp解决跨域的配置文件问题
2017/06/08 Javascript
深入理解AngularJs-scope的脏检查(一)
2017/06/19 Javascript
vue使用axios时关于this的指向问题详解
2017/12/22 Javascript
vue移动端UI框架实现QQ侧边菜单组件
2018/03/09 Javascript
Vue项目前后端联调(使用proxyTable实现跨域方式)
2020/07/18 Javascript
Vue中的this.$options.data()和this.$data用法说明
2020/07/26 Javascript
Python基本数据类型详细介绍
2014/03/11 Python
python使用正则搜索字符串或文件中的浮点数代码实例
2014/07/11 Python
Python中尝试多线程编程的一个简明例子
2015/04/07 Python
使用python爬虫实现网络股票信息爬取的demo
2018/01/05 Python
详解Django CAS 解决方案
2019/10/30 Python
宝拉珍选官方旗舰店:2%水杨酸精华液,收缩毛孔粗大和祛痘
2018/07/01 全球购物
internal修饰符起什么作用
2013/12/16 面试题
大学毕业生最详细的自我评价分享
2013/11/18 职场文书
商务日语毕业生自荐信
2013/11/23 职场文书
运动会开幕式邀请函
2014/01/22 职场文书
财务学生的职业生涯发展
2014/02/11 职场文书
电台实习生求职信
2014/02/25 职场文书
机关单位工作失职检讨书
2014/11/20 职场文书
幼儿园园长新年寄语2015
2014/12/08 职场文书
个人自我鉴定怎么写?
2019/07/01 职场文书
MySQL 数据丢失排查案例
2021/05/08 MySQL
Python Matplotlib库实现画局部图
2021/11/17 Python
Python万能模板案例之matplotlib绘制甘特图
2022/04/13 Python
docker compose 部署 golang 的 Athens 私有代理问题
2022/04/28 Servers