Python 我如何让敌人死后丢钱?

Python 我如何让敌人死后丢钱?,python,pygame,sprite,Python,Pygame,Sprite,我想让我的游戏中的一些敌人在我射击他们时放弃“上升点”(以黄色小方块的形式升级点)。我尝试了一些不同的方法,但似乎不知道如何在敌人刚刚死亡的地方产生这些收集点。有人对我如何实现这一点有什么想法吗 高级班: class Up(pygame.sprite.Sprite): def __init__(self, color): super().__init__() self.image = pygame.Surface([5, 5]) self

我想让我的游戏中的一些敌人在我射击他们时放弃“上升点”(以黄色小方块的形式升级点)。我尝试了一些不同的方法,但似乎不知道如何在敌人刚刚死亡的地方产生这些收集点。有人对我如何实现这一点有什么想法吗

高级班:

class Up(pygame.sprite.Sprite):

    def __init__(self, color):
        super().__init__()
        self.image = pygame.Surface([5, 5])
        self.image.fill(color)
        self.rect = self.image.get_rect()
这是一个循环,当敌人被射杀并死亡时:

for bullet in bullet_list: #For each bullet:
        # Whenever a bullet collides with a zombie,
        block_hit_list = pygame.sprite.spritecollide(bullet, zombie_list, True)
        for i in block_hit_list:
            # Destroy the bullet and zombie and add to the score
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            score += 100
很抱歉没有发布我的全部代码,主游戏循环在底部:)

导入pygame
输入数学
随机输入
黑色=(0,0,0)
白色=(255,255,255)
绿色=(0,255,0)
红色=(255,0,0)
蓝色=(0,0255)
橙色=(255、119、0)
僵尸绿=(122172,34)
黄色=(255,255,0)
光标_x=100
光标_y=100
职业玩家(pygame.sprite.sprite):
定义初始化(自身,颜色):
super()。\uuuu init\uuuuu()
#Surface将创建一个具有给定宽度和高度的矩形
#下面的命令告诉它用这种颜色填充
self.image=pygame.Surface([15,15])
self.image.fill(颜色)
self.rect=self.image.get_rect()
#这定义了起始位置(x,y)
#不管精灵经过什么地方
自校正x=600
自校正y=300
#这是绘制时将移动的当前速度
self.change_x=0
self.change_y=0
self.walls=无
#定义玩家将如何移动
def移动(自身、x、y):
自我改变_x+=x
自我改变_y+=y
#更新信息,使屏幕显示玩家移动
def更新(自我):
self.rect.x+=self.change\u x
block\u hit\u list=pygame.sprite.spritecollide(self,self.walls,False)
对于block_命中列表中的block_:
如果self.change_x>0:
self.rect.right=block.rect.left
其他:
self.rect.left=block.rect.right
self.rect.y+=self.change\u y
block\u hit\u list=pygame.sprite.spritecollide(self,self.walls,False)
对于block_命中列表中的block_:
如果self.change_y>0:
self.rect.bottom=block.rect.top
其他:
self.rect.top=block.rect.bottom
职业敌人(pygame.sprite.sprite):
定义初始化(自身,颜色):
super()。\uuuu init\uuuuu()
self.image=pygame.Surface([20,20])
self.image.fill(颜色)
self.rect=self.image.get_rect()
self.pos\u x=self.rect.x=random.randrange(35,屏幕宽度-35)
self.pos_y=self.rect.y=random.randrange(35,屏幕高度-135)
#僵尸如何走向玩家
def更新(自我):
zombie_vec_x=self.rect.x-player.rect.x
zombie_vec_y=self.rect.y-player.rect.y
向量长度=math.sqrt(僵尸向量x**2+僵尸向量y**2)
如果self.rect.x!=player.rect.x和self.rect.y!=player.rect.y:
zombie_vec_x=(zombie_vec_x/vec_length)*1#这些数字决定
僵尸_vec_y=(僵尸_vec_y/vec_长度)*1#僵尸移动速度
self.pos_x-=僵尸_vec_x
self.pos_y-=僵尸_vec_y
self.rect.x=self.pos\ux
self.rect.y=self.pos_y
block\u hit\u list=pygame.sprite.spritecollide(self,sprites\u list,False)
对于block_命中列表中的block_:
如果self.rect.x>0:
self.rect.right=block.rect.left
elif self.rect.x<0:
self.rect.left=block.rect.right
如果自校正y>0:
self.rect.bottom=block.rect.top
其他:
self.rect.top=block.rect.bottom
类墙(pygame.sprite.sprite):
定义初始值(自身、颜色、x、y、宽度、高度):
super()。\uuuu init\uuuuu()
self.image=pygame.Surface([宽度,高度])
self.image.fill(颜色)
self.rect=self.image.get_rect()
self.rect.x=x
自校正y=y
类游标(pygame.sprite.sprite):
定义初始值(自身、宽度、高度):
self.groups=所有精灵列表
自身层=1
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.image=pygame.Surface([宽度,高度])
self.image.fill(红色)
self.rect=self.image.get_rect()
self.walls=无
#这将更新光标,使其随着您的移动而移动
#鼠标位置(在控制逻辑中定义)
def更新(自我):
self.rect.x=光标
self.rect.y=光标
类子弹(pygame.sprite.sprite):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.image=pygame.Surface([8,8])
self.image.fill(橙色)
self.rect=self.image.get_rect()
#而不是使用rect。位置,我们将使用位置变量
#计算位置。这是因为矩形。使用整数
#而变量可以有精确的浮点数。这将保持
#子弹的弹道与使用将军的弹道完全不同

