From 21c110ca732ac578c9ba4eb00c51f21db08d31ba Mon Sep 17 00:00:00 2001 From: marblexu <314299052@qq.com> Date: Fri, 30 Aug 2019 08:02:56 +0800 Subject: [PATCH] add ice bullet function --- source/component/plant.py | 7 ++++--- source/component/zombie.py | 23 +++++++++++++++++++---- source/state/level.py | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/source/component/plant.py b/source/component/plant.py index 895c530..ee35ba1 100644 --- a/source/component/plant.py +++ b/source/component/plant.py @@ -5,7 +5,7 @@ from .. import tool from .. import constants as c class Bullet(pg.sprite.Sprite): - def __init__(self, x, y, name, damage): + def __init__(self, x, y, name, damage, ice): pg.sprite.Sprite.__init__(self) self.name = name @@ -18,6 +18,7 @@ class Bullet(pg.sprite.Sprite): self.rect.y = y self.x_vel = 4 self.damage = damage + self.ice = ice self.state = c.FLY self.current_time = 0 @@ -182,7 +183,7 @@ class PeaShooter(Plant): def attacking(self): if (self.current_time - self.shoot_timer) > 2000: - self.bullet_group.add(Bullet(self.rect.right, self.rect.y, c.BULLET_PEA, c.BULLET_DAMAGE_NORMAL)) + self.bullet_group.add(Bullet(self.rect.right, self.rect.y, c.BULLET_PEA, c.BULLET_DAMAGE_NORMAL, False)) self.shoot_timer = self.current_time def setAttack(self): @@ -195,7 +196,7 @@ class SnowPeaShooter(Plant): def attacking(self): if (self.current_time - self.shoot_timer) > 2000: - self.bullet_group.add(Bullet(self.rect.right, self.rect.y, c.BULLET_PEA_ICE, c.BULLET_DAMAGE_NORMAL)) + self.bullet_group.add(Bullet(self.rect.right, self.rect.y, c.BULLET_PEA_ICE, c.BULLET_DAMAGE_NORMAL, True)) self.shoot_timer = self.current_time def setAttack(self): diff --git a/source/component/zombie.py b/source/component/zombie.py index f72f404..45c47f3 100644 --- a/source/component/zombie.py +++ b/source/component/zombie.py @@ -31,6 +31,8 @@ class Zombie(pg.sprite.Sprite): self.attack_timer = 0 self.state = c.WALK self.animate_interval = 150 + self.ice_slow_ratio = 1 + self.ice_slow_timer = 0 def loadFrames(self, frames, name, image_x): frame_list = tool.GFX[name] @@ -44,6 +46,7 @@ class Zombie(pg.sprite.Sprite): def update(self, game_info): self.current_time = game_info[c.CURRENT_TIME] self.handleState() + self.updateIceSlow() self.animation() def handleState(self): @@ -64,7 +67,7 @@ class Zombie(pg.sprite.Sprite): self.changeFrames(self.walk_frames) self.helmet = False - if (self.current_time - self.walk_timer) > c.ZOMBIE_WALK_INTERVAL: + if (self.current_time - self.walk_timer) > (c.ZOMBIE_WALK_INTERVAL * self.ice_slow_ratio): self.walk_timer = self.current_time self.rect.x -= 1 @@ -74,7 +77,7 @@ class Zombie(pg.sprite.Sprite): elif self.health <= c.LOSTHEAD_HEALTH and not self.losHead: self.changeFrames(self.losthead_attack_frames) self.setLostHead() - if (self.current_time - self.attack_timer) > c.ATTACK_INTERVAL: + if (self.current_time - self.attack_timer) > (c.ATTACK_INTERVAL * self.ice_slow_ratio): self.plant.setDamage(self.damage) self.attack_timer = self.current_time @@ -104,7 +107,7 @@ class Zombie(pg.sprite.Sprite): self.rect.centerx = centerx def animation(self): - if (self.current_time - self.animate_timer) > self.animate_interval: + if (self.current_time - self.animate_timer) > (self.animate_interval * self.ice_slow_ratio): self.frame_index += 1 if self.frame_index >= self.frame_num: if self.state == c.DIE: @@ -115,8 +118,20 @@ class Zombie(pg.sprite.Sprite): self.image = self.frames[self.frame_index] - def setDamage(self, damage): + def setIceSlow(self): + '''when get a ice bullet damage, slow the attack or walk speed of the zombie''' + self.ice_slow_timer = self.current_time + self.ice_slow_ratio = 2 + + def updateIceSlow(self): + if self.ice_slow_ratio > 1: + if (self.current_time - self.ice_slow_timer) > c.ICE_SLOW_TIME: + self.ice_slow_ratio = 1 + + def setDamage(self, damage, ice): self.health -= damage + if ice: + self.setIceSlow() def setWalk(self): self.state = c.WALK diff --git a/source/state/level.py b/source/state/level.py index 21dc9dc..2c3ab7f 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -206,7 +206,7 @@ class Level(tool.State): if bullet.state == c.FLY: zombie = pg.sprite.spritecollideany(bullet, self.zombie_groups[i], collided_func) if zombie and zombie.state != c.DIE: - zombie.setDamage(bullet.damage) + zombie.setDamage(bullet.damage, bullet.ice) bullet.setExplode() def checkZombieCollisions(self):