sprite.Group()中的对象不像它们应该的那样表现为唯一对象

时间:2014-12-29 06:06:57

标签: python for-loop pygame sprite

下面是令人不安的代码,快速细分。

if player.attacked == True and delay >= 60: #if it's monster's turn plus a built in delay so everything isn't instant
    for enemy in enemies_list: #iterating the all the enemies in the sprite group
        if enemy.attack_rdy == True and enemy.health >0 and delay ==60: #if THAT particular monster hasn't attacked, isn't dead, and the delay has been long enough.
            player.hit.play() #play the player's damaged sound
            damage = random.randrange(each.attack[0],each.attack[1]+1)-player.armor #random attack roll of a tuple assigned to the enemy class
            player.health -= damage 
            enemy.attack_rdy = False #Sets THIS particular enemy so that he may not attack until it's his turn again
            player.damaged = True #Just triggers an animation on the player
            each.attack_anim = 1 #triggers an animation on the enemy
            break #stops the for loop so that everything doesn't happen at once and relies on the delay int.

问题: 该迭代正常工作,因为根据攻击他的敌人的数量,玩家被准确地攻击了正确的次数。出于某些奇怪的原因,同样的敌人将会攻击所有这些时间。当攻击敌人死亡时,另一个人只是接管他作为攻击者的任务。我对它没有任何意义。

1 个答案:

答案 0 :(得分:0)

以真正的骨头方式,在盯着我编写代码几个小时之后,我发现我的错误只是在发布后的一瞬间。这真的只是一个愚蠢的错误。

each.attack_anim = 1

应该阅读

enemy.attack_anim = 1

修正错误后,代码按设计运行。