Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
    def startLevel(self, char):
        # get random position of target
        targetX = random.randint(0, 9)
        targetY = random.randint(0, 9)

        self.blockLayer = Sprite()
        self.blockLayer.name = "xxx"
        self.blockLayer.x = 50
        self.blockLayer.y = 50
        self.blockLayer.alpha = 0.7
        self.blockLayer.mouseChildren = False
        self.addChild(self.blockLayer)

        shapeLayer = Sprite()
        self.blockLayer.addChild(shapeLayer)

        txtLayer = Sprite()
        self.blockLayer.addChild(txtLayer)

        # create blocks with a character
        for i in range(10):
            for j in range(10):
                block = Shape()
                block.x = j * 50
                block.y = i * 50
                block.graphics.beginFill("#33CCFF")
                block.graphics.lineStyle(5, "#0066FF")
                block.graphics.drawRect(0, 0, 50, 50)
                block.graphics.endFill()
                shapeLayer.addChild(block)

                txt = TextField()
                txt.size = 20
                txt.font = "Microsoft YaHei"

                # when this block is target
                if i == targetY and j == targetX:
                    txt.text = char[1]

                    block.isTarget = True
                    # when this block is not target
                else:
                    txt.text = char[0]

                    block.isTarget = False

                txt.x = block.x + (50 - txt.width) / 2
                txt.y = block.y + (50 - txt.height) / 2
                txtLayer.addChild(txt)

        self.blockLayer.addEventListener(MouseEvent.MOUSE_UP, self.check)
Ejemplo n.º 3
0
    def startLevel(self, char):
        # get random position of target
        targetX = random.randint(0, 9)
        targetY = random.randint(0, 9)

        self.blockLayer = Sprite()
        self.blockLayer.name = "xxx"
        self.blockLayer.x = 50
        self.blockLayer.y = 50
        self.blockLayer.alpha = 0.7
        self.blockLayer.mouseChildren = False
        self.addChild(self.blockLayer)

        shapeLayer = Sprite()
        self.blockLayer.addChild(shapeLayer)

        txtLayer = Sprite()
        self.blockLayer.addChild(txtLayer)

        # create blocks with a character
        for i in range(10):
            for j in range(10):
                block = Shape()
                block.x = j * 50
                block.y = i * 50
                block.graphics.beginFill("#33CCFF")
                block.graphics.lineStyle(5, "#0066FF")
                block.graphics.drawRect(0, 0, 50, 50)
                block.graphics.endFill()
                shapeLayer.addChild(block)

                txt = TextField()
                txt.size = 20
                txt.font = "Microsoft YaHei"

                # when this block is target
                if i == targetY and j == targetX:
                    txt.text = char[1]

                    block.isTarget = True
                # when this block is not target
                else:
                    txt.text = char[0]

                    block.isTarget = False

                txt.x = block.x + (50 - txt.width) / 2
                txt.y = block.y + (50 - txt.height) / 2
                txtLayer.addChild(txt)

        self.blockLayer.addEventListener(MouseEvent.MOUSE_UP, self.check)
Ejemplo n.º 4
0
def gameInit():
	global stageLayer, gameLayer, ctrlLayer, mapLayer, characterLayer, fps

	# create some layers
	stageLayer = Sprite()
	addChild(stageLayer)

	gameLayer = Sprite()
	stageLayer.addChild(gameLayer)

	mapLayer = Sprite()
	gameLayer.addChild(mapLayer)

	characterLayer = Sprite()
	characterLayer.mouseChildren = False
	gameLayer.addChild(characterLayer)

	ctrlLayer = Sprite()
	stageLayer.addChild(ctrlLayer)

	# add FPS
	fps = FPS()
	addChild(fps)

	addMap()
	initCtrlLayer()
	addEvents()
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
def gameInit():
	global stageLayer, gameLayer, ctrlLayer, mapLayer, characterLayer, fps

	# create some layers
	stageLayer = Sprite()
	addChild(stageLayer)

	gameLayer = Sprite()
	stageLayer.addChild(gameLayer)

	mapLayer = Sprite()
	gameLayer.addChild(mapLayer)

	characterLayer = Sprite()
	characterLayer.mouseChildren = False
	gameLayer.addChild(characterLayer)

	ctrlLayer = Sprite()
	stageLayer.addChild(ctrlLayer)

	# add FPS
	fps = FPS()
	addChild(fps)

	addMap()
	initCtrlLayer()
	addEvents()
