2015-12-09 79 views
0

我正在使用pygame製作一個小遊戲,並且想要刪除已從屏幕消失的對象。我想做的是如何從本身中刪除對象實例

class Projectile(pygame.sprite.Sprite): 
    def __init__(self, path, move_speed, x_pos, y_pos): 
     super().__init__()   
     self.rect = self.image.get_rect() 

    def move(self): 
     if self.rect.y < 0: 
      del self 

但這並不起作用。我該怎麼辦?

+0

你在哪裏存儲對象? – jwodder

+0

@jwodder在一個組中,我後來從中得到一個精靈列表並迭代它。我現在解決了我的問題。 – ca1ek

回答

0

好吧,我找到了解決辦法。

在我的遊戲循環我做

for projectile in projectile_group.sprites(): 
    if projectile.rect.y < 0: 
     projectile.kill() 

,它會殺死那些關閉屏幕的任何拋射。