如何将图像放到精灵上 pygame

how to put an image onto a sprite pygame

我正在使用 python 在 pygame 中创建类似 pacman 的游戏。我想将珠宝图像分配给 jewel class。在迷宫中 'J' 代表 jewel。所以只是为了弄清楚我如何将图像分配给 Jewel Class 以便迷宫地图中的所有 J 都是该图像?

宝石的 class 是

class Jewel(object):
    """Class to hold Jewel sprite properties"""

    def __init__(self, pos):
        jewels.append(self)
        self.rect = pygame.Rect(pos[0], pos[1], 16, 16)

墙壁和迷宫的 class 是

class Wall(object):
    """Class to hold Wall sprite properties"""

    def __init__(self, pos):
        walls.append(self)
        self.rect = pygame.Rect(pos[0], pos[1], 16, 16)

walls = [] #List to contain walls
jewels = []#List to contain Jewels

#!-----------------------Maze Layout----------------------!

#Table used to create the level, where W = wall
level = [
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"W       J               W          JJJ           W",
"W                       W     W                  W",
"W    WWWW   WWWWW   WWWWW     W      WWWWWWWWWW  W",
"W    W          W       W     W           W      W",
"W    W      JJJJ        W   WWWWWWW       W      W",
"W    W   WW                    W          W      W",
"W    W    W     WWWW           W          W      W",
"W    WWW  W     W  W           W  W   WWWWW      W",
"W         W                    W  W              W",
"WWWW      WWWWWWWWWWWWWWWWWWWWWW                 W",
"W  W     WW                    W                 W",
"W       WW                     W WWWWWWWWWWWWWWWWW",
"W             WWW              W                 W",
"W   WW               WW  WWW   W                 W",
"W    W  WWW          WWWWWWW   WWWWWWWWWWWWWWWW  W",
"W    W    W   WWW       WW         W          W  W",
"W    WW   W                            W      W  W",
"W     W   W          WWWWWWWWWWWWWWWWWWWWWWWW W  W",
"WWWW      WWWWW      WW  WWW                  W  W",
"W  W      W                W  WWWWWWWWWWWWWWWWW  W",
"W  W   WWWW     W  W       W     W               W",
"W      W           W  WWW  W            W        W",
"W      WWWW        W  W    WWWWWWWWWWWWWWWWWWWWWWW",
"W  WW     W   WWW  W  W      JJJ                 W",
"W   W     W        W  W                     J    W",
"W   W   WWW           W    WWW                   W",
"W  WWW       WWWWWWWWWW      WWWW                W",
"W    W       W                  WWWWWWW    W     W",
"WW   W    WWWW        WWWWWW          WWWW WWWW  W",
"WJ   W                   W            W    W     W",
"W     W  J    WWW           WWWWW    W    W  WWWW",
"WWW   W        W      W               W WWWW     W",
"W W       WWWWWW   WWWW               W    W     W",
"W WWW     W        W         WWWWW    WWWW WWWWW W",
"W       WWW        W     W               W       W",
"W       W                W               W       W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]

#Draw the wall rects as shown in the table above
x = y = 0
for row in level:
    for column in row:
        if column == "W":
            Wall((x, y))
        if column == "J":
            Jewel((x, y))
        x += 16
    y += 16
    x = 0

#Draw walls and jewels
    for wall in walls:
        pygame.draw.rect(screen, (WHITE), wall.rect)

    for jewel in jewels:
        pygame.draw.rect(screen, (BLUE), jewel.rect)

只需将图像添加为对象的属性 - 例如,您可以在 __init__ 方法中加载它们,例如:

def __init__(self, pos):
    walls.append(self)
    self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
    self.image = pygame.image.load("/path/to/image_file.png")

然后,在您的游戏循环中,调用 blit 传递图像的 "screen" 方法(这是一个 pygame.Surface 对象):

for jewel in jewels:
    screen.blit(jewel.image, jewel.rect)

稍后当你对游戏有了更多的结构时,你应该把你的游戏对象放在专门的精灵组中,然后可以使用的 .image.rect 属性精灵在调用组 draw 方法时将它们 blit 到屏幕上。 (查看 http://www.pygame.org/docs/ref/sprite.html 上的文档),而不是直接调用 blit