2017-06-13 5 views
0

私はゲームのために敵を召喚しようとしています。スプライトグループを更新すればそれが可能だと言われました。何らかの理由で、敵はまったく産まない。何かエラーや何かを得ることはありません。ここに私のクラスです:私はそれとすべてを呼び出すところスプライトの表示方法がわかりません

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.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() 
     spritegroup.draw(screen) 
     pygame.display.flip() 

は、どのように私はスプライトを呼び出す/表示していますか?私の乱雑で不便なコードをお詫び申し上げます。私はクラスとスプライトを使うのが初めてです。

答えて

0

spritegroup.update()を呼び出すと、新しいスプライトが生成されず、すべてのスプライトのupdateメソッドが呼び出されます。 Spawnスプライトサブクラスにはupdateメソッドがないため、spritegroup.update()は実際にプログラムに影響しません。

あなたがここで行ったようにインスタンスを作成し、スプライトのグループに追加することで、新しいスプライトを起動することができます:スプライトとスプライトグループについて

spawn = Spawn(600, 200) 
spritegroup.add(spawn) 
+0

詳しい情報は、[プログラムのアーケードゲーム](HTTPで見つけることができます: //programarcadegames.com/index.php?chapter=introduction_to_sprites&lang=de#section_13)(前の章はクラスに関するものです)。 – skrx

+0

しかし私はコードでそれを行い、スプライトは生成されませんでした。右? – Qartx

+0

あなたのスプライトクラスは正しく動作するためには 'self.image'(pygame.Surface)属性と' self.rect'(pygame.Rect)属性も必要です。私はあなたの例が完全ではなかったので、私はそれを見落とした、私はそれをテストすることができませんでした。質問のコードを編集し、[最小で完全で検証可能な例](https://stackoverflow.com/help/mcve)を提供する必要があります。上にリンクしたチュートリアルを読んでください。基本を教えてくれるからです。 – skrx

関連する問題