解决Pygame内使用精灵实现人物角色上下左右移动

以知乎上“使用Sprite实现移动动画(Pygame中的精灵类)”为例子,处理如下图片实现人物的移动,请问有内行可以详细解释一下其中的前后过程操作原理吗?
img

import pygame
class GameSprite(pygame.sprite.Sprite):
    def __init__ (self, filename, frames = 1)

        pygame.sprite.Sprite.__init__(self)
        self.images = []
        img = pygame.image.load(filename)
        self.original_width = img.get_width() // frames
        self.original_height = img.get_height()
        frame_surface = pygame.Surface([self.original_width, self.original_height])
        x = 0
        for frame_no in range(frames):
            frame_surface = pygame.Surface([self.original_width, self.original_height])
            frame_surface.blit(img, [x,0])
            self.images.append(frame_surface.copy())
            x -= self.original_width
        self.image = self.images[0]
        self.current_index = 0
        self.rect=self.image.get_rect()
    def move(self, pos_X, pos_Y):
        self.rect.center = [pos_X, pos_Y]
    def change_image(self, index):
        self.current_index = index
        self.image = self.images[index]
        oldcenter = self.rect.center
        self.rect = self.image.get_rect()
        self.rect.center = oldcenter

在类中定义了移动方法move,在主循环内,如果按下按键则调用要操作精灵对象的move方法,并传入按键对应的方向,在move内部会通过更改精灵类rect在画面上的相对位置并刷新screen来达到移动的效果
代码应该没贴全,理论上外部应该还有针对方向切换贴图的部分代码
有帮助望采纳

动画部分见
pygame 学习笔记(8)精灵动画的实现:子弹的爆炸效果_白杨木屋-CSDN博客 本文内容subsurface() 子 surface的学习实现一个精灵动画Note:本文内容基于上一小节,如果你想要跟着文中的内容一步一步测试和实现代码,需要下载上一节文末提供的工程文件至少是图片资源,之后你就可以按本文的步骤测试了。效果演示在上一节中,我们只是让坦克移动了起来,但不是真正的动画,因为我们只是对一个图片进行旋转。这一节我们介绍如何播放一个动画,本节我们需要的素材是... https://blog.csdn.net/qq_17351161/article/details/89297326