Ejemplo n.º 7
0
	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
Ejemplo n.º 8
0
	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
Ejemplo n.º 9
0
def main():
    global stageLayer

    stageLayer = Sprite()
    stageLayer.graphics.beginFill()
    stageLayer.graphics.drawRect(0, 0, stage.width, stage.height)
    stageLayer.graphics.endFill()
    addChild(stageLayer)

    # add events
    stageLayer.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove)
    stageLayer.addEventListener(MouseEvent.MOUSE_UP, mouseUp)

    # create a circle
    circle = DraggableShape(Circle(0, 0, 60))
    circle.x = circle.y = 150
    stageLayer.addChild(circle)

    # create a square
    square = DraggableShape(Polygon(getPolygonVertices(4, 70)))
    square.x = square.y = 350
    stageLayer.addChild(square)

    # create a pentagon
    pentagon = DraggableShape(Polygon(getPolygonVertices(5, 80)))
    pentagon.x = 150
    pentagon.y = 350
    stageLayer.addChild(pentagon)

    # create a hexagon
    hexagon = DraggableShape(Polygon(getPolygonVertices(6, 90)))
    hexagon.x = 350
    hexagon.y = 150
    stageLayer.addChild(hexagon)

    # create a dodecagon
    dodecagon = DraggableShape(Polygon(getPolygonVertices(12, 100)))
    dodecagon.x = 580
    dodecagon.y = 250
    stageLayer.addChild(dodecagon)
Ejemplo n.º 10
0
def gameInit(result):
	global dataList, stageLayer

	dataList = result

	# create stage layer
	stageLayer = Sprite()
	addChild(stageLayer)

	# add FPS
	fps = FPS()
	addChild(fps)

	# add background
	bg = Bitmap(BitmapData(dataList["bg"]))
	stageLayer.addChild(bg)

	# add some text fields
	titleTxt = TextField()
	titleTxt.text = "Get Furit"
	titleTxt.size = 70
	titleTxt.textColor = "red"
	titleTxt.x = (stage.width - titleTxt.width) / 2
	titleTxt.y = 100
	stageLayer.addChild(titleTxt)

	hintTxt = TextField()
	hintTxt.text = "Tap to Start the Game!~"
	hintTxt.textColor = "red"
	hintTxt.size = 40
	hintTxt.x = (stage.width - hintTxt.width) / 2
	hintTxt.y = 300
	stageLayer.addChild(hintTxt)

	engineTxt = TextField()
	engineTxt.text = "- Powered by Pylash -"
	engineTxt.textColor = "red"
	engineTxt.size = 20
	engineTxt.weight = TextFormatWeight.BOLD
	engineTxt.italic = True
	engineTxt.x = (stage.width - engineTxt.width) / 2
	engineTxt.y = 500
	stageLayer.addChild(engineTxt)

	# add event that the game will start when you click
	stageLayer.addEventListener(MouseEvent.MOUSE_UP, startGame)

	# add keyboard events to control player
	stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown)
	stage.addEventListener(KeyboardEvent.KEY_UP, keyUp)
Ejemplo n.º 11
0
def gameInit(result):
	global beginningLayer, dataList, loadingPage

	loadingPage.remove()

	# save the data which is loaded
	dataList = result

	# create a Sprite object
	beginningLayer = Sprite()
	# push the Sprite object into display list to show
	addChild(beginningLayer)

	# add background
	bg = Bitmap(BitmapData(dataList["bg"], 330, 300))
	beginningLayer.addChild(bg)

	# add some text fields
	title = TextField()
	title.text = "Find Character"
	title.font = "Microsoft YaHei"
	title.size = 60
	title.weight = TextFormatWeight.BOLD
	title.x = (stage.width - title.width) / 2
	title.y = 150
	beginningLayer.addChild(title)

	hintTxt = TextField()
	hintTxt.text = "Tap to Start The Game"
	hintTxt.font = "Microsoft YaHei"
	hintTxt.size = 30
	hintTxt.x = (stage.width - hintTxt.width) / 2
	hintTxt.y = 300
	beginningLayer.addChild(hintTxt)

	engineTxt = TextField()
	engineTxt.text = "- Powered by Pylash -"
	engineTxt.font = "Microsoft YaHei"
	engineTxt.size = 20
	engineTxt.italic = True
	engineTxt.x = (stage.width - engineTxt.width) / 2
	engineTxt.y = 400
	beginningLayer.addChild(engineTxt)

	# add event that game will start when you click
	beginningLayer.addEventListener(MouseEvent.MOUSE_UP, startGame)
