2017-06-13 74 views
0

我試圖做一個遊戲。就像其他射擊遊戲一樣,敵人在射擊時應該消失。顯然,你不能殺死表面,但你可以殺死精靈。所以我試圖加載我的圖像作爲精靈,但我得到一個奇怪的錯誤。我創建該類,爲其應用一個變量,爲pygame.sprite.Group創建一個變量,並將該類變量添加到該組中。當我嘗試更新組時,它說"'NoneType' object has no attribute 'update'"。下面是類代碼:沒有類型的對象沒有屬性更新

class Spawn(pygame.sprite.Sprite): 
    def __init__(self,primaryx,primaryy): 
     pygame.sprite.Sprite.__init__(self) 
     global directionM 
     self.directionM=directionM 
     x1=random.randint(100,400) 
     y1=random.randint(100,400) 
     self.x1=x1 
     self.y1=y1 
     self.primaryx=primaryx 
     self.primaryy=primaryy 
    def AIPrototype(self): 
     minionup=pygame.image.load("Alien.png").convert_alpha() 
     miniondown=pygame.image.load("Aliendown.png").convert_alpha() 
     minionleft=pygame.image.load("Alienleft.png").convert_alpha() 
     minionright=pygame.image.load("Alienright.png").convert_alpha() 
     global x,y,posx,posy 
     seperate=random.randint(1,1000) 
     screen.blit(self.directionM,(self.primaryx,self.primaryy)) 
     if seperate==2: 
      self.primaryx=x+100 
     if seperate==20: 
      self.primaryx=x-100 
     if seperate==150: 
      self.primaryy=y+100 
     if seperate==200: 
      self.primaryy=y-100 
     self.x1=self.primaryx 
     self.y1=self.primaryy 
     if self.x1<x: 
      xspeed1=1 
      slopex1=x-self.x1 
     if self.x1>x: 
      xspeed1=-1 
      slopex1=self.x1-x 
     if self.y1<y: 
      yspeed1=1 
      slopey1=y-self.y1 
     if self.y1>y: 
      yspeed1=-1 
      slopey1=self.y1-y  
    # 
     hypo1=((slopex1**2)+(slopey1**2))**0.5 
     speedmark1=hypo1/1 
     speedy1=slopey1/speedmark1 
     speedx1=slopex1/speedmark1 
     movex1=speedx1 
     movey1=speedy1 
     if self.x1<=640 and self.x1>=0: 
      if self.x1>x: 
       self.x1+=xspeed1*movex1 
       if self.x1<x: 
        xspeed1=0 
     if self.y1<=480 and self.x1>=0: 
      if self.y1>y: 
       self.y1+=yspeed1*movey1 
       if self.y1<y: 
        yspeed1=0 
     if self.x1<=640 and self.x1>=0: 
      if self.x1<x: 
       self.x1+=xspeed1*movex1 
       if self.x1>x: 
        xspeed1=0 
     if self.y1<=480 and self.x1>=0: 
      if self.y1<y: 
       self.y1+=yspeed1*movey1 
       if self.y1>y: 
        yspeed1=0 
    # 
     if self.x1>640: 
      self.x1=640 
     if self.x1<0: 
      self.x1=0 
     if self.y1>480: 
      self.y1=480 
     if self.y1<0: 
      self.y1=0 
     if self.y1>=posy-20 and self.y1<=posy+20 and self.x1>=x-20 and self.x1<=x+20: 
      Spawn.kill() 
     self.primaryx=self.x1 
     self.primaryy=self.y1 

而這正是遊戲循環:

spritegroup=pygame.sprite.Group() 
spawn=Spawn(600,200) 
spritegroup=spritegroup.add(spawn) 
clock = pygame.time.Clock() 
keepGoing = True   

try: 
    while keepGoing: 
     clock.tick(60) 
     screen.fill(THECOLORS['red']) 
     char()#start 
     x+1 
     posxlist.append(x) 
     posylist.append(y) 
     spritegroup.update() 
     pygame.display.flip() 

我意識到,我的代碼是非常的混亂,效率低下,而且我對此感到非常抱歉。我完全不熟悉使用類和精靈。

回答

2

spritegroup.add(spawn)返回None。不要將其指定回spritegroup,該操作將對象更改爲

換句話說,這樣做,而不是:

spritegroup = pygame.sprite.Group() 
spawn = Spawn(600,200) 
spritegroup.add(spawn) 

spritegroup.Group.add() documentation

精靈添加到這個集團
add(*sprites) - >None

+0

謝謝!我的錯誤消失了。但我遇到了另一個問題。有人告訴我說,更新團隊會召喚班級,並展示敵人,但不會發生。 – Qartx

+0

@Qartx對不起,我對pygame不夠了解,以幫助你進一步。 –

+0

感謝您的幫助。我只會爲此發佈另一個問題。 – Qartx

相關問題