优化杨桃子弹

This commit is contained in:
星外之神 2022-04-24 15:17:09 +08:00
parent 4c9fd6506f
commit c140a376ba
2 changed files with 17 additions and 24 deletions

View File

@ -123,6 +123,7 @@ class StarBullet(Bullet):
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.x = x self.rect.x = x
self.rect.y = start_y self.rect.y = start_y
_, self.map_y = self.level.map.getMapIndex(self.rect.x, self.rect.centery)
self.direction = direction self.direction = direction
self.damage = damage self.damage = damage
self.state = c.FLY self.state = c.FLY
@ -168,6 +169,7 @@ class StarBullet(Bullet):
self.rect.y += 10 self.rect.y += 10
else: else:
self.rect.x -= 10 self.rect.x -= 10
self.handleMapYPosition()
if ((self.rect.x > c.SCREEN_WIDTH) or (self.rect.x < 0) if ((self.rect.x > c.SCREEN_WIDTH) or (self.rect.x < 0)
or (self.rect.y > c.SCREEN_HEIGHT) or (self.rect.y < 0)): or (self.rect.y > c.SCREEN_HEIGHT) or (self.rect.y < 0)):
self.kill() self.kill()
@ -184,6 +186,15 @@ class StarBullet(Bullet):
def draw(self, surface): def draw(self, surface):
surface.blit(self.image, self.rect) surface.blit(self.image, self.rect)
# 这里可以用坚果保龄球的代码改一下
def handleMapYPosition(self):
_, map_y1 = self.level.map.getMapIndex(self.rect.x, self.rect.centery)
if self.map_y != map_y1:
# 换行
self.level.bullet_groups[self.map_y].remove(self)
self.level.bullet_groups[map_y1].add(self)
self.map_y = map_y1
class Plant(pg.sprite.Sprite): class Plant(pg.sprite.Sprite):
def __init__(self, x, y, name, health, bullet_group, scale=1): def __init__(self, x, y, name, health, bullet_group, scale=1):
@ -1125,9 +1136,8 @@ class TorchWood(Plant):
i.kill() i.kill()
class StarFruit(Plant): class StarFruit(Plant):
def __init__(self, x, y, bullet_group, global_bullet_group): def __init__(self, x, y, bullet_group):
Plant.__init__(self, x, y, c.STARFRUIT, c.PLANT_HEALTH, bullet_group) Plant.__init__(self, x, y, c.STARFRUIT, c.PLANT_HEALTH, bullet_group)
self.global_bullet_group = global_bullet_group
self.shoot_timer = 0 self.shoot_timer = 0
def canAttack(self, zombie): def canAttack(self, zombie):
@ -1143,9 +1153,9 @@ class StarFruit(Plant):
def attacking(self): def attacking(self):
if (self.current_time - self.shoot_timer) > 1400: if (self.current_time - self.shoot_timer) > 1400:
self.bullet_group.add(StarBullet(self.rect.left + 10, self.rect.y, c.BULLET_DAMAGE_NORMAL, c.STAR_BACKWARD)) self.bullet_group.add(StarBullet(self.rect.left + 10, self.rect.y, c.BULLET_DAMAGE_NORMAL, c.STAR_BACKWARD))
self.global_bullet_group.add(StarBullet(self.rect.centerx, self.rect.bottom - self.rect.h + 5, c.BULLET_DAMAGE_NORMAL, c.STAR_UPWARD)) self.bullet_group.add(StarBullet(self.rect.centerx, self.rect.bottom - self.rect.h + 5, c.BULLET_DAMAGE_NORMAL, c.STAR_UPWARD))
self.global_bullet_group.add(StarBullet(self.rect.centerx, self.rect.bottom - 5, c.BULLET_DAMAGE_NORMAL, c.STAR_DOWNWARD)) self.bullet_group.add(StarBullet(self.rect.centerx, self.rect.bottom - 5, c.BULLET_DAMAGE_NORMAL, c.STAR_DOWNWARD))
self.global_bullet_group.add(StarBullet(self.rect.left + 5, self.rect.y + 10, c.BULLET_DAMAGE_NORMAL, c.STAR_FORWARD_DOWN)) self.bullet_group.add(StarBullet(self.rect.left + 5, self.rect.y + 10, c.BULLET_DAMAGE_NORMAL, c.STAR_FORWARD_DOWN))
self.global_bullet_group.add(StarBullet(self.rect.left + 5, self.rect.y - 10, c.BULLET_DAMAGE_NORMAL, c.STAR_FORWARD_UP)) self.bullet_group.add(StarBullet(self.rect.left + 5, self.rect.y - 10, c.BULLET_DAMAGE_NORMAL, c.STAR_FORWARD_UP))
self.shoot_timer = self.current_time self.shoot_timer = self.current_time

View File