Ejemplo n.º 12
0
def gameInit(result):

    global dataList, stageLayer  # 全局变量dataList和stageLayer

    dataList = result  # 将result的值赋给dataList,又因为dataList是全局变量,所以在其他函数中也可以通过dataList调用加载完成的文件

    # 创建主界面
    stageLayer = Sprite()  # 创建了一个Sprite类的对象stageLayer
    addChild(
        stageLayer)  # addChild是一个用于把显示对象加到自身这个层上的函数,详见utils.py文件中的全局函数addChild

    # 创建背景元素
    bg = Bitmap(BitmapData(dataList["bg"]))
    #dataList保存加载完成的文件,BitmapData类用于存储图像数据,而Bitmap类用于显示图片,类似于TextField
    stageLayer.addChild(bg)
    #此处使用的不是全局函数addChild,而是stageLayer对象调用的addChild,stageLayer作为self参数传入,使得bd添加在了stageLayer上

    # 创建文本元素
    titleTxt = creatTxt("忍住不能吃零食", 70, 155, 100,
                        "black")  #以上代码生成的是第一行文本,即游戏的名字

    # 创建按钮元素
    button0 = creatButton("button0", stage.width - 225, 350,
                          mySettings)  # button0是"开始游戏"按钮,点击按钮进入"设置"界面
    button1 = creatButton("button1", stage.width - 225, 420,
                          introduction)  # button1是"规则介绍"按钮,点击按钮进入"规则介绍"界面

    # 创建音乐播放元素
    music0 = Sound()
    music0.load("http:\\bd.kuwo.cn\yinyue\37552330?from=baidu")
    music0.play()
    #未能正常运行

    # 添加监听器
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown)
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUp)
Ejemplo n.º 13
0
def gameInit(result):
    global beginningLayer, dataList, loadingPage

    loadingPage.remove()

    # save the data which is loaded
    dataList = result

    # create a Sprite object
    beginningLayer = Sprite()
    # push the Sprite object into display list to show
    addChild(beginningLayer)

    # add background
    bg = Bitmap(BitmapData(dataList["bg"], 330, 300))
    beginningLayer.addChild(bg)

    # add some text fields
    title = TextField()
    title.text = "Find Character"
    title.font = "Microsoft YaHei"
    title.size = 60
    title.weight = TextFormatWeight.BOLD
    title.x = (stage.width - title.width) / 2
    title.y = 150
    beginningLayer.addChild(title)

    hintTxt = TextField()
    hintTxt.text = "Tap to Start The Game"
    hintTxt.font = "Microsoft YaHei"
    hintTxt.size = 30
    hintTxt.x = (stage.width - hintTxt.width) / 2
    hintTxt.y = 300
    beginningLayer.addChild(hintTxt)

    engineTxt = TextField()
    engineTxt.text = "- Powered by Pylash -"
    engineTxt.font = "Microsoft YaHei"
    engineTxt.size = 20
    engineTxt.italic = True
    engineTxt.x = (stage.width - engineTxt.width) / 2
    engineTxt.y = 400
    beginningLayer.addChild(engineTxt)

    # add event that game will start when you click
    beginningLayer.addEventListener(MouseEvent.MOUSE_UP, startGame)
Ejemplo n.º 14
0
    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
Ejemplo n.º 15
0
    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
