class AttackCharacter(Sprite): def __init__(self, name, mov, atk, xInMap, yInMap, atkValue): super(AttackCharacter, self).__init__() self.xInMap = xInMap self.yInMap = yInMap self.x = xInMap * 48 self.y = yInMap * 48 self.isAtk = False self.atkValue = atkValue self.dir = "down" self.animaSet = AnimationSet() self.addChild(self.animaSet) frameList = Animation.divideUniformSizeFrames(48, 96, 1, 2) # move down bmpd = BitmapData(mov, 0, 0, 48, 96) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("mov_down", anima) # move up bmpd = BitmapData(mov, 0, 96, 48, 96) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("mov_up", anima) # move left bmpd = BitmapData(mov, 0, 192, 48, 96) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("mov_left", anima) # move right bmpd = BitmapData(mov, 0, 192, 48, 96) anima = Animation(bmpd, frameList) anima.mirroring = True self.animaSet.addAnimation("mov_right", anima) frameList = Animation.divideUniformSizeFrames(64, 256, 1, 4) # atk down bmpd = BitmapData(atk, 0, 0, 64, 256) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("atk_down", anima) # atk up bmpd = BitmapData(atk, 0, 256, 64, 256) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("atk_up", anima) # atk left bmpd = BitmapData(atk, 0, 512, 64, 256) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("atk_left", anima) # atk right bmpd = BitmapData(atk, 0, 512, 64, 256) anima = Animation(bmpd, frameList) anima.mirroring = True self.animaSet.addAnimation("atk_right", anima) # uniform treatment of these animations for n in self.animaSet.animationList: o = self.animaSet.animationList[n] o.playMode = AnimationPlayMode.VERTICAL o.speed = 5 o.addEventListener(AnimationEvent.CHANGE_FRAME, self.step) # if atk animation... if n.find("atk") >= 0: o.loopTimes = 1 o.x -= 8 o.y -= 8 o.addEventListener(AnimationEvent.STOP, self.atkOver) self.animaSet.changeAnimation("mov_" + self.dir) ourList = [] def step(self, e): if not self.isAtk: self.attack() def attack(self): x = self.xInMap y = self.yInMap targetList = [] # the attack range of the character: # o o o # o x o # o o o # get some targets to attack and turn to these targets for o in Enemy.enemyList: if o.xInMap == x - 1 and o.yInMap == y: self.dir = "left" targetList.append(o) elif o.xInMap == x - 1 and o.yInMap == y - 1: self.dir = "left" targetList.append(o) elif o.xInMap == x - 1 and o.yInMap == y + 1: self.dir = "left" targetList.append(o) elif o.xInMap == x + 1 and o.yInMap == y: self.dir = "right" targetList.append(o) elif o.xInMap == x + 1 and o.yInMap == y - 1: self.dir = "right" targetList.append(o) elif o.xInMap == x + 1 and o.yInMap == y + 1: self.dir = "right" targetList.append(o) elif o.xInMap == x and o.yInMap == y + 1: self.dir = "down" targetList.append(o) elif o.xInMap == x and o.yInMap == y - 1: self.dir = "up" targetList.append(o) if len(targetList) > 0: label = "atk_" + self.dir self.animaSet.changeAnimation(label, 0, 0, True) self.isAtk = True # attack targets for t in targetList: t.beAttacked(self.atkValue / len(targetList)) else: self.isAtk = False return self.isAtk def atkOver(self, e): if not self.attack(): self.animaSet.changeAnimation("mov_" + self.dir)
class Enemy(Sprite): def __init__(self, mov, xInMap, yInMap): super(Enemy, self).__init__() self.xInMap = xInMap self.yInMap = yInMap self.x = xInMap * 48 self.y = yInMap * 48 # direction that enemy move towards self.dir = "up" # notice that value of stepLength property must be a divisible number of 48 self.stepLength = 8 self.stepIndex = 0 self.stepNum = 48 / self.stepLength self.fullHp = 150 self.hp = self.fullHp self.animaSet = AnimationSet() self.addChild(self.animaSet) frameList = Animation.divideUniformSizeFrames(48, 96, 1, 2) # move down bmpd = BitmapData(mov, 0, 0, 48, 96) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("mov_down", anima) # move up bmpd = BitmapData(mov, 0, 96, 48, 96) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("mov_up", anima) # move left bmpd = BitmapData(mov, 0, 192, 48, 96) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("mov_left", anima) # move right bmpd = BitmapData(mov, 0, 192, 48, 96) anima = Animation(bmpd, frameList) anima.mirroring = True self.animaSet.addAnimation("mov_right", anima) # uniform treatment of these animations for n in self.animaSet.animationList: o = self.animaSet.animationList[n] o.playMode = AnimationPlayMode.VERTICAL o.speed = 3 o.addEventListener(AnimationEvent.CHANGE_FRAME, self.step) self.animaSet.changeAnimation("mov_" + self.dir) # create hp layer self.hpLayer = Sprite() self.hpLayer.x = 8 self.hpLayer.y = -5 self.addChild(self.hpLayer) self.hpLayer.graphics.beginFill("red") self.hpLayer.graphics.drawRect(0, 0, 32, 5) self.hpLayer.graphics.endFill() EVENT_ARRIVE = "event_arrive" EVENT_DIE = "event_die" enemyList = [] def step(self, e): # make enemy move if self.dir == "up": self.y -= self.stepLength elif self.dir == "down": self.y += self.stepLength elif self.dir == "left": self.x -= self.stepLength elif self.dir == "right": self.x += self.stepLength self.stepIndex += 1 # when moved to the next small map block... if self.stepIndex >= self.stepNum: self.stepIndex = 0 if self.dir == "up": self.yInMap -= 1 elif self.dir == "down": self.yInMap += 1 elif self.dir == "left": self.xInMap -= 1 elif self.dir == "right": self.xInMap += 1 self.dir = self.searchDirection() if self.dir == "arrive": return self.animaSet.changeAnimation("mov_" + self.dir, 0, 0) def searchDirection(self): terrain = terrainList x = self.xInMap y = self.yInMap if 0 <= y - 1 and terrain[y - 1][x] == 1 and self.dir != "down": return "up" elif 0 <= x - 1 and terrain[y][x - 1] == 1 and self.dir != "right": return "left" elif y + 1 < len(terrain) and terrain[y + 1][x] == 1 and self.dir != "up": return "down" elif x + 1 < len( terrain[y]) and terrain[y][x + 1] == 1 and self.dir != "left": return "right" self.dispatchEvent(Enemy.EVENT_ARRIVE) return "arrive" def beAttacked(self, v): self.hp -= v # redraw hp self.hpLayer.graphics.clear() self.hpLayer.graphics.beginFill("red") self.hpLayer.graphics.drawRect(0, 0, 32 * (self.hp / self.fullHp), 5) self.hpLayer.graphics.endFill() if self.hp <= 0: self.dispatchEvent(Enemy.EVENT_DIE)
class Enemy(Sprite): def __init__(self, mov, xInMap, yInMap): super(Enemy, self).__init__() self.xInMap = xInMap self.yInMap = yInMap self.x = xInMap * 48 self.y = yInMap * 48 # direction that enemy move towards self.dir = "up" # notice that value of stepLength property must be a divisible number of 48 self.stepLength = 8 self.stepIndex = 0 self.stepNum = 48 / self.stepLength self.fullHp = 150 self.hp = self.fullHp self.animaSet = AnimationSet() self.addChild(self.animaSet) frameList = Animation.divideUniformSizeFrames(48, 96, 1, 2) # move down bmpd = BitmapData(mov, 0, 0, 48, 96) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("mov_down", anima) # move up bmpd = BitmapData(mov, 0, 96, 48, 96) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("mov_up", anima) # move left bmpd = BitmapData(mov, 0, 192, 48, 96) anima = Animation(bmpd, frameList) self.animaSet.addAnimation("mov_left", anima) # move right bmpd = BitmapData(mov, 0, 192, 48, 96) anima = Animation(bmpd, frameList) anima.mirroring = True self.animaSet.addAnimation("mov_right", anima) # uniform treatment of these animations for n in self.animaSet.animationList: o = self.animaSet.animationList[n] o.playMode = AnimationPlayMode.VERTICAL o.speed = 3 o.addEventListener(AnimationEvent.CHANGE_FRAME, self.step) self.animaSet.changeAnimation("mov_" + self.dir) # create hp layer self.hpLayer = Sprite() self.hpLayer.x = 8 self.hpLayer.y = -5 self.addChild(self.hpLayer) self.hpLayer.graphics.beginFill("red") self.hpLayer.graphics.drawRect(0, 0, 32, 5) self.hpLayer.graphics.endFill() EVENT_ARRIVE = "event_arrive" EVENT_DIE = "event_die" enemyList = [] def step(self, e): # make enemy move if self.dir == "up": self.y -= self.stepLength elif self.dir == "down": self.y += self.stepLength elif self.dir == "left": self.x -= self.stepLength elif self.dir == "right": self.x += self.stepLength self.stepIndex += 1 # when moved to the next small map block... if self.stepIndex >= self.stepNum: self.stepIndex = 0 if self.dir == "up": self.yInMap -= 1 elif self.dir == "down": self.yInMap += 1 elif self.dir == "left": self.xInMap -= 1 elif self.dir == "right": self.xInMap += 1 self.dir = self.searchDirection() if self.dir == "arrive": return self.animaSet.changeAnimation("mov_" + self.dir, 0, 0) def searchDirection(self): terrain = terrainList x = self.xInMap y = self.yInMap if 0 <= y - 1 and terrain[y - 1][x] == 1 and self.dir != "down": return "up" elif 0 <= x - 1 and terrain[y][x - 1] == 1 and self.dir != "right": return "left" elif y + 1 < len(terrain) and terrain[y + 1][x] == 1 and self.dir != "up": return "down" elif x + 1 < len(terrain[y]) and terrain[y][x + 1] == 1 and self.dir != "left": return "right" self.dispatchEvent(Enemy.EVENT_ARRIVE) return "arrive" def beAttacked(self, v): self.hp -= v # redraw hp self.hpLayer.graphics.clear() self.hpLayer.graphics.beginFill("red") self.hpLayer.graphics.drawRect(0, 0, 32 * (self.hp / self.fullHp), 5) self.hpLayer.graphics.endFill() if self.hp <= 0: self.dispatchEvent(Enemy.EVENT_DIE)
def demoInit(result): global loadingBar, animaSet loadingBar.remove() # move image mov = result["mov"] # atk image atk = result["atk"] ### # create animation set to contain a sort of animations ### animaSet = AnimationSet() animaSet.x = animaSet.y = 100 addChild(animaSet) ### # move animation ### # get move animation frame list frameList = Animation.divideUniformSizeFrames(48, 96, 1, 2) # move down bmpd = BitmapData(mov, 0, 0, 48, 96) anima = Animation(bmpd, frameList) animaSet.addAnimation("move_down", anima) # move up bmpd = BitmapData(mov, 0, 96, 48, 96) anima = Animation(bmpd, frameList) animaSet.addAnimation("move_up", anima) # move left bmpd = BitmapData(mov, 0, 192, 48, 96) anima = Animation(bmpd, frameList) animaSet.addAnimation("move_left", anima) # move right bmpd = BitmapData(mov, 0, 192, 48, 96) anima = Animation(bmpd, frameList) anima.mirroring = True animaSet.addAnimation("move_right", anima) ### # attack animation ### # get atk animation frame list frameList = Animation.divideUniformSizeFrames(64, 256, 1, 4) # atk down bmpd = BitmapData(atk, 0, 0, 64, 256) anima = Animation(bmpd, frameList) animaSet.addAnimation("atk_down", anima) # atk up bmpd = BitmapData(atk, 0, 256, 64, 256) anima = Animation(bmpd, frameList) animaSet.addAnimation("atk_up", anima) # atk left bmpd = BitmapData(atk, 0, 512, 64, 256) anima = Animation(bmpd, frameList) animaSet.addAnimation("atk_left", anima) # atk right bmpd = BitmapData(atk, 0, 512, 64, 256) anima = Animation(bmpd, frameList) anima.mirroring = True animaSet.addAnimation("atk_right", anima) for n in animaSet.animationList: o = animaSet.animationList[n] o.playMode = AnimationPlayMode.VERTICAL o.speed = 5 # if atk animation... (because atk animation is bigger than move animation) if n.find("atk") >= 0: o.x -= 8 o.y -= 8 animaSet.changeAnimation("move_down") ### # create buttons ### btnList = [ "move down", "move up", "move right", "move left", "atk down", "atk up", "atk right", "atk left" ] btnLayer = Sprite() btnLayer.x = 200 btnLayer.y = 50 addChild(btnLayer) for i, n in enumerate(btnList): btn = ButtonSample(text=n) btn.label = n btnLayer.addChild(btn) btn.addEventListener(MouseEvent.MOUSE_DOWN, onBtnClick) btn.x = (0 if (4 - i) > 0 else 1) * 150 btn.y = (i % 4) * 50