def initCoverPage(result): # receive the loaded data dataList = result # add the cover layer coverLayer = Sprite() addChild(coverLayer) # set the bg image coverBg = Bitmap(BitmapData(dataList["coverBg"], 0, 0, 520, 640)) coverLayer.addChild(coverBg) # define the state of buttons normal = Bitmap(BitmapData(dataList["normalStartBtn"])) over = Bitmap(BitmapData(dataList["actionStartBtn"])) down = Bitmap(BitmapData(dataList["actionStartBtn"])) startBtn = Button(normal, over, down, None) startBtn.x = 135 startBtn.y = 385 coverLayer.addChild(startBtn) def next(e): # remove the contents of cover layer coverLayer.remove() # init cover layer initSettingPage(dataList) startBtn.addEventListener(MouseEvent.MOUSE_UP, next)
def gameOver(): global stageLayer, selectedCharacter, start, enemyNum, isMouseDownAtGameLayer for o in AttackCharacter.ourList: o.animaSet.currentAnimation.removeAllEventListeners() o.animaSet.currentAnimation.stop() for o in Enemy.enemyList: o.animaSet.currentAnimation.removeAllEventListeners() o.animaSet.currentAnimation.stop() start = False stageLayer.mouseEnabled = False stageLayer.mouseChildren = False if selectedCharacter: selectedCharacter.remove() selectedCharacter = None enemyNum = 0 isMouseDownAtGameLayer = False resultLayer = Sprite() addChild(resultLayer) # add hint of game over hintTxt = TextField() hintTxt.textColor = "red" hintTxt.text = "Game Over!" hintTxt.weight = TextFormatWeight.BOLD hintTxt.size = 50 hintTxt.x = (stage.width - hintTxt.width) / 2 hintTxt.y = (stage.height - hintTxt.height) / 2 - 40 resultLayer.addChild(hintTxt) # add restart button def clickRestartBtn(e): global money, lives, loopIndex, stageLayer, fps, terrain AttackCharacter.ourList = [] Enemy.enemyList = [] loopIndex = 0 money = 200 lives = 10 terrain = copy.deepcopy(terrainList) stageLayer.remove() fps.remove() resultLayer.remove() gameInit() normal = Sprite() normal.graphics.beginFill("#666666") normal.graphics.lineStyle(3, "#222222") normal.graphics.drawRect(0, 0, 130, 60) normal.graphics.endFill() txt = TextField() txt.text = "RESTART" 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, 130, 60) over.graphics.endFill() txt = TextField() txt.text = "RESTART" txt.textColor = "white" txt.size = 20 txt.x = (over.width - txt.width) / 2 txt.y = (over.height - txt.height) / 2 over.addChild(txt) restartBtn = Button(normal, over) restartBtn.x = (stage.width - restartBtn.width) / 2 restartBtn.y = stage.height / 2 + 30 resultLayer.addChild(restartBtn) restartBtn.addEventListener(MouseEvent.MOUSE_UP, clickRestartBtn)
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 gameOver(overKind, overInfo): if effectsOn: gameOverEffect.play() global gameContinue gameContinue = False playingLayer.removeAllEventListeners() if overInfo["snakeKind"] == "snake1": snake = snake1 else: snake = snake2 # adjust the positions winner = ["P1"] snake1.speed = 1 snake2.speed = 1 snake1.eatenCandies = 0 snake2.eatenCandies = 0 if (overKind == "wall"): if overInfo["snakeKind"] == "snake1": winner[0] = "P2" while True: if (snake.items[0]["row"] <= 0 or snake.items[0]["row"] >= 49) or (snake.items[0]["col"] <= 0 or snake.items[0]["col"] >= 49): break else: snake.move() elif overKind == "head": if snake1.score == snake2.score: winner = [] elif snake1.score < snake2.score: winner[0] = "P2" for s in overInfo["snakeToMove"]: while (s.items[0]["row"], s.items[0]["col"]) != overInfo["point"]: s.move() elif (overKind == "body"): if overInfo["snakeKind"] == "snake1": winner[0] = "P2" for s in overInfo["snakeToMove"]: while (s.items[0]["row"], s.items[0]["col"]) != overInfo["point"]: s.move() elif (overKind == "bodySameDirection"): if overInfo["snakeKind"] == "snake1": winner[0] = "P2" for s in overInfo["snakeToMove"]: while not((s.items[0]["row"], s.items[0]["col"]) in overInfo["s2Position"]): s.move() lastHead = snake.items[0]["item"] lastHead.visible = False newHead = Bitmap(BitmapData(dataList[snake.snakeKind+"Head"+snake.direction])) newHead.x = lastHead.x newHead.y = lastHead.y playingLayer.addChild(newHead) # display the faces p1Face = TextField() playingLayer.addChild(p1Face) if "P1" in winner: p1Face.text = "😂" else: p1Face.text = "ðŸ˜" p1Face.size = 50 p1Face.x = 20 + 50 - p1Face.width/2 p1Face.y = 20 + 50 - p1Face.height/2 p2Face = TextField() playingLayer.addChild(p2Face) if "P2" in winner: p2Face.text = "😂" else: p2Face.text = "ðŸ˜" p2Face.size = 50 p2Face.x = 400 + 50 - p2Face.width/2 p2Face.y = 20 + 50 - p2Face.height/2 # define the state of buttons normal = Bitmap(BitmapData(dataList["normalLeaveBtn"])) over = Bitmap(BitmapData(dataList["actionLeaveBtn"])) down = Bitmap(BitmapData(dataList["actionLeaveBtn"])) normal.alpha = 0.5 over.alpha = 0.75 down.alpha = 0.75 leaveBtn = Button(normal, over, down, None) leaveBtn.x = 135 leaveBtn.y = 130+250-leaveBtn.height/2 playingLayer.addChild(leaveBtn) def next(e): btnEffect.play() exit() leaveBtn.addEventListener(MouseEvent.MOUSE_UP, next) leaveBtn.addEventListener(MouseEvent.MOUSE_DOWN, btnEffectPlay)