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

一、初始化主界面

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

二、第一种进度条

(一)核心代码

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

(四)运行效果

三、第二种进度条

(一)核心代码

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

(三)运行结果

四、第三种进度条

(一)核心代码

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

(三)运行效果

五、第四种进度条

(一)加载图片资源

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

(六)运行效果

六、综合案例

(一)完整代码

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

(二)运行效果

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

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

(0)

相关推荐

  • python pygame 愤怒的小鸟游戏示例代码

    小鸟(image) 游戏展示 代码展示 import pygame,sys pygame.init()#初始化操作 #保存窗口大小 width,height=600,400 screen=pygame.display.set_mode([width,height])#创建游戏窗口 #设置窗口标题 pygame.display.set_caption("愤怒的小鸟") #加载小鸟素材 player=pygame.image.load("xiaoniao.png") #

  • Python3.8安装Pygame教程步骤详解

    注:因为最近想用一下Python做一些简单小游戏的开发作为项目练手之用,而Pygame模块里面提供了大量的有用的方法和属性.今天我们就在之前安装过PyCharm的基础上,安装Pygame,下面是安装的步骤,希望能够帮到大家. 第一步 安装Python和pip 如果已安装,使用python --version 查看安装的Python版本 使用pip --version查看安装的pip版本 注:如果是没有安装pip,其下载链接是Download the file for your platform:

  • python+pygame实现代码雨(黑客帝国既视感)

    本文主要介绍了python+pygame实现代码雨,分享给大家,具体如下: 效果: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/12/29 12:34 # @Author : huni # @File : 代码雨.py # @Software: PyCharm import random import pygame PANEL_width = 1600 PANEL_highly = 1000 FONT_PX = 15

  • python基于pygame实现飞机大作战小游戏

    基于pygame的飞机大作战小游戏,适合新手,不能直接运行,只能在命令行进入当前游戏目录,输入python game.py才能够运行,尚不知道是什么原因. 游戏截图如下,我们用黄色的圆圈代表敌机: 代码如下 import pygame,sys,time,random,math def init(): pygame.init() size = width, height =600,600 screen =pygame.display.set_mode(size) plx=270 ply=528 b

  • python之pygame模块实现飞机大战完整代码

    本文实例为大家分享了python之pygame模块实现飞机大战的具体代码,供大家参考,具体内容如下 Python飞机大战步骤: 1.数据区 2.主界面 3.飞船 4.事件监控及边界 5.外星人 6.记分系统 飞机大战效果图: 源码: """ 功能:飞机大战 time:2019/10/3 """ import os import pygame import sys import time from pygame.sprite import Spri

  • python+pygame实现坦克大战小游戏的示例代码(可以自定义子弹速度)

    python+pygame实现坦克大战小游戏-可以自定义子弹速度: 运行环境–python3.7.pycharm: 源码需要请:点赞留言邮箱: 正常版子弹速度: 普通速度版 加速版子弹速度: 子弹加速版 另外还有多种道具,支持两人一起玩.main()方法如下: def main(): pygame.init() pygame.mixer.init() resolution = 630, 630 screen = pygame.display.set_mode(resolution) pygame

  • Python使用Pygame绘制时钟

    本文实例为大家分享了Python使用Pygame绘制时钟的具体代码,供大家参考,具体内容如下 前提条件: 需要安装pygame 功能: 1.初始化界面显示一个时钟界面 2.根据当前的时间实现时针.分针.秒针的移动 import pygame, sys, random, math from datetime import datetime from pygame.locals import * def print_text(font, x, y, text, color=(255, 255, 25

  • Python Pygame实现俄罗斯方块

    本文实例为大家分享了Python Pygame实现俄罗斯方块的具体代码,供大家参考,具体内容如下 源码: # coding : utf-8 #: pip install pygame import random import sys import pygame #: 颜色定义 COLOR_WHITE = (255, 255, 255) COLOR_BLACK = (0, 0, 0) class Block: """小块""" width = 24

  • 如何基于Python pygame实现动画跑马灯

    前言 大家都看过彩带飘落吧?这个在比较喜庆的场合是很常见的: 还有"跑马灯"效果,听起来很陌生,其实很常见,下面的就是: 好了,相信大家都有了初步的认识.当然,如果有做前端或者搞设计的同学,上面的效果应该不难实现,那如果想通过Python呢?有没有包可以调用呢? 答案是有的--pygame 这个包适合用来开发游戏,今天就不打算给大家详细介绍了,还是想给大伙儿放松放松,以后有机会再多写写它. 不多说,直接甩出代码: import pygame from random import ran

  • python 基于pygame实现俄罗斯方块

    一.简单说明 80.90后的小伙伴都玩过"俄罗斯方块",那种"叱咤风云"场景 偶尔闪现在脑海 真的是太爽了:如果没有来得及玩过的同学,这次可以真正的自己做一个了 本实例用的是Python3(当然了Python3.5 3.6 3.7....都行 )+ pygame实现的 运行之前需要安装pygame模块,安装命令如下 pip install pygame -i https://mirrors.aliyun.com/pypi/simple/ 二.运行效果 三.完整代码

随机推荐