2017-07-10 10 views
0

敵が死ぬとき、私はアイテムのドロップをロールバックするこのコードを作成しました:すべてのアイテムRPG

if len(current_enemy_group.sprites()) > 0: 
     for goblin in current_enemy_group: 
      if goblin.health <= 0: 
       current_goblin_x = goblin.rect.x 
       current_goblin_y = goblin.rect.y 
       attack = False 
       current_enemy_group.remove(goblin) 
       all_sprites.remove(spell1) 
       drop = random.randrange(0, 2, 1) 
       if key_drop == False: 
        key_drop = True 
        key = Pickups(keyIMG, 35, 11) 
        current_pickups_group.add(key) 
        key.rect.x = current_goblin_x 
        key.rect.y = current_goblin_y 
        print("KEY DROPPED") 

       elif drop == 1: 
        gold = Pickups(goldIMG, 24, 25) 
        current_pickups_group.add(gold) 
        gold.rect.x = current_goblin_x 
        gold.rect.y = current_goblin_y 


       elif drop == 0: 
        print ("Item Drop") 
        ##WHAT TYPE OF ITEM 
        item_type = random.randrange(0, 3, 1) 
        if item_type == 0: 
         print("Potion Drop") 
         potion = Pickups(potionIMG, 9, 9) 
         current_pickups_group.add(potion) 
         potion.rect.x = current_goblin_x 
         potion.rect.y = current_goblin_y 
    elif item_type == 1: 
         print("Armor Drop") 
         if cloth_armor == False: 
          player.armor = 15 
          cloth_armor = True 
        elif item_type == 2: 
         print ("Weapon Drop") 
         item_rarity = random.randrange(0, 100, 1) + magic_find 
         if item_rarity >= 90: 
          print("ULTIMATE WEAPON AQCUIRED") 
          player.damage =1000000 


       goblin.health = 2000 

を私は衝突のためにこれを追加しました検出/アイテムピックアップ:

item_pickup = pygame.sprite.spritecollide(player, current_pickups_group, False) 
    if enemy_alive == True: 
     print (gold.rect.x, potion.rect.x, key.rect.x) 

    if item_pickup: 
     for gold in item_pickup: 
      player.gold += 1000 
      print("You have acquired 1000 gold") 
      current_pickups_group.remove(gold) 
      current_pickups_group.update 


     for potion in item_pickup: 
      player.potion_count += 1 
      print("+1 potions") 
      current_pickups_group.remove(potion) 
      current_pickups_group.update 


     for key in item_pickup: 
      have_key = True 
      print("You now have the Gate Key") 
      current_pickups_group.remove(key) 
      current_pickups_group.update 
      sound_KEYGRAB.play(loops=0, maxtime=0) 

したがって、プレーヤーのスプライトがcurrent_pickups_groupスプライトと衝突するたびに(私はそれを収集するために項目に入る)、item_pickup内の3つのループすべてを実行します。それは金、薬、そしてその順序の鍵とみなされます。

私はゴールド、ポーション、キーx座標を印刷して、どこにあるのかを確認しました。 と私が何かを集めたとき、私はちょうど私が拾ったものにすべてのx座標を変更します。

私はこれがなぜ起こっているのか理解できず、多くの時間を試行錯誤して過ごしました。

gold、potion、およびkeyは異なるインスタンスでも、item_pickupを反復処理するとき、同じものとして処理され続けます。

誰かが私に問題の原因を教えてもらえると大変感謝しています。

答えて

0

私はこの問題を解決しました。誰かがこの問題に遭遇した場合、私は固定コードを掲示して、そこからいくつかの洞察を得ることができるかもしれません。

基本的には、以下に示すように、個々のアイテムとして渡した各インスタンスに名前を付ける代わりに、それらをすべてドロップしました。私はまだそれぞれの新しいインスタンスを個別に参照する方法が必要でした。 私は、ラベルと呼ばれるピックアップクラス内に新しい引数を追加し、新しいインスタンス別名アイテムドロップの作成時に特定のラベルを通過しました。

if len(current_enemy_group.sprites()) > 0: 

     for goblin in current_enemy_group: 

      if goblin.health <= 0: 
       current_goblin_x = goblin.rect.x 
       current_goblin_y = goblin.rect.y 
       attack = False 
       current_enemy_group.remove(goblin) 
       all_sprites.remove(spell1) 
       drop = random.randrange(0, 2, 1) 
       if key_drop == False: 
        key_drop = True 
    drop = Pickups(keyIMG, 35, 11, "key") 
    current_pickups_group.add(drop) 
        drop.rect.x = current_goblin_x 
        drop.rect.y = current_goblin_y 
        print("KEY DROPPED") 

       elif drop == 1: 

        drop = Pickups(goldIMG, 24, 25, "gold") 

        current_pickups_group.add(drop) 
        drop.rect.x = current_goblin_x 
        drop.rect.y = current_goblin_y 


       elif drop == 0: 
        print ("Item Drop") 
        ##WHAT TYPE OF ITEM 
        item_type = random.randrange(0, 3, 1) 
        if item_type == 0: 
         print("Potion Drop") 
         drop = Pickups(potionIMG, 9, 9, "potion") 

         current_pickups_group.add(drop) 
         drop.rect.x = current_goblin_x 
         drop.rect.y = current_goblin_y 

その後、私はちょうどラベルを持つすべてのインスタンスを参照するために "ピックアップの" コード "X" を改変。

 item_pickup = pygame.sprite.spritecollide(player, current_pickups_group, False) 

    if item_pickup: 
     for drop in item_pickup: 
  if drop.label == "gold": 
   player.gold += 1000 
       print("You have acquired 1000 gold") 
       current_pickups_group.remove(drop) 


      if drop.label == "potion": 
       player.potion_count += 1 
       print("+1 potions") 
       current_pickups_group.remove(drop) 

      if drop.label == "key": 
       have_key = True 
       print("You now have the Gate Key") 
       current_pickups_group.remove(drop) 
       sound_KEYGRAB.play(loops=0, maxtime=0) 
関連する問題