Ich habe ein Problem mit meinem Code, der erwähnt, dass 'Sprite' Objekt kein Attribut 'add_internal' hat. Es wird von der active_sprite_list.add
Variable ausgelöst. Ich wollte nur wissen, warum dieser Fehler auftritt und wie ich es lösen kann. Hier habe ich die Sprite-Klasse und die spezifische Zeile, in der der Fehler auftritt, eingefügt.Wie löst man add_internal Fehler in Pygame?
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)
* Dies ist der Code, der den current_level_update Fehler auslöst, wenn es eine Positions Argument erfordert ‚Selbst‘ Was soll ich diesen Fehler zu lösen tun, wird dieser Code nach der Sprite-Klasse platziert werden im vollen Version des Codes selbst. Klasse Level_01 (Stufe): „“ „Definition für Stufe 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)
Versuchen Sie, Ihren Code in ein [minimales, ausführbares Beispiel] (https://stackoverflow.com/help/mcve) umzuwandeln, das nur den Code enthält, der zum Reproduzieren des Fehlers erforderlich ist. Veröffentlichen Sie auch die vollständige Rückverfolgung. Stellen Sie sicher, dass Ihr Code korrekt eingerückt ist (Sie können ihn im Eingabefenster auswählen und Strg + K drücken). Der Schlüssel 'key = pygame.key.get_pressed()' und die folgenden Zeilen sollten nicht in der Ereignisschleife sein. – skrx