'Sprite'객체에 'add_internal'속성이 없다는 내용의 코드에 문제가 있습니다. 변수 active_sprite_list.add
에서 트리거됩니다. 이 오류가 발생하는 이유와 해결 방법을 알고 싶었습니다. 여기서 스프라이트 클래스와 에러가 발생하기 시작하는 특정 라인을 포함시켰다.파이 게임에서 add_internal 오류를 해결하는 방법은 무엇입니까?
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 오류를 유발하는 코드입니다 버전의 코드 자체. 클래스 Level_01 (레벨) : "" "레벨 1에 대한 정의" ""당신이 pygame.sprite.Group
에 추가하려는 경우
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)
오류를 재현하는 데 필요한 코드 만 포함 된 [최소, 실행 가능한 예] (https://stackoverflow.com/help/mcve)로 코드를 변환하십시오. 또한 전체 추적을 게시하십시오. 코드가 정확하게 들여 쓰기되었는지 확인하십시오 (제출 창에서 코드를 선택하고 Ctrl + K를 누르십시오). 'key = pygame.key.get_pressed()'항목과 아래의 줄은 이벤트 루프에 있어서는 안됩니다. – skrx