def main(): controlLayer = Sprite() addChild(controlLayer) title = TextField() title.size = 25 title.text = "Choose A Loading Bar" controlLayer.addChild(title) btn1 = ButtonSample(text="LoadingSample1", size=30) btn1.y = 100 btn1.addEventListener(MouseEvent.MOUSE_UP, lambda e: addLoadingBar(1)) controlLayer.addChild(btn1) btn2 = ButtonSample(text="LoadingSample2", size=30) btn2.y = 180 btn2.addEventListener(MouseEvent.MOUSE_UP, lambda e: addLoadingBar(2)) controlLayer.addChild(btn2) btn3 = ButtonSample(text="LoadingSample3", size=30) btn3.y = 260 btn3.addEventListener(MouseEvent.MOUSE_UP, lambda e: addLoadingBar(3)) controlLayer.addChild(btn3) title.x = (controlLayer.width - title.width) / 2 controlLayer.x = (stage.width - controlLayer.width) / 2 controlLayer.y = (stage.height - controlLayer.height) / 2
def addResultLayer(self, *text): resultLayer = Sprite() self.addChild(resultLayer) for i in text: txt = TextField() txt.font = "Microsoft YaHei" txt.text = i txt.size = 40 txt.textAlign = TextFormatAlign.CENTER txt.x = stage.width / 2 txt.y = resultLayer.height + 20 txt.textColor = "orangered" txt.weight = TextFormatWeight.BOLD resultLayer.addChild(txt) # add a botton used for restart restartBtn = Sprite() restartBtn.graphics.beginFill("yellow") restartBtn.graphics.lineStyle(3, "orangered") restartBtn.graphics.drawRect(0, 0, 200, 60) restartBtn.graphics.endFill() restartBtn.x = (stage.width - restartBtn.width) / 2 restartBtn.y = resultLayer.height + 50 resultLayer.addChild(restartBtn) restartTxt = TextField() restartTxt.font = "Microsoft YaHei" restartTxt.text = "Restart" restartTxt.textColor = "orangered" restartTxt.size = 30 restartTxt.x = (restartBtn.width - restartTxt.width) / 2 restartTxt.y = (restartBtn.height - restartTxt.height) / 2 restartBtn.addChild(restartTxt) def restart(e): self.start() resultLayer.remove() restartBtn.addEventListener(MouseEvent.MOUSE_UP, restart) # make the result layer locate at vertical center resultLayer.y = (stage.height - resultLayer.height) / 2
def selectCharacter(e): global selectedCharacter name = e.currentTarget.name # create selectedCharacter selectedCharacter = Sprite() selectedCharacter.name = name selectedCharacter.mouseShelter = False stageLayer.addChild(selectedCharacter) bmp = Bitmap(BitmapData(dataList[name + "_mov"], 0, 0, 48, 48)) selectedCharacter.addChild(bmp) # make selectedCharacter move to mouse's position selectedCharacter.x = e.offsetX - selectedCharacter.width / 2 selectedCharacter.y = e.offsetY - selectedCharacter.height / 2
def initCtrlLayer(): global ctrlLayer, selectedCharacter, startBtn, moneyTxt, livesTxt, money, lives def selectCharacter(e): global selectedCharacter name = e.currentTarget.name # create selectedCharacter selectedCharacter = Sprite() selectedCharacter.name = name selectedCharacter.mouseShelter = False stageLayer.addChild(selectedCharacter) bmp = Bitmap(BitmapData(dataList[name + "_mov"], 0, 0, 48, 48)) selectedCharacter.addChild(bmp) # make selectedCharacter move to mouse's position selectedCharacter.x = e.offsetX - selectedCharacter.width / 2 selectedCharacter.y = e.offsetY - selectedCharacter.height / 2 # draw the background of the ctrlLayer ctrlLayer.graphics.beginFill("#333333") ctrlLayer.graphics.lineStyle(3, "#222222") ctrlLayer.graphics.drawRect(0, 0, 150, stage.height) ctrlLayer.graphics.endFill() # list of our characters ourList = ["guanyu", "xiahoudun", "soldier1"] # show selectors of our characters selectorsLayer = Sprite() ctrlLayer.addChild(selectorsLayer) for i, v in enumerate(ourList): selector = Sprite() selector.name = v selectorsLayer.addChild(selector) # the appearance of the character bmp = Bitmap(BitmapData(dataList[v + "_mov"], 0, 0, 48, 48)) selector.addChild(bmp) # show the cost of the character costTxt = TextField() costTxt.text = charactersData[v]["cost"] costTxt.textColor = "white" costTxt.x = (bmp.width - costTxt.width) / 2 costTxt.y = bmp.height + 10 selector.addChild(costTxt) selector.y = i * 88 selector.addEventListener(MouseEvent.MOUSE_DOWN, selectCharacter) selectorsLayer.x = (ctrlLayer.width - selectorsLayer.width) / 2 selectorsLayer.y = 40 # add lives and money text livesTxt = TextField() livesTxt.text = "Lives: %s" % lives livesTxt.x = (ctrlLayer.width - livesTxt.width) / 2 livesTxt.y = stage.height - 150 livesTxt.textColor = "white" ctrlLayer.addChild(livesTxt) moneyTxt = TextField() moneyTxt.text = "Money: %s" % money moneyTxt.x = (ctrlLayer.width - moneyTxt.width) / 2 moneyTxt.y = livesTxt.y + 30 moneyTxt.textColor = "white" ctrlLayer.addChild(moneyTxt) # add a button to start game def clickStartBtn(e): global start, enemyNum start = True enemyNum = 0 e.currentTarget.state = ButtonState.DISABLED normal = Sprite() normal.graphics.beginFill("#666666") normal.graphics.lineStyle(3, "#222222") normal.graphics.drawRect(0, 0, 100, 60) normal.graphics.endFill() txt = TextField() txt.text = "START" txt.textColor = "white" txt.size = 20 txt.x = (normal.width - txt.width) / 2 txt.y = (normal.height - txt.height) / 2 normal.addChild(txt) over = Sprite() over.graphics.beginFill("#888888") over.graphics.lineStyle(3, "#222222") over.graphics.drawRect(0, 0, 100, 60) over.graphics.endFill() txt = TextField() txt.text = "START" txt.textColor = "white" txt.size = 20 txt.x = (over.width - txt.width) / 2 txt.y = (over.height - txt.height) / 2 over.addChild(txt) disabled = Sprite() disabled.graphics.beginFill("#666666") disabled.graphics.lineStyle(3, "#222222") disabled.graphics.drawRect(0, 0, 100, 60) disabled.graphics.endFill() txt = TextField() txt.text = "START" txt.textColor = "#333333" txt.size = 20 txt.x = (disabled.width - txt.width) / 2 txt.y = (disabled.height - txt.height) / 2 disabled.addChild(txt) startBtn = Button(normal, over, None, disabled) startBtn.x = (ctrlLayer.width - startBtn.width) / 2 startBtn.y = stage.height - 80 ctrlLayer.addChild(startBtn) startBtn.addEventListener(MouseEvent.MOUSE_UP, clickStartBtn) ctrlLayer.x = stage.width - ctrlLayer.width
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