#(四舍五入)整数你要做的是在你摧毁僵尸精灵之前,抓住它的位置并取出硬币。然后确保每当玩家控制精灵时,子弹在接触时“收集”硬币。我没有太多使用python,但主要代码如下所示:

def drawCoin():  
    zombieCoords = grabZombieCoords()   
    drawSprite(zombieCoords())
这基本上就是获取僵尸的坐标,摧毁它,然后将硬币放在僵尸最后一个已知的位置。
希望这有帮助

更改
Up
类以接受参数
pos
,并使用它设置起始位置:

class Up(pygame.sprite.Sprite):

    def __init__(self, color, pos):
        super().__init__()
        self.image = pygame.Surface([5, 5])
        self.image.fill(color)
        self.rect = self.image.get_rect(center=pos)
现在,当你击中一个僵尸时,用你刚刚杀死的僵尸的位置创建一个
向上的

for bullet in bullet_list:
    block_hit_list = pygame.sprite.spritecollide(bullet, zombie_list, True)
    for i in block_hit_list:
        # just use .kill() to remove a Sprite from all of its Groups
        # kill() may get called multiple times, but that does not hurt
        bullet.kill() 
        score += 100
        # create an Up for each killed zombie
        # takes two arguments: color and pos
        # we call .add() to add the Sprite immediately to
        # the all_sprites_list and the up_list
        Up((255, 0, 0), i.rect.center).add(all_sprites_list, up_list)

您没有显示其余的代码,但我想您可以调用
。绘制
。更新
所有精灵列表中的
,并创建一个名为
向上列表
,确保他们正在执行任务
for bullet in bullet_list:
    block_hit_list = pygame.sprite.spritecollide(bullet, zombie_list, True)
    for i in block_hit_list:
        # just use .kill() to remove a Sprite from all of its Groups
        # kill() may get called multiple times, but that does not hurt
        bullet.kill() 
        score += 100
        # create an Up for each killed zombie
        # takes two arguments: color and pos
        # we call .add() to add the Sprite immediately to
        # the all_sprites_list and the up_list
        Up((255, 0, 0), i.rect.center).add(all_sprites_list, up_list)