#!python # -*- coding: utf-8 -*- u""" Display split image with Pygame. Windows10 x64 21H1 + Python 3.9.5 64bit + pygame 2.0.1 """ import pygame import sys SCRW, SCRH = 320, 240 pygame.init() screen = pygame.display.set_mode((SCRW, SCRH), pygame.DOUBLEBUF) imgname = "spritesheet_number.png" img = pygame.image.load(imgname).convert_alpha() # make split area list rects = [] w, h = 96, 96 for y in range(8): for x in range(8): rects.append(pygame.Rect(x * w, y * h, w, h)) index = 0 running = True clock = pygame.time.Clock() # Main loop while running: # check event for ev in pygame.event.get(): if ev.type == pygame.QUIT: running = False if ev.type == pygame.KEYDOWN: if ev.key == pygame.K_ESCAPE or ev.key == pygame.K_q: # Push ESC or Q key running = False screen.fill((40, 60, 200)) # clear screen # draw image x = 160 - 96 / 2 y = 120 - 96 / 2 screen.blit(img, (x, y), area=rects[index]) index = (index + 1) % len(rects) pygame.display.flip() clock.tick_busy_loop(60) cap = "Image Split - %5.2f FPS" % (clock.get_fps()) pygame.display.set_caption(cap) pygame.quit() sys.exit()