Ejemplo n.º 16
0
class GameLayer(Sprite):
    def __init__(self, bg):
        super(GameLayer, self).__init__()

        self.blockLayer = None
        self.characters = (("我", "找"), ("王", "玉"), ("籍", "藉"), ("春", "舂"),
                           ("龙", "尤"), ("已", "己"), ("巳", "已"))

        # add background
        bg = Bitmap(BitmapData(bg, 50, 50))
        self.addChild(bg)

        # start game
        self.start()

    def createTimeBar(self):
        self.timeBar = TimeBar()
        self.timeBar.x = stage.width - 70
        self.timeBar.y = (stage.height - self.timeBar.height) / 2
        self.addChild(self.timeBar)

    def startLevel(self, char):
        # get random position of target
        targetX = random.randint(0, 9)
        targetY = random.randint(0, 9)

        self.blockLayer = Sprite()
        self.blockLayer.name = "xxx"
        self.blockLayer.x = 50
        self.blockLayer.y = 50
        self.blockLayer.alpha = 0.7
        self.blockLayer.mouseChildren = False
        self.addChild(self.blockLayer)

        shapeLayer = Sprite()
        self.blockLayer.addChild(shapeLayer)

        txtLayer = Sprite()
        self.blockLayer.addChild(txtLayer)

        # create blocks with a character
        for i in range(10):
            for j in range(10):
                block = Shape()
                block.x = j * 50
                block.y = i * 50
                block.graphics.beginFill("#33CCFF")
                block.graphics.lineStyle(5, "#0066FF")
                block.graphics.drawRect(0, 0, 50, 50)
                block.graphics.endFill()
                shapeLayer.addChild(block)

                txt = TextField()
                txt.size = 20
                txt.font = "Microsoft YaHei"

                # when this block is target
                if i == targetY and j == targetX:
                    txt.text = char[1]

                    block.isTarget = True
                # when this block is not target
                else:
                    txt.text = char[0]

                    block.isTarget = False

                txt.x = block.x + (50 - txt.width) / 2
                txt.y = block.y + (50 - txt.height) / 2
                txtLayer.addChild(txt)

        self.blockLayer.addEventListener(MouseEvent.MOUSE_UP, self.check)

    def check(self, e):
        global gameOver

        clickX = math.floor(e.selfX / 50)
        clickY = math.floor(e.selfY / 50)

        shapeLayer = self.blockLayer.getChildAt(0)

        b = shapeLayer.getChildAt(clickY * 10 + clickX)

        if b.isTarget:
            self.gameOver("win")

    def gameOver(self, flag):
        self.blockLayer.remove()

        if flag == "win":
            self.levelIndex += 1

            if self.levelIndex >= len(self.characters):
                self.addResultLayer("Level Clear!",
                                    "Time: %s" % int(self.timeBar.usedTime))

                self.timeBar.remove()
            else:
                self.startLevel(self.characters[self.levelIndex])
        elif flag == "lose":
            self.addResultLayer("Time is Up!", "Level: %s" % (self.levelIndex))

            self.timeBar.remove()

    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 start(self):
        self.levelIndex = 0

        # add time bar
        self.createTimeBar()

        # start level 1
        self.startLevel(self.characters[0])
Ejemplo n.º 17
0
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)
Ejemplo n.º 18
0
def gameStart(data, bgmPlay, effectsPlay, p1Profile, p2Profile):
    global dataList, playingLayer, effectsOn, gameOverEffect, eatCandyEffects, moveEffect, btnEffect

    effectsOn = effectsPlay
    # receive the loaded data
    dataList = data

    gameOverEffect = Sound(dataList["gameOverEffect"])

    eatCandyEffects = [Sound(dataList["eatCandyEffect1"]),Sound(dataList["eatCandyEffect2"]),Sound(dataList["eatCandyEffect3"])]
    for eatCandyEffect in eatCandyEffects:
        eatCandyEffect.loopCount = 1

    moveEffect = Sound(dataList["moveEffect"])
    moveEffect.loopCount = Sound.LOOP_FOREVER

    btnEffect = Sound(dataList["btnEffect"])
    btnEffect.loopCount = 1

    # create the playing layer
    playingLayer = Sprite()
    addChild(playingLayer)

    # set the bg image
    playingBg = Bitmap(BitmapData(dataList["playingBg"], 0, 0, 520, 640))
    playingLayer.addChild(playingBg)

    # add profiles
    profile1 = Bitmap(BitmapData(dataList["profile%s" % p1Profile]))
    profile1.x = 20
    profile1.y = 20
    profile1.width = 100
    profile1.height = 100
    playingLayer.addChild(profile1)

    profile2 = Bitmap(BitmapData(dataList["profile%s" % p2Profile]))
    profile2.x = 400
    profile2.y = 20
    profile2.width = 100
    profile2.height = 100
    playingLayer.addChild(profile2) # display the scores
    colon = TextField()
    colon.text = ":"
    colon.size = 50
    colon.x = 260 - colon.width/2
    colon.y = 70 - colon.height/2
    colon.textColor = "#225590"
    colon.font = "Bradley Hand"
    playingLayer.addChild(colon)

    global snake1, snake2
    # create game snakes
    snake1 = snake("snake1", 60, 180, "Right")
    snake2 = snake("snake2", 450, 570, "Left")

    global score1Txt, score2Txt

    score1Txt = TextField()
    score1Txt.text = "%s" % snake1.score
    score1Txt.size = 50
    score1Txt.x = 260 - 20 - score1Txt.width
    score1Txt.y = 70 - score1Txt.height/2
    score1Txt.textColor = "#225590"
    score1Txt.font = "Bradley Hand"
    playingLayer.addChild(score1Txt)

    score2Txt = TextField()
    score2Txt.text = "%s" % snake2.score
    score2Txt.size = 50
    score2Txt.x = 260 + 20
    score2Txt.y = 70 - score2Txt.height/2
    score2Txt.textColor = "#225590"
    score2Txt.font = "Bradley Hand"
    playingLayer.addChild(score2Txt)

    # add sample snakes
    sampleSnake1 = snake("snake1", 130, 40, "Up")
    sampleSnake2 = snake("snake2", 380, 40, "Up")


    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown)
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUp)
    playingLayer.addEventListener(LoopEvent.ENTER_FRAME, loop)

    generate(0.05)
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
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)
Ejemplo n.º 21
0
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
Ejemplo n.º 22
0
class GameLayer(Sprite):
    def __init__(self, bg):
        super(GameLayer, self).__init__()

        self.blockLayer = None
        self.characters = (("我", "找"), ("王", "玉"), ("籍", "藉"), ("春", "舂"), ("龙", "尤"), ("已", "己"), ("巳", "已"))

        # add background
        bg = Bitmap(BitmapData(bg, 50, 50))
        self.addChild(bg)

        # start game
        self.start()

    def createTimeBar(self):
        self.timeBar = TimeBar()
        self.timeBar.x = stage.width - 70
        self.timeBar.y = (stage.height - self.timeBar.height) / 2
        self.addChild(self.timeBar)

    def startLevel(self, char):
        # get random position of target
        targetX = random.randint(0, 9)
        targetY = random.randint(0, 9)

        self.blockLayer = Sprite()
        self.blockLayer.name = "xxx"
        self.blockLayer.x = 50
        self.blockLayer.y = 50
        self.blockLayer.alpha = 0.7
        self.blockLayer.mouseChildren = False
        self.addChild(self.blockLayer)

        shapeLayer = Sprite()
        self.blockLayer.addChild(shapeLayer)

        txtLayer = Sprite()
        self.blockLayer.addChild(txtLayer)

        # create blocks with a character
        for i in range(10):
            for j in range(10):
                block = Shape()
                block.x = j * 50
                block.y = i * 50
                block.graphics.beginFill("#33CCFF")
                block.graphics.lineStyle(5, "#0066FF")
                block.graphics.drawRect(0, 0, 50, 50)
                block.graphics.endFill()
                shapeLayer.addChild(block)

                txt = TextField()
                txt.size = 20
                txt.font = "Microsoft YaHei"

                # when this block is target
                if i == targetY and j == targetX:
                    txt.text = char[1]

                    block.isTarget = True
                    # when this block is not target
                else:
                    txt.text = char[0]

                    block.isTarget = False

                txt.x = block.x + (50 - txt.width) / 2
                txt.y = block.y + (50 - txt.height) / 2
                txtLayer.addChild(txt)

        self.blockLayer.addEventListener(MouseEvent.MOUSE_UP, self.check)

    def check(self, e):
        global gameOver

        clickX = math.floor(e.selfX / 50)
        clickY = math.floor(e.selfY / 50)

        shapeLayer = self.blockLayer.getChildAt(0)

        b = shapeLayer.getChildAt(clickY * 10 + clickX)

        if b.isTarget:
            self.gameOver("win")

    def gameOver(self, flag):
        self.blockLayer.remove()

        if flag == "win":
            self.levelIndex += 1

            if self.levelIndex >= len(self.characters):
                self.addResultLayer("Level Clear!", "Time: %s" % int(self.timeBar.usedTime))

                self.timeBar.remove()
            else:
                self.startLevel(self.characters[self.levelIndex])
        elif flag == "lose":
            self.addResultLayer("Time is Up!", "Level: %s" % (self.levelIndex))

            self.timeBar.remove()

    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 start(self):
        self.levelIndex = 0

        # add time bar
        self.createTimeBar()

        # start level 1
        self.startLevel(self.characters[0])
Ejemplo n.º 23
0
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