2016-12-03 6 views
0

私は、複数のスプライトで衝突チェックを処理しようとしていますが、それはプレイヤーキャラクターに対してチェックしています。関連するコードはEnemyクラスは画像で表現される新しいスプライトを作成し、Characterクラスはプレイヤーが制御できるスプライト以外は似ています。ここに私がプロジェクトから抜粋した関連コードがあります。同じタイプの複数のスプライトの衝突検出のチェックはどうすれば処理できますか?

self.all_sprites_list = pygame.sprite.Group() 
    sprite = Character(warrior, (500, 500), (66, 66)) 
    enemies = [] 
    for i in range(10): 
     enemy = Enemy("evilwizard") 
     enemies.append(enemy) 
     self.all_sprites_list.add(enemy) 
    self.all_sprites_list.add(sprite) 

class Enemy(pygame.sprite.Sprite): 
# This class represents the types of an enemy possible to be rendered to the scene 

def __init__(self, enemy_type): 
    super().__init__() # Call sprite constructor 
    # Pass in the type of enemy, x/y pos, and width/height (64x64) 
    self.image = pygame.Surface([76, 76]) 
    self.image.fill(WHITE) 
    self.image.set_colorkey(WHITE) 
    self.rect = self.image.get_rect() 
    self.rect.x = random.randrange(10, 1150) # random start 
    self.rect.y = random.randrange(10, 590) # random start 
    self.speed = 2 
    self.move = [None, None] # x-y coordinates to move to 
    self.image = pygame.image.load(FILE_PATH_ENEMY + enemy_type + ".png").convert_alpha() 
    self.direction = None # direction to move the sprite` 

class Character(pygame.sprite.Sprite): 
    def __init__(self, role, position, dimensions): 
    """ 
    :param role: role instance giving character attributes 
    :param position: (x, y) position on screen 
    :param dimensions: dimensions of the sprite for creating image 
    """ 
    super().__init__() 
    # Call the sprite constructor 
    # Pass in the type of the character, and its x and y position, width and height. 
    # Set the background color and set it to be transparent. 
    self.image = pygame.Surface(dimensions) 
    self.image.fill(WHITE) 
    self.image.set_colorkey(WHITE) 
    self.image = pygame.image.load(FILE_PATH_CHAR + role.title + ".png").convert_alpha() 

    # Draw the character itself 
    # position is the tuple (x, y) 
    self.rect = self.image.get_rect() 
    self.rect.x, self.rect.y = position 

    self.attack = role.attack 
    self.health = role.health 
    self.title = role.title 
+0

のためのドキュメントを参照してください() 'self.image = pygame.Surface()'を使用する意味がありません – furas

+0

スプライトが動くたびに、その新しい場所が他のすべてのスプライトと照合されて動きが発生したかどうかを確認する必要があります結託が起こる。 – martineau

+0

@martineau iveは 'Character'クラスのような移動メソッドを持っていて、' self.rect.x'と 'self.rect.y'が動きの許可の前に壁の境界に近いかどうかをチェックし、敵クラスにはランダムな動きを生み出すローミング方法がありますが、その領域でそれを行う必要がありますか? – timoxazero

答えて

0

pygameのは、それは衝突をチェックするためにも、他の機能を備えていpygame.sprite.Sprite()pygame.sprite.Group()

間の衝突をチェックするpygame.sprite.spritecollideを持っている - あなたは `self.image = pygame.image.loadを使用する場合Sprite

関連する問題