@ -97,7 +97,6 @@ class Level(tool.State):
self.zombie_groups = [] self.zombie_groups = []
self.hypno_zombie_groups = [] #zombies who are hypno after eating hypnoshroom self.hypno_zombie_groups = [] #zombies who are hypno after eating hypnoshroom
self.bullet_groups = [] self.bullet_groups = []
self.global_bullet_group = pg.sprite.Group() # 全局可用的子弹
for i in range(self.map_y_len): for i in range(self.map_y_len):
self.plant_groups.append(pg.sprite.Group()) self.plant_groups.append(pg.sprite.Group())
self.zombie_groups.append(pg.sprite.Group()) self.zombie_groups.append(pg.sprite.Group())
@ -361,7 +360,6 @@ class Level(tool.State):
self.createZombie(data[1]) self.createZombie(data[1])
self.zombie_list.remove(data) self.zombie_list.remove(data)
self.global_bullet_group.update((self.game_info))
for i in range(self.map_y_len): for i in range(self.map_y_len):
self.bullet_groups[i].update(self.game_info) self.bullet_groups[i].update(self.game_info)
self.plant_groups[i].update(self.game_info) self.plant_groups[i].update(self.game_info)
@ -436,7 +434,6 @@ class Level(tool.State):
# 检查碰撞啥的 # 检查碰撞啥的
self.checkBulletCollisions() self.checkBulletCollisions()
self.checkGlobalBulletCollision()
self.checkZombieCollisions() self.checkZombieCollisions()
self.checkPlants() self.checkPlants()
self.checkCarCollisions() self.checkCarCollisions()
@ -539,7 +536,7 @@ class Level(tool.State):
elif self.plant_name == c.TORCHWOOD: elif self.plant_name == c.TORCHWOOD:
new_plant = plant.TorchWood(x, y, self.bullet_groups[map_y]) new_plant = plant.TorchWood(x, y, self.bullet_groups[map_y])
elif self.plant_name == c.STARFRUIT: elif self.plant_name == c.STARFRUIT:
new_plant = plant.StarFruit(x, y, self.bullet_groups[map_y], self.global_bullet_group) new_plant = plant.StarFruit(x, y, self.bullet_groups[map_y])
if new_plant.can_sleep and self.background_type in {c.BACKGROUND_DAY, c.BACKGROUND_POOL, c.BACKGROUND_ROOF, c.BACKGROUND_WALLNUTBOWLING, c.BACKGROUND_SINGLE, c.BACKGROUND_TRIPLE}: if new_plant.can_sleep and self.background_type in {c.BACKGROUND_DAY, c.BACKGROUND_POOL, c.BACKGROUND_ROOF, c.BACKGROUND_WALLNUTBOWLING, c.BACKGROUND_SINGLE, c.BACKGROUND_TRIPLE}:
new_plant.setSleep() new_plant.setSleep()
@ -636,19 +633,6 @@ class Level(tool.State):
if abs(rangeZombie.rect.x - bullet.rect.x) <= (c.GRID_X_SIZE // 2): if abs(rangeZombie.rect.x - bullet.rect.x) <= (c.GRID_X_SIZE // 2):
rangeZombie.setDamage(c.BULLET_DAMAGE_FIREBALL_RANGE, effect=False, damageType=c.ZOMBIE_DEAFULT_DAMAGE) rangeZombie.setDamage(c.BULLET_DAMAGE_FIREBALL_RANGE, effect=False, damageType=c.ZOMBIE_DEAFULT_DAMAGE)
def checkGlobalBulletCollision(self):
collided_func = pg.sprite.collide_circle_ratio(0.6)
for i in range(self.map_y_len):
for globalBullet in self.global_bullet_group:
if globalBullet.state == c.FLY:
zombie = pg.sprite.spritecollideany(globalBullet, self.zombie_groups[i], collided_func)
if zombie and zombie.state != c.DIE:
# 这里生效代表已经发生了碰撞
zombie.setDamage(globalBullet.damage, damageType=c.ZOMBIE_COMMON_DAMAGE) # 这里设定刻意与原版不同:逻辑上,杨桃是斜着打的,伤害应当可用穿透二类防具
globalBullet.setExplode()
def checkZombieCollisions(self): def checkZombieCollisions(self):
if self.bar_type == c.CHOSSEBAR_BOWLING: if self.bar_type == c.CHOSSEBAR_BOWLING:
ratio = 0.6 ratio = 0.6
@ -950,7 +934,6 @@ class Level(tool.State):
self.menubar.draw(surface) self.menubar.draw(surface)
for car in self.cars: for car in self.cars:
car.draw(surface) car.draw(surface)
self.global_bullet_group.draw(surface)
for i in range(self.map_y_len): for i in range(self.map_y_len):
self.plant_groups[i].draw(surface) self.plant_groups[i].draw(surface)
self.zombie_groups[i].draw(surface) self.zombie_groups[i].draw(surface)