【pygame游戏编程】第五篇-----动画显示

import pygame
import sys
import os

pygame.init()

#窗口居中
os.environ['SDL_VIDEO_CENTERED'] = '1'

screen_width = 600
screen_high = 500

cnt = 0

image = pygame.image.load(r'running cat.png')
rects = image.get_rect()

rect = pygame.Rect(0, 0, rects.width // 4, rects.height)

#pygame.FULLSCREEN可使screen全屏显示
screen = pygame.display.set_mode((screen_width, screen_high), pygame.FULLSCREEN)

screen_rect = screen.get_rect()

#获取clock对象
clock = pygame.time.Clock()

while True:
    
    screen.fill((255, 250, 230))
    
    for event in pygame.event.get():
        
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    
    press_keys = pygame.key.get_pressed()
    
    if press_keys[pygame.K_ESCAPE]:
        pygame.quit()
        sys.exit()
    
    #在(0,0)处绘制image的rect区域
    screen.blit(image, (0, 0), rect)
    
    rect.x += rects.width // 4
    cnt += 1
    
    #用cnt标记显示到了第几帧
    if cnt == 4:
        cnt = 0
        rect.x = 0
    
    pygame.display.flip()
    
    #每秒循环十次(10帧/秒)
    clock.tick(10)

 运行效果截屏:

使用素材:

猜你喜欢

转载自www.cnblogs.com/huwt/p/10349697.html