第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以正確的順序錯誤地移除精靈組敵人

以正確的順序錯誤地移除精靈組敵人

墨色風(fēng)雨 2022-12-20 09:40:42
我有 2 個精靈組,一組用于敵人,一組用于玩家武器。我如何設(shè)置敵人 sprite 組的示例......(武器 sprite 組以相同的方式完成)class Bat(pygame.sprite.Sprite):    def __init__(self, bat_x, bat_y, bat_image, bat_health, bat_immune, const, dash_counter, dash, dash_time, fly_count, fly_time):        pygame.sprite.Sprite.__init__(self)        self.bat_health = bat_health        self.bat_immune = bat_immune        self.const = const        self.dash_counter = dash_counter        self.dash = dash        self.dash_time = dash_time        self.fly_count = fly_count        self.fly_time = fly_time        self.image = bat_image        self.rect = self.image.get_rect()        self.mask = pygame.mask.from_surface(self.image)        self.rect.topleft = (bat_x, bat_y)        self.bat_x = bat_x        self.bat_y = bat_y    def update(self):        if pygame.sprite.groupcollide(all_bats, all_sword, False, True):            self.bat_health -= 1         if self.bat_health == 0:            self.kill()        ... #Irrelevantall_bats = pygame.sprite.Group()def bat_create():    bat_x = r_x*40    bat_y = r_y*40    bat_health = 5    bat_immune = False    const = 3    dash_counter = 0    dash = False    dash_time = 0    fly_count = 0    fly_time = 0    new_bat = Bat(bat_x, bat_y, bat_image, bat_health, bat_immune, const, dash_counter, dash, dash_time, fly_count, fly_time)    all_bats.add(new_bat)當(dāng)玩家的武器擊中蝙蝠時,無論擊中的是哪只蝙蝠,在擊中 5 次后第一只生成的蝙蝠將被殺死。我不知道為什么會這樣。
查看完整描述

1 回答

?
繁花如伊

TA貢獻(xiàn)2012條經(jīng)驗 獲得超12個贊

看起來這是問題所在:


def update(self):

    if pygame.sprite.groupcollide(all_bats, all_sword, False, True):

        self.bat_health -= 1 

groupcollide返回碰撞列表。上面的代碼正在檢查all_bats,all_swords所以一切都 Vs 一切。并且if ( non-empty-list )總是返回True。


所以如果發(fā)生任何蝙蝠+劍的碰撞,這個精靈的生命值就會減少。每次任何蝙蝠碰撞時,這可能都會減少每只蝙蝠的數(shù)量。因此,創(chuàng)建的第一個項目將首先達(dá)到零。self.bat_health


您需要檢查“這個精靈”是否是碰撞的一部分。這是一個相當(dāng)簡單的改變:


def update(self):

    hit_list = pygame.sprite.groupcollide(all_bats, all_sword, False, True):

    if ( self in hit_list ):

        self.bat_health -= 1 

但是groupcollide在每只蝙蝠的update(). 也許將groupcollide調(diào)用移動到.update()函數(shù)之外的某個地方,并且只執(zhí)行一次這些計算。將碰撞結(jié)果傳遞給 sprite-update 函數(shù)等。


查看完整回答
反對 回復(fù) 2022-12-20
  • 1 回答
  • 0 關(guān)注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號