I recommend to use pygame.sprite.Sprite
and pygame.sprite.Group
:
Create a class derived from pygame.sprite.Sprite
:
class MySprite(pygame.sprite.Sprite):
def __init__(self, image, pos_x, pos_y):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = (pos_x, pos_y)
Load the image
image = pygame.transform.scale(pygame.image.load('pawn.png'), (100,100))
Create a list of sprites
imageList = [MySprite(image, x_pos*100, 100) for x_pos in range(0,8,1)]
and create a sprite group:
group = pygame.sprite.Group(imageList)
The sprites of a group can be drawn by .draw
(screen
is the surface created by pygame.display.set_mode()
):
group.draw(screen)
The position of the sprite can be changed by changing the position of the .rect
property (see pygame.Rect
).
e.g.
imageList[0].rect = imageList[0].rect.move(move_x, move_y)
Of course, the movement can be done in a method of class MySprite
:
e.g.
class MySprite(pygame.sprite.Sprite):
# [...]
def move(self, move_x, move_y):
self.rect = self.rect.move(move_x, move_y)
imageList[1].move(0, 100)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…