2017-12-31 23 views
1

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) 
+0

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

Antwort

2

Ihre Sprite Klasse muss von pygame.sprite.Sprite erben, wenn Sie es zu einem pygame.sprite.Group hinzufügen möchten.

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__() 

Hier ist ein komplettes Beispiel:

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) 

Statt pygame.key.get_pressed() Sie auch die Ereignisschleife verwenden könnten und prüfen, ob ein pygame.KEYDOWN Ereignis erzeugt wurde, und wenn ist es pygame.K_LEFT oder K_RIGHT, und dann Setzen Sie das Attribut _vx des Sprites auf den gewünschten Wert. Die Position könnte dann in der update-Methode des Sprites aktualisiert werden.

+0

Danke für die Hilfe, sollte ich jetzt die Sachen von der key = pygame.get_pressed() in die Klasse verschieben, wo ich die Bewegung definiert habe? Auch ich habe Ihre Änderungen übernommen und es kommt jetzt mit der Fehler Zeile 218, in current_level.update() TypeError: update() fehlt 1 benötigt positional Argument: 'self' –

+0

es kommt jetzt mit dem Fehler Zeile 218, in current_level.update() TypeError: update() fehlt 1 erforderliches Positionsargument: 'self' Ich füge dem Fehler die entsprechende Klasse hinzu, um zu sehen, ob Sie helfen könnten? –

+0

Ich denke, es wäre besser, eine neue Frage zu stellen, da dies nicht mit der ursprünglichen Frage zusammenhängt. Versuchen Sie, Ihren Code in ein [minimales, vollständiges und überprüfbares Beispiel] (https://stackoverflow.com/help/mcve) umzuwandeln, das wir kopieren und ausführen können. – skrx