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 相关文章推荐
Python3基础之条件与循环控制实例解析
Aug 13 Python
浅谈Python程序与C++程序的联合使用
Apr 07 Python
python线程中同步锁详解
Apr 27 Python
用python爬取租房网站信息的代码
Dec 14 Python
python读取csv和txt数据转换成向量的实例
Feb 12 Python
python 处理微信对账单数据的实例代码
Jul 19 Python
PyCharm中代码字体大小调整方法
Jul 29 Python
python爬虫 爬取超清壁纸代码实例
Aug 16 Python
解决python 读取excel时 日期变成数字并加.0的问题
Oct 08 Python
PyTorch中 tensor.detach() 和 tensor.data 的区别详解
Jan 06 Python
Django静态文件加载失败解决方案
Aug 26 Python
Python爬虫网络请求之代理服务器和动态Cookies
Apr 12 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
一个程序下载的管理程序(一)
2006/10/09 PHP
php截取字符串函数substr,iconv_substr,mb_substr示例以及优劣分析
2014/06/10 PHP
ThinkPHP处理Ajax返回的方法
2014/11/22 PHP
PHP+Javascript实现在线拍照功能实例
2015/07/18 PHP
php版交通银行网银支付接口开发入门教程
2016/09/26 PHP
php array_values 返回数组的值实例详解
2016/11/17 PHP
JavaScript 对象成员的可见性说明
2009/10/16 Javascript
网站导致浏览器崩溃的原因总结(多款浏览器) 推荐
2010/04/15 Javascript
JQUBar 基于JQUERY的柱状图插件
2010/11/23 Javascript
js switch case default 的用法示例介绍
2013/10/23 Javascript
关于js数组去重的问题小结
2014/01/24 Javascript
页面元素绑定jquery toggle后元素隐藏的解决方法
2014/03/27 Javascript
Jquery基础之事件操作详解
2016/06/14 Javascript
JS DOMReady事件的六种实现方法总结
2016/11/23 Javascript
addEventListener()与removeEventListener()解析
2017/04/20 Javascript
详解key在Vue列表渲染时究竟起到了什么作用
2019/04/20 Javascript
微信小程序简单的canvas裁剪图片功能详解
2019/07/12 Javascript
零基础写python爬虫之抓取百度贴吧代码分享
2014/11/06 Python
简单使用Python自动生成文章
2014/12/25 Python
Python中字典和JSON互转操作实例
2015/01/19 Python
Python实现二维有序数组查找的方法
2016/04/27 Python
详解python 爬取12306验证码
2019/05/10 Python
python pandas cumsum求累计次数的用法
2019/07/29 Python
使用Pyinstaller转换.py文件为.exe可执行程序过程详解
2019/08/06 Python
python基于socket函数实现端口扫描
2020/05/28 Python
我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?
2014/03/30 面试题
夫妻忠诚协议书范本
2014/11/17 职场文书
护士先进个人总结
2015/02/13 职场文书
企业承诺书格式范文
2015/04/28 职场文书
防卫过当辩护词
2015/05/21 职场文书
毕业生入职感言
2015/07/31 职场文书
学习杨善洲同志先进事迹心得体会
2016/01/23 职场文书
2016优秀员工先进事迹材料
2016/02/25 职场文书
MySQL创建索引需要了解的
2021/04/08 MySQL
SQL实战演练之网上商城数据库商品类别数据操作
2021/10/24 MySQL
Python基本的内置数据类型及使用方法
2022/04/13 Python