今天的內容我們要讓玩家的子彈可以擊殺怪物,並讓怪物的子彈,可以傷害玩家。
class Player(pygame.sprite.Sprite):
def __init__(self, construction: dict, **kwargs):
self.shield = 100
self.lives = 3
self.score = 0
class Player(pygame.sprite.Sprite):
def update(self, command: dict) -> None:
if self.shield <= 0:
self.lives -= 1
if self.lives <= 0:
self.is_alive = False
self.shield = 100
class BattleMode:
def update(self, command: dict) -> None:
self.handle_collisions()
class BattleMode:
def handle_collisions(self):
for mob in self.mobs:
if isinstance(mob, Mob):
bullets = pygame.sprite.spritecollide(mob, self.player_1P.bullets, True, pygame.sprite.collide_rect_ratio(0.8))
if bullets:
mob.kill()
self.player_1P.score += 10
bullets = pygame.sprite.spritecollide(mob, self.player_2P.bullets, True, pygame.sprite.collide_rect_ratio(0.8))
if bullets:
mob.kill()
self.player_2P.score += 10
hits_dict = pygame.sprite.groupcollide(self.players, mob.bullets, False, True, pygame.sprite.collide_rect_ratio(0.8))
for player, bullet in hits_dict.items():
if isinstance(player, Player):
player.shield -= len(bullet) * 10
今日檔案更新有: