2017-06-30 18 views
1

スプライトと特定の行と列を画面に表示する方法を知りました。ここに私のコードは、これまでのところです:Pythonのスクリーンにスプライトのマトリックスを追加するには

rows = 3 
cols = 6 
choices = [Enemy(), Enemy2()] 

def create_enemies(): 
matrix = [[np.random.choice(choices) for i in range(cols)] for j in range(rows)] 
create_enemies() 

私は画面にスプライトでこの行列を描画する方法を知ってはいけない点が異なります。どんな助け? ここに私の敵のクラスでもある:

class Enemy(pygame.sprite.Sprite): 
    speed = 2 
    def __init__(self): 
     super().__init__() 
     self.image = pygame.Surface([40, 40]) 
     self.image = pygame.image.load("images/e1.png").convert_alpha() 
     self.rect = self.image.get_rect(center=(40,40)) 
     self.rect.x = 0 
     self.rect.y = 0 
    def update(self): 
     self.rect.y += 1 

class Enemy2(pygame.sprite.Sprite): 
    speed = 2 
    def __init__(self): 
     super().__init__() 
     self.image = pygame.Surface([40, 40]) 
     self.image = pygame.image.load("images/e2.png").convert_alpha() 
     self.rect = self.image.get_rect(center=(40,40)) 
     self.rect.x = 0 
     self.rect.y = 0 
    def update(self): 
     self.rect.y += 1 

答えて

1

pygame.sprite.Groupインスタンスを作成し、それにマトリックスを追加します。

all_sprites = pygame.sprite.Group() 
all_sprites.add(matrix) 

を更新し、ちょうどall_sprites.update()を呼び出し、すべてのスプライトが含まれて描画するには、(のupdateメソッドを呼び出しますスプライト)とall_sprites.draw(screen)をメインループに入れます。あなたの行列は、あなたのchoicesリストに2つの敵のインスタンスへの参照のみが含まれてい


注意。ユニークなスプライトインスタンスが必要な場合は、次のようにコードを変更します。

choices = [Enemy, Enemy2] # Contains references to the classes now. 
# Create new instances in the list comprehension. 
matrix = [[random.choice(choices)() for i in range(cols)] for j in range(rows)] 

numpyを使用する理由はないようです。

+0

[こちらはスプライト/スプライトグループのチュートリアルです。](http://programarcadegames.com/index.php?chapter=introduction_to_sprites&lang=de#section_13) – skrx

関連する問題