Python 취미 도전 의 가르침 pygame 으로 진도 표 그리 기

1.메 인 화면 초기 화

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()
在这里插入图片描述
2.첫 번 째 진도 조
(1)핵심 코드

 pygame.draw.rect(screen,(192,192,192),(5,100,490,20))
    pygame.draw.rect(screen,(0,0,255),(5,100,step,20))
(2)보폭 을 설정 하고 순환 증가

step += 1
(3)전체 코드

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()
(4)실행 효과
在这里插入图片描述
3.두 번 째 진도 조
(1)핵심 코드

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

(2)전체 코드

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()
(3)실행 결과
在这里插入图片描述
넷 째,세 번 째 진도 조
(1)핵심 코드

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

(2)전체 코드

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()
(3)실행 효과
在这里插入图片描述
다섯 째,네 번 째 진도 조
(1)그림 자원 불 러 오기

picture = pygame.transform.scale(pygame.image.load('score/5.png'), (20, 20))
(2)진도 표 그리 기

pygame.draw.rect(screen,(192,192,192),(5,100,length+10,20))
    pygame.draw.rect(screen,(251,174,63),(5,100,step % length,20))
(3)그림 자원

  screen.blit(picture,(step%length,100))
(4)글자 그리 기

 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))
(5)전체 코드

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()
(6)실행 효과
在这里插入图片描述
6.종합 사례
(1)전체 코드

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()
(2)실행 효과
在这里插入图片描述
OK,다 썼 습 니 다.이 박문 은 순 전 히 과학 보급 스티커 에 속 하고 기술 함량 이 높 지 않 으 며 입문 등급 이 니 여러분 이 좋아 하 시 면 됩 니 다.
그리고 내부 코드 가 상대 적 으로 간단 하고 최적화 도 고려 하지 않 았 습 니 다.여러분 들 은 실제 작업 과정 에서 최적화 하고 보완 할 수 있 으 며 저 에 게 함께 발전 하도록 피드백 할 수 있 습 니 다.
파 이 썬 의 취미 도전 에 관 한 이 글 은 pygame 으로 진도 표를 그 리 는 것 을 가 르 치 는 글 입 니 다.더 많은 pygame 진도 항목 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 세 요.앞으로 많은 응원 바 랍 니 다!

좋은 웹페이지 즐겨찾기