我创建了这个代码,以便在敌人死亡时滚动项目:
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中运行所有三个for循环。它按照这个顺序算作黄金,药水和钥匙。
我打印了黄金,药水和关键x坐标以查看它们的位置, 当我收集东西时,它会将所有x坐标改变为我刚刚拾取的东西。
我无法弄清楚为什么会发生这种情况,我花了很多时间进行反复试验。
尽管黄金,药水和钥匙是不同的实例,但当我遍历item_pickup时,它们仍然被视为同样的事情。
如果有人能让我知道问题所在,我将不胜感激。
答案 0 :(得分:0)
所以,我最终解决了这个问题。我将发布我的固定代码,以便如果其他人遇到此问题,那么他们可能会从中获得一些见解。
基本上,我将在下面显示,而不是将我作为单个项目传递的每个实例命名,而是将它们全部称为drop。我仍然需要一种方法来单独引用每个新实例,因为它们都被命名为drop right? 所以我在Pickups类中添加了一个名为label的新参数,并在创建新实例aka item drop时传递了一个特定的标签。
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
然后我修改了“on pickup”代码以引用标签为“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)