예제 #1
0
class selfMenu(ShowBase):
    def __init__(self):
        """Creates initial window"""
        # Set up the window, camera, etc.
        ShowBase.__init__(self)
        # Set the background color to black
        self.win.setClearColor((0, 0, 0, 1))
        #Loads the world
        self.newScreen()

    def setKey(self, key, value):
        """Binds moving actions to keys"""
        self.keyMap[key] = value

    def move(self, task):
        """Moves actor around and gets camera to follow"""

        #Time since the last frame was called
        dt = globalClock.getDt()

        # If a move-key is pressed, move moore in the specified direction.
        if self.keyMap["left"]:
            self.moore.setH(self.moore.getH() + 300 * dt)
        if self.keyMap["right"]:
            self.moore.setH(self.moore.getH() - 300 * dt)
        if self.keyMap["forward"]:
            self.moore.setY(self.moore, -25 * dt)

        # If Moore is moving, loop the run animation.
        # If he is standing still, stop the animation.

        if self.keyMap["forward"] or self.keyMap["left"] or self.keyMap[
                "right"]:
            if self.isMoving is False:
                self.moore.loop("run")
                self.isMoving = True
        else:
            if self.isMoving:
                self.moore.stop()
                self.moore.pose("walk", 5)
                self.isMoving = False

        # If the camera is too far from Moore, move it closer.
        # If the camera is too close to Moore, move it farther.
        # Camera set far away so that it does not dip under the model

        camvec = self.moore.getPos() - self.camera.getPos()
        camdist = camvec.length()
        camvec.normalize()
        if camdist > 28.0:
            self.camera.setPos(self.camera.getPos() + camvec * (camdist - 28))
            camdist = 28.0
        if camdist < 3.0:
            self.camera.setPos(self.camera.getPos() - camvec * (3 - camdist))
            camdist = 3.0

        self.camera.lookAt(self.floater)

        # Makes lines that Moore can collide with to trigger an event
        self.collisionevent('castle', -5, -80, 10)
        self.collisionevent('house', -70, -10, 20)
        self.collisionevent('halloffame', -10, 115, 10)
        self.collisionevent('tepee', 55, -10, 10)
        self.collisionevent('fifthline', -200, 180, 320)
        self.collisionevent('sixthline', 120, -188, 368)
        self.collisionevent('seventhline', -200, -188, 320)
        self.collisionevent('eighthline', -200, -188, 368)

        return task.cont

    def collisionevent(self, name, startx, starty, iterations):
        """Triggers an event upon a collision"""
        xnames = ["castle", "halloffame", "fifthline", "seventhline"]
        ynames = ["house", "tepee", "sixthline", "eighthline"]
        levelnames = ['castle', 'house', 'tepee', 'halloffame']
        outsidenames = ['fifthline', 'sixthline', 'seventhline', 'eighthline']
        self.name = LineSegs(name)
        self.name.moveTo(startx, starty, 0)
        x = startx
        y = starty
        for i in range(iterations):
            self.name.drawTo(x, y, 0)
            if name in xnames:
                x += 1
            if name in ynames:
                y += 1
        self.name.create()
        vertexlist = self.name.getVertices()

        coorlist = []

        for i in range(len(vertexlist)):
            coorlist.append([vertexlist[i][0], vertexlist[i][1]])

        moorecoor = [int(self.moore.getX()), int(self.moore.getY())]

        if (moorecoor in coorlist):
            if name in levelnames:
                base.graphicsEngine.removeWindow(self.win)
                self.secondWindow(name)

            if name in outsidenames:
                self.moore.setPos(0, 0, 0)

    def secondWindow(self, name):
        #secondwindowcode, after level code based on level
        if name == 'castle':
            self.music.stop()
            from christmas import hometownController
            with open("christmas.py") as f:
                code = compile(f.read(), "christmas.py", 'exec')
                exec(code)

        if name == 'house':
            self.music.stop()
            from hometown import christmasController
            with open("hometown.py") as f:
                code = compile(f.read(), "hometown.py", 'exec')
                exec(code)

        if name == 'tepee':
            self.music.stop()
            from m83 import mController
            with open("m83.py") as f:
                code = compile(f.read(), "m83.py", 'exec')
                exec(code)

        if name == 'halloffame':
            from highscores import highscoreController
            with open("highscores.py") as f:
                code = compile(f.read(), "highscores.py", 'exec')
                exec(code)

    def startMusic(self):
        """Starts and stops music"""
        if self.music:
            self.music.stop()
            self.music = None
        else:
            self.music = self.loader.loadMusic("phase/gunsforhands.ogg")
            self.music.play()

    def newScreen(self):
        """Creates screen with world model and actors"""

        #Loads world model
        self.world = loader.loadModel("phase/trialworld.bam")
        self.world.reparentTo(render)
        #Loads the font
        self.font = loader.loadFont("phase/LemonMilk.ttf")

        self.keyMap = {
            "left": 0,
            "right": 0,
            "forward": 0,
            "cam-left": 0,
            "cam-right": 0
        }
        #Loads actor and animations
        mooreStartPos = (0, 0, 0)
        self.moore = Actor("phase/ralph", {
            "run": "phase/ralph-run",
            "walk": "phase/ralph-walk"
        })
        self.moore.reparentTo(render)
        self.moore.setScale(.2)
        self.moore.setPos(mooreStartPos)
        # Loads a floater above Moore's head to point the camera at
        self.floater = NodePath(PandaNode("floater"))
        self.floater.reparentTo(self.moore)

        #Controls for moving actor around
        self.accept("a", self.setKey, ["left", True])
        self.accept("d", self.setKey, ["right", True])
        self.accept("w", self.setKey, ["forward", True])
        self.accept("a-up", self.setKey, ["left", False])
        self.accept("d-up", self.setKey, ["right", False])
        self.accept("w-up", self.setKey, ["forward", False])

        taskMgr.add(self.move, "moveTask")

        self.isMoving = False

        # Set up the camera
        # Disables the mouse from moving the scren
        self.disableMouse()
        self.camera.setPos(self.moore.getX(),
                           self.moore.getY() + 10,
                           self.moore.getZ() + 2)

        # Loads the music file
        self.music = self.loader.loadMusic("phase/gunsforhands.ogg")
        # Plays the music
        self.music.play()
        # Starts/stops music
        self.accept("m-up", self.startMusic)
        # Creates a title
        self.title = self.addTitle("Moore's World: The Best World", self.font)
        # Creates the instructions
        self.inst1 = self.addInstructions(0.06, "ESC: Quit", self.font)
        self.inst5 = self.addInstructions(0.12, "M: Enable/Disable Music",
                                          self.font)
        self.inst2 = self.addInstructions(0.18, "CONTROLS: W/A/D", self.font)
        self.inst5 = self.addInstructions(0.24, "TEPEE: EASY", self.font)
        self.inst5 = self.addInstructions(0.30, "HOUSE: MEDIUM", self.font)
        self.inst5 = self.addInstructions(0.36, "CASTLE: HARD", self.font)
        # Exits the program
        self.accept("escape", sys.exit)

    def addInstructions(self, pos, msg, font):
        """Posts instructions on screen"""
        return OnscreenText(text=msg,
                            style=1,
                            font=font,
                            fg=(1, 1, 1, 1),
                            scale=.05,
                            shadow=(0, 0, 0, 1),
                            parent=base.a2dTopLeft,
                            pos=(0.08, -pos - 0.04),
                            align=TextNode.ALeft)

    def addTitle(self, text, font):
        """Posts title on screen"""
        return OnscreenText(text=text,
                            style=1,
                            font=font,
                            fg=(1, 1, 1, 1),
                            scale=.07,
                            parent=base.a2dBottomRight,
                            align=TextNode.ARight,
                            pos=(-0.1, 0.09),
                            shadow=(0, 0, 0, 1))