如何解决pygame中的add_internal错误?

问题描述 投票:1回答:1

我的代码存在问题,提到'Sprite'对象没有属性'add_internal'。它是从active_sprite_list.add变量触发的。我只是想知道为什么会出现这种错误以及如何解决它。在这里,我已经包含了sprite类和错误开始发生的特定行。

class Sprite(object):
    def __init__(self, pos):
        super(Sprite, self).__init__()  # platform
        self.width = width
        self.height = height
        self.platform = pygame.Surface((width, height))
        self.platform.fill(WHITE)
        # set a reference to the image rect
        self.rect = self.platform.get_rect()
        # Assign the global image to `self.image`.
        self.image = sprite_image

        # Create a rect which will be used as blit
        # position and for the collision detection.
        self.rect = self.image.get_rect()
        # Set the rect's center to the passed `pos`.
        self.rect.center = pos
        self._vx = 0
        self._vy = 0
        # Assign the pos also to these attributes.
        self._spritex = pos[0]
        self._spritey = pos[1]
        # set of sprites sprite can bump against
        self.level = None


sprite = Sprite([400, 550])
level_list = []
level_list.append(Level_01)

# Set the current level
current_level_no = 0
current_level = level_list[current_level_no]

active_sprite_list = pygame.sprite.Group()
sprite.level = current_level

sprite.rect.x = 340
sprite.rect.y = H - sprite.rect.height
active_sprite_list.add(sprite)

# Loop until the user clicks the close button.
done = False

while not done:
    events()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        key = pygame.key.get_pressed()
        if key == [pygame.K_RIGHT]:
            sprite.go_right()
        if key == [pygame.K_LEFT]:
            sprite.go_left()
        if key == [pygame.K_UP]:
            sprite.jump()
            # If the player gets near the right side, shift the world left (-x)
        if sprite.rect.right > W:
            sprite.rect.right = W

            # If the player gets near the left side, shift the world right (+x)
        if sprite.rect.left < 0:
            sprite.rect.left = 0

        current_level.draw(DS)
        active_sprite_list.draw(DS)
    # Call the `update` method of the sprite to move it.

    sprite.update()
    # Update the player.
    active_sprite_list.update()

    # Update items in the level
    current_level.update()

    DS.fill(BLACK)

    # Blit the sprite's image at the sprite's rect.topleft position.
    DS.blit(sprite.image, sprite.rect)

    pygame.display.flip()

    clock.tick(FPS)

*这是触发current_level_update错误的代码,它需要一个位置参数'self'我该如何解决这个错误,这个代码将放在代码本身的完整版本的sprite类之后。 class Level_01(等级):“”“等级1的定义。”“”

def __init__(self):
    """ Create level 1. """

    # Call the parent constructor
    Level.__init__(self, Sprite)

    # Array with width, height, x, and y of platform
    level = [[210, 70, 500, 500],
             [210, 70, 200, 400],
             [210, 70, 600, 300],
             ]

    # Go through the array above and add platforms
    for p in level:
        block = platform(p[0], p[1])
        block.rect.x = p[2]
        block.rect.y = p[3]
        block.player = self.sprite
        self.platform_list.add(block)
python pygame
1个回答
2
投票

如果你想将它添加到Sprite,你的pygame.sprite.Sprite类必须继承pygame.sprite.Group

class Sprite(pygame.sprite.Sprite):
    def __init__(self, pos):
        # Don't forget to call the __init__ method of the parent class.
        super(Sprite, self).__init__()

这是一个完整的例子:

import pygame

pygame.init()

class Sprite(pygame.sprite.Sprite):
    def __init__(self, pos):
        super(Sprite, self).__init__()
        self.image = pygame.Surface((30, 50))
        self.image.fill((40, 60, 140))
        self.rect = self.image.get_rect()
        self.rect.center = pos
        self._vx = 3  # The x-velocity.
        self._spritex = pos[0]
        self._spritey = pos[1]

    def go_right(self):
        # Update the _spritex position first and then the rect.
        self._spritex += self._vx
        self.rect.centerx = self._spritex

    def go_left(self):
        self._spritex -= self._vx
        self.rect.centerx = self._spritex


BLACK = pygame.Color('black')
clock = pygame.time.Clock()
display = pygame.display.set_mode((800, 600))
sprite = Sprite([340, 550])
active_sprite_list = pygame.sprite.Group()
active_sprite_list.add(sprite)

done = False

while not done:
    # Handle events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # get_pressed() returns a list with the keys
    # that are currently held down.
    key = pygame.key.get_pressed()
    # Use pygame.K_RIGHT etc. as the index.
    if key[pygame.K_RIGHT]:
        sprite.go_right()
    elif key[pygame.K_LEFT]:
        sprite.go_left()
    if key[pygame.K_UP]:
        sprite.jump()  # Not implemented.

    # Update the game.
    # This calls the update methods of all contained sprites.
    active_sprite_list.update()

    # Draw everything.
    display.fill(BLACK)
    # This blits the images of all sprites at their rect.topleft coords.
    active_sprite_list.draw(display)

    pygame.display.flip()
    clock.tick(30)

您也可以使用事件循环检查是否生成了pygame.key.get_pressed()事件,如果它是pygame.KEYDOWNpygame.K_LEFT,然后将精灵的K_RIGHT属性设置为所需的值,而不是_vx。然后可以在精灵的update方法中更新位置。

© www.soinside.com 2019 - 2024. All rights reserved.