Exemplo n.º 1
0
    def __init__(self, fn):
        """
      Load the image and cut out the individual characters.

      fn - the font XML file.
      """

        #parse the XML file
        root = data.getTreeRoot(fn)
        path = os.path.join(settings.path, "data",
                            data.getAttr(root, "file", data.D_STRING))
        self.height = data.getAttr(root, "height", data.D_INT)
        transparency = data.getAttr(root, "transparency", data.D_INT3LIST)

        #load the image
        image = data.getImage(path, fn)
        image.set_colorkey(transparency)

        #cut out the tiles and store them in a dictionary
        self.characters = {}
        for c in data.getChildren(root, "character"):
            char = data.getAttr(c, "char", data.D_STRING)
            width = data.getAttr(c, "width", data.D_INT)
            location = data.getAttr(c, "location", data.D_INT2LIST)
            self.characters[char] = image.subsurface(location[0], location[1],
                                                     width, self.height)
Exemplo n.º 2
0
   def __init__(self, fn):
      """
      Open the tileset image, and set up animations.

      fn - the path to the tileset image.
      """

      #parse the XML file
      root = data.getTreeRoot(fn)      
      self.tileSize = data.getAttr(root, "tilesize", data.D_INT2LIST) 
      self.transparency = data.getAttr(root, "transparency", data.D_INT3LIST)

      #create the tiles
      tilesetPath = os.path.join(settings.path, "data", data.getAttr(root, "file", data.D_STRING))
      self.openImage(tilesetPath)

      #create animations for the tileset
      self.autoAnimations = {}
      self.walkontoAnimations = {}
      for anim in root.findall("animation"):
         trigger = data.getAttr(anim, "trigger", data.D_STRING)
         tile = data.getAttr(anim, "tile", data.D_INT)
         frames = data.getAttr(anim, "frames", data.D_INTLIST)
         a = animation.Animation(frames)
         
         if trigger == "auto":
            a.play(True)
            self.autoAnimations[tile] = a
         elif trigger == "walkonto":
            self.walkontoAnimations[tile] = a
Exemplo n.º 3
0
    def __init__(self, fn):
        """
      Open the tileset image, and set up animations.

      fn - the path to the tileset image.
      """

        #parse the XML file
        root = data.getTreeRoot(fn)
        self.tileSize = data.getAttr(root, "tilesize", data.D_INT2LIST)
        self.transparency = data.getAttr(root, "transparency", data.D_INT3LIST)

        #create the tiles
        tilesetPath = os.path.join(settings.path, "data",
                                   data.getAttr(root, "file", data.D_STRING))
        self.openImage(tilesetPath)

        #create animations for the tileset
        self.autoAnimations = {}
        self.walkontoAnimations = {}
        for anim in root.findall("animation"):
            trigger = data.getAttr(anim, "trigger", data.D_STRING)
            tile = data.getAttr(anim, "tile", data.D_INT)
            frames = data.getAttr(anim, "frames", data.D_INTLIST)
            a = animation.Animation(frames)

            if trigger == "auto":
                a.play(True)
                self.autoAnimations[tile] = a
            elif trigger == "walkonto":
                self.walkontoAnimations[tile] = a
Exemplo n.º 4
0
    def setup(self):
        """Initialize pygame, find the main game file and parse it."""

        #initialise pygame
        pygame.init()

        #find the main game file and parse it
        if settings.path is not None:
            fn = os.path.join(settings.path, "data", "game.xml")
        else:
            raise error.DittoInvalidResourceException("settings.py", "path")
        root = data.getTreeRoot(fn, "Ditto Main")
        for p in data.getChildren(root, "property"):
            if data.getAttr(p, "name", data.D_STRING) == "tilesize":
                globs.TILESIZE = data.getAttr(p, "value", data.D_INT2LIST)
            elif data.getAttr(p, "name", data.D_STRING) == "font":
                globs.FONT = data.getAttr(p, "value", data.D_STRING)
            elif data.getAttr(p, "name", data.D_STRING) == "dialog":
                globs.DIALOG = data.getAttr(p, "value", data.D_STRING)
            elif data.getAttr(p, "name", data.D_STRING) == "pokemon":
                globs.POKEMON = data.getAttr(p, "value", data.D_STRING)
            elif data.getAttr(p, "name", data.D_STRING) == "soundeffects":
                soundPath = os.path.join(
                    settings.path, "data",
                    data.getAttr(p, "value", data.D_STRING))
            elif data.getAttr(p, "name", data.D_STRING) == "moves":
                globs.MOVES = os.path.join(
                    settings.path, "data",
                    data.getAttr(p, "value", data.D_STRING))
            elif data.getAttr(p, "name", data.D_STRING) == "behaviours":
                globs.BEHAVIOURS = os.path.join(
                    settings.path, "data",
                    data.getAttr(p, "value", data.D_STRING))

        #if there's an icon specified, use it
        if len(data.getChildren(root, "icon")) > 0:
            node = data.getChild(root, "icon")
            iconPath = os.path.join(settings.path, "data",
                                    data.getAttr(node, "file", data.D_STRING))
            icon = data.getImage(iconPath, fn)
            pygame.display.set_icon(icon)

        #set up the main window
        windowSize = settings.screenSize[0] * globs.TILESIZE[
            0], settings.screenSize[1] * globs.TILESIZE[1]
        self.screen = pygame.display.set_mode(windowSize)
        pygame.display.set_caption("%s --- Ditto Engine" %
                                   data.getAttr(root, "title", data.D_STRING))

        #create a clock object
        self.clock = pygame.time.Clock()

        #initialise sound
        sound.init(soundPath)

        #set up the initial scene, the intro
        self.activeScene = intro.Intro(self.screen)
Exemplo n.º 5
0
   def __init__(self, text, screen, drawCursor=True):
      """
      Create the dialog box and load cursors.

      text - a list of lines of text to go in the dialog.
      font - the font with which to write the text.
      screen - the surface to draw the dialog onto.
      soundManager - the sound manager.
      drawCursor - whether a continuation cursor should be drawn when the text has finished writing.
      """

      #store variables we'll need again
      self.text = text.split(LINESEP)
      self.screen = screen
      self.drawCursor = drawCursor
      
      #determine the speed to write at, in characters per tick
      if settings.textSpeed == "SLOW":
         self.speed = 1
      elif settings.textSpeed == "MEDIUM":
         self.speed = 2
      elif settings.textSpeed == "FAST":
         self.speed = 4

      #parse the dialog xml file
      fn = os.path.join(settings.path, "data", globs.DIALOG)
      root = data.getTreeRoot(fn, "Ditto main")
      transparency = data.getAttr(root, "transparency", data.D_INT3LIST)

      #create font
      fontPath = os.path.join(settings.path, "data", globs.FONT)
      self.font = font.Font(fontPath)

      #create the box
      size = ((self.screen.get_width()-(OBJECTBUFFER*2), (len(self.text)*(self.font.height+LINEBUFFER))-LINEBUFFER+(BORDER*2)))
      self.box = box.Box(size).convert(self.screen)

      #load the cursors
      cursorPath = os.path.join(settings.path, "data", data.getAttr(root, "cursor", data.D_STRING))
      self.cursor = data.getImage(cursorPath).convert(self.screen)
      self.cursor.set_colorkey(transparency)
      self.cursorLocation = (self.screen.get_width()-OBJECTBUFFER-BORDER-self.cursor.get_width(),
                             self.screen.get_height()-OBJECTBUFFER-BORDER-self.cursor.get_height())

      cursorPath = os.path.join(settings.path, "data", data.getAttr(root, "sidecursor", data.D_STRING))
      self.sideCursor = data.getImage(cursorPath).convert(self.screen)
      self.sideCursor.set_colorkey(transparency)

      #calculate location of dialog box
      self.location = OBJECTBUFFER, self.screen.get_height()-self.box.get_height()-OBJECTBUFFER

      #start progress at 0 and set drawing and busy
      self.progress = 0
      self.writing = True
      self.busy = True
Exemplo n.º 6
0
def init(fn):
   """
   Initialise the pygame mixer and parse XML file for sound effect locations.

   fn - the path to the XML file.
   """

   #initialise the pygame mixer
   pygame.mixer.init()

   #parse the XML file
   root = data.getTreeRoot(fn)
   for effect in data.getChildren(root, "soundeffect"):
      e = SOUNDEFFECTS[data.getAttr(effect, "name", data.D_STRING)]
      effects[e] = os.path.join(settings.path, "data", data.getAttr(effect, "file", data.D_STRING))
Exemplo n.º 7
0
   def setup(self):
      """Initialize pygame, find the main game file and parse it."""

      #initialise pygame
      pygame.init()

      #find the main game file and parse it
      if settings.path is not None:
         fn = os.path.join(settings.path, "data", "game.xml")
      else:
         raise error.DittoInvalidResourceException("settings.py", "path")
      root = data.getTreeRoot(fn, "Ditto Main")
      for p in data.getChildren(root, "property"):
         if data.getAttr(p, "name", data.D_STRING) == "tilesize":
            globs.TILESIZE = data.getAttr(p, "value", data.D_INT2LIST)
         elif data.getAttr(p, "name", data.D_STRING) == "font":
            globs.FONT = data.getAttr(p, "value", data.D_STRING)
         elif data.getAttr(p, "name", data.D_STRING) == "dialog":
            globs.DIALOG = data.getAttr(p, "value", data.D_STRING)
         elif data.getAttr(p, "name", data.D_STRING) == "pokemon":
            globs.POKEMON = data.getAttr(p, "value", data.D_STRING)
         elif data.getAttr(p, "name", data.D_STRING) == "soundeffects":
            soundPath = os.path.join(settings.path, "data", data.getAttr(p, "value", data.D_STRING))
         elif data.getAttr(p, "name", data.D_STRING) == "moves":
            globs.MOVES = os.path.join(settings.path, "data", data.getAttr(p, "value", data.D_STRING))
         elif data.getAttr(p, "name", data.D_STRING) == "behaviours":
            globs.BEHAVIOURS = os.path.join(settings.path, "data", data.getAttr(p, "value", data.D_STRING))

      #if there's an icon specified, use it      
      if len(data.getChildren(root, "icon")) > 0:
         node = data.getChild(root, "icon")
         iconPath = os.path.join(settings.path, "data", data.getAttr(node, "file", data.D_STRING))
         icon = data.getImage(iconPath, fn)
         pygame.display.set_icon(icon)

      #set up the main window
      windowSize = settings.screenSize[0]*globs.TILESIZE[0], settings.screenSize[1]*globs.TILESIZE[1]   
      self.screen = pygame.display.set_mode(windowSize)
      pygame.display.set_caption("%s --- Ditto Engine" % data.getAttr(root, "title", data.D_STRING))

      #create a clock object
      self.clock = pygame.time.Clock()

      #initialise sound
      sound.init(soundPath)

      #set up the initial scene, the intro
      self.activeScene = intro.Intro(self.screen)
Exemplo n.º 8
0
    def __init__(self, screen, game, fn):
        self.screen = screen
        self.game = game
        self.font = font.Font(os.path.join(settings.path, "data",
                                           globs.FONT))  #create the font

        self.menuNode = data.getTreeRoot(fn)

        self.foregroundObject = None

        self.objectBuffer = 2
        self.border = 10
        self.lineBuffer = 3

        self.choices = ["POKEDEX", "POKEMON", "BAG", "OPTIONS", "SAVE", "EXIT"]

        self.current = 0

        fn = os.path.join(settings.path, "data", globs.DIALOG)
        root = ET.parse(fn).getroot()
        self.transparency = map(int, root.attrib["transparency"].split(","))
        self.sideCursor = pygame.image.load(
            os.path.join(
                settings.path, "data", root.attrib["sidecursor"])).convert(
                    self.screen)  ##load the side cursor image, and convert
        self.sideCursor.set_colorkey(self.transparency)  ##set the transparency

        width = max(
            map(self.font.calcWidth,
                self.choices)) + self.border * 2 + self.sideCursor.get_width()
        self.size = (width, (self.border * 2) +
                     (self.font.height * len(self.choices)) +
                     (self.lineBuffer * (len(self.choices) - 1)))
        self.location = (self.screen.get_width() - self.size[0] -
                         self.objectBuffer, self.objectBuffer)

        self.box = box.Box(self.size)

        i = 0
        for choice in self.choices:
            location = self.border + self.sideCursor.get_width(), (
                i * (self.font.height + self.lineBuffer)) + self.border
            self.font.writeText(choice, self.box, location)
            i += 1

        self.busy = True
Exemplo n.º 9
0
   def __init__(self, moveId):
      self.moveId = moveId
      
      fn = os.path.join(settings.path, "data", globs.MOVES)
      root = data.getTreeRoot(fn)

      self.moveNode = None
      for m in data.getChildren(root, "move"):
         if data.getAttr(m, "id", data.D_STRING) == moveId:
            self.moveNode = m
            break
      if self.moveNode is None:
         raise error.DittoInvalidResourceException(fn, "MOVE %s" % self.moveId)

      self.name = data.getAttr(self.moveNode, "name", data.D_STRING)
      self.maxPP = data.getAttr(self.moveNode, "pp", data.D_INT)
      self.currPP = self.maxPP
Exemplo n.º 10
0
   def setup(self, game):
      self.symbols = symbols.Symbols(game, self)

      self.script = None
      self.result = False
      self.currentCmd = 0

      self.caller = None

      self.waitingFor = None

      #determine behaviour scripts
      root = data.getTreeRoot(globs.BEHAVIOURS)
      self.behaviours = {}
      for behaviourNode in data.getChildren(root, "behaviour"):
         i = data.getAttr(behaviourNode, "index", data.D_INT)
         s = data.getChild(behaviourNode, "script")
         self.behaviours[i] = script.Script(s, False)
Exemplo n.º 11
0
    def __init__(self, moveId):
        self.moveId = moveId

        fn = os.path.join(settings.path, "data", globs.MOVES)
        root = data.getTreeRoot(fn)

        self.moveNode = None
        for m in data.getChildren(root, "move"):
            if data.getAttr(m, "id", data.D_STRING) == moveId:
                self.moveNode = m
                break
        if self.moveNode is None:
            raise error.DittoInvalidResourceException(fn,
                                                      "MOVE %s" % self.moveId)

        self.name = data.getAttr(self.moveNode, "name", data.D_STRING)
        self.maxPP = data.getAttr(self.moveNode, "pp", data.D_INT)
        self.currPP = self.maxPP
Exemplo n.º 12
0
    def setup(self, game):
        self.symbols = symbols.Symbols(game, self)

        self.script = None
        self.result = False
        self.currentCmd = 0

        self.caller = None

        self.waitingFor = None

        #determine behaviour scripts
        root = data.getTreeRoot(globs.BEHAVIOURS)
        self.behaviours = {}
        for behaviourNode in data.getChildren(root, "behaviour"):
            i = data.getAttr(behaviourNode, "index", data.D_INT)
            s = data.getChild(behaviourNode, "script")
            self.behaviours[i] = script.Script(s, False)
Exemplo n.º 13
0
    def __init__(self, screen, savegame=None):
        """
      Open the save file (or make a new one) and create camera and script engine.

      screen - the screen to render to.
      sound - the sound manager.
      savegame - path to the save file to open, or None to start a new game.
      """

        #store variables for use later
        self.screen = screen

        #parse the game XML file
        fn = os.path.join(settings.path, "data", "game.xml")
        root = data.getTreeRoot(fn, "Ditto main")
        for p in data.getChildren(root, "property"):
            if data.getAttr(p, "name", data.D_STRING) == "menu":
                self.menuPath = data.getAttr(p, "value", data.D_STRING)

        #create script engine and initialise variables
        self.scriptEngine = script_engine.ScriptEngine()
        self.scriptEngine.setup(self)
        self.paused = False
        self.foregroundObject = None

        #if there's a save, open it
        #else start a new game
        if savegame is not None:
            self.openSave(savegame, root)
        else:
            self.newGame(os.path.join(settings.path, "saves", "ditto.sav"),
                         root)

        #put the player onto the map and play music
        self.map.sprites["PLAYER"] = self.player
        sound.playMusic(self.player.map.music)

        #create a camera and attach to the player
        self.camera = camera.Camera(screen)
        self.camera.attachTo(self.player)
Exemplo n.º 14
0
    def __init__(self, screen, savegame=None):
        """
      Open the save file (or make a new one) and create camera and script engine.

      screen - the screen to render to.
      sound - the sound manager.
      savegame - path to the save file to open, or None to start a new game.
      """

        # store variables for use later
        self.screen = screen

        # parse the game XML file
        fn = os.path.join(settings.path, "data", "game.xml")
        root = data.getTreeRoot(fn, "Ditto main")
        for p in data.getChildren(root, "property"):
            if data.getAttr(p, "name", data.D_STRING) == "menu":
                self.menuPath = data.getAttr(p, "value", data.D_STRING)

        # create script engine and initialise variables
        self.scriptEngine = script_engine.ScriptEngine()
        self.scriptEngine.setup(self)
        self.paused = False
        self.foregroundObject = None

        # if there's a save, open it
        # else start a new game
        if savegame is not None:
            self.openSave(savegame, root)
        else:
            self.newGame(os.path.join(settings.path, "saves", "ditto.sav"), root)

        # put the player onto the map and play music
        self.map.sprites["PLAYER"] = self.player
        sound.playMusic(self.player.map.music)

        # create a camera and attach to the player
        self.camera = camera.Camera(screen)
        self.camera.attachTo(self.player)
Exemplo n.º 15
0
   def __init__(self, screen):
      """
      Load all intro screens and start the first.

      screen - the screen to blit to.
      """

      #store screen for use later
      self.screen = screen

      #parse the intro XML file
      fn = os.path.join(settings.path, "data", "intro.xml")
      root = data.getTreeRoot(fn, "Ditto main")

      #create each intro screen
      self.introScreens = []
      for s in data.getChildren(root, "screen"):
         self.introScreens.append(IntroScreen(screen, s))

      #set the first screen active
      self.current = 0
      self.activeScreen = self.introScreens[self.current]
      self.activeScreen.onShow()
Exemplo n.º 16
0
    def __init__(self, screen):
        """
      Load all intro screens and start the first.

      screen - the screen to blit to.
      """

        #store screen for use later
        self.screen = screen

        #parse the intro XML file
        fn = os.path.join(settings.path, "data", "intro.xml")
        root = data.getTreeRoot(fn, "Ditto main")

        #create each intro screen
        self.introScreens = []
        for s in data.getChildren(root, "screen"):
            self.introScreens.append(IntroScreen(screen, s))

        #set the first screen active
        self.current = 0
        self.activeScreen = self.introScreens[self.current]
        self.activeScreen.onShow()
Exemplo n.º 17
0
   def __init__(self, fn):
      """
      Load the image and cut out the individual characters.

      fn - the font XML file.
      """

      #parse the XML file
      root = data.getTreeRoot(fn)
      path = os.path.join(settings.path, "data", data.getAttr(root, "file", data.D_STRING))
      self.height = data.getAttr(root, "height", data.D_INT)
      transparency = data.getAttr(root, "transparency", data.D_INT3LIST)

      #load the image
      image = data.getImage(path, fn)
      image.set_colorkey(transparency)

      #cut out the tiles and store them in a dictionary
      self.characters = {}
      for c in data.getChildren(root, "character"):
         char = data.getAttr(c, "char", data.D_STRING)
         width = data.getAttr(c, "width", data.D_INT)
         location = data.getAttr(c, "location", data.D_INT2LIST)
         self.characters[char] = image.subsurface(location[0], location[1], width, self.height)
Exemplo n.º 18
0
    def __init__(self, species, level):
        self.species = species

        fn = os.path.join(settings.path, "data", globs.POKEMON)
        root = data.getTreeRoot(fn, "Ditto main")

        for sp in data.getChildren(root, "species"):
            if data.getAttr(sp, "id", data.D_STRING) == self.species:
                self.speciesNode = sp
                break

        self.nickname = None

        self.level = level
        self.exp = 0

        typeNode = data.getChild(self.speciesNode, "type")
        self.type1 = data.getAttr(typeNode, "primary", data.D_STRING)
        self.type2 = data.getOptionalAttr(typeNode, "secondary", data.D_STRING,
                                          None)

        self.PID = random.randint(0, 4294967295)
        self.trainer = None
        self.trainerID = None

        self.ability = None

        self.gender = G_MALE
        self.nature = 0
        self.shiny = False

        self.form = 0
        self.happiness = 0
        self.pokerus = False

        self.EVs = {
            ST_HP: 0,
            ST_ATTACK: 0,
            ST_DEFENSE: 0,
            ST_SPEED: 0,
            ST_SPATTACK: 0,
            ST_SPDEFENSE: 0
        }

        self.IVs = {
            ST_HP: random.randint(0, 31),
            ST_ATTACK: random.randint(0, 31),
            ST_DEFENSE: random.randint(0, 31),
            ST_SPEED: random.randint(0, 31),
            ST_SPATTACK: random.randint(0, 31),
            ST_SPDEFENSE: random.randint(0, 31)
        }

        self.stats = {}
        self.calcStats()

        self.currentHP = self.stats[ST_HP]
        self.status = None

        self.moves = [None, None, None, None]
        movesNode = data.getChild(self.speciesNode, "attacks")
        moveNodes = sorted(data.getChildren(movesNode, "move"),
                           key=lambda n: int(n.attrib["level"]))
        i = 0
        for node in moveNodes[-4:]:
            self.moves[i] = moves.Move(data.getAttr(node, "id", data.D_STRING))
            i += 1

        self.ballCaughtIn = 0

        self.heldItem = None
Exemplo n.º 19
0
    def __init__(self, fn):
        """
      Open the map data file, set border tiles and connections, and add NPCs and other events.

      fn - the filename of the map XML file.
      """

        #for the scripting engine
        script_engine.ScriptableObject.__init__(self)

        #store variables we'll need later
        self.fn = fn

        #get a script engine (singleton)
        self.scriptEngine = script_engine.ScriptEngine()

        #parse the XML file
        root = data.getTreeRoot(fn)
        self.music = os.path.join(settings.path, "data",
                                  data.getAttr(root, "music", data.D_STRING))

        #open the actual map data file to create the map tile data
        mapPath = os.path.join(settings.path, "data",
                               data.getAttr(root, "file", data.D_STRING))
        self.openMap(mapPath)

        #create the tileset
        tilesetPath = os.path.join(
            settings.path, "data", data.getAttr(root, "tileset",
                                                data.D_STRING))
        self.tileset = tileset.Tileset(tilesetPath)

        #set the border tiles
        self.borderTiles = {}
        borderNode = data.getChild(root, "border")

        #set each border node with the correct tile indexes, subtracting 1 because the tileset starts at 1 not 0
        self.borderTiles[BD_NW] = data.getAttr(borderNode, "nw",
                                               data.D_INT) - 1
        self.borderTiles[BD_NE] = data.getAttr(borderNode, "ne",
                                               data.D_INT) - 1
        self.borderTiles[BD_SW] = data.getAttr(borderNode, "sw",
                                               data.D_INT) - 1
        self.borderTiles[BD_SE] = data.getAttr(borderNode, "se",
                                               data.D_INT) - 1

        #create any connections from the map
        #connected maps will not be loaded until the map becomes the main game map
        #connections are stored as {direction: (filename, offset)}
        self.connections = {}
        self.connectedMaps = {}
        for c in data.getChildren(root, "connection"):
            side = data.getAttr(c, "side", data.D_STRING)
            fp = os.path.join(settings.path, "data",
                              data.getAttr(c, "map", data.D_STRING))
            offset = data.getAttr(c, "offset", data.D_INT)

            if side == "left":
                self.connections[sprite.DIR_LEFT] = (fp, offset)
            elif side == "right":
                self.connections[sprite.DIR_RIGHT] = (fp, offset)
            elif side == "up":
                self.connections[sprite.DIR_UP] = (fp, offset)
            elif side == "down":
                self.connections[sprite.DIR_DOWN] = (fp, offset)

        #create any NPCs, adding them to the sprite dictionary
        self.sprites = {}
        for n in data.getChildren(root, "npc"):
            spr = npc.NPC(n, self)
            self.sprites[spr.id] = spr

        #create a dictionary to hold positions reserved by moving sprites
        self.reservedPositions = {}

        #create script and warp events, adding them to the events dictionary
        #if a load script is defined, create it
        self.events = {}
        loadScript = None
        for s in data.getChildren(root, "script"):
            trigger = data.getAttr(s, "trigger", data.D_STRING)
            if trigger == "load":
                loadScript = script_engine.Script(s)
            else:
                position = tuple(data.getAttr(s, "position", data.D_INT2LIST))
                self.events[position] = events.ScriptEvent(s, self)

        for w in root.findall("warp"):
            position = tuple(data.getAttr(w, "position", data.D_INT2LIST))
            self.events[position] = events.Warp(w, self)

        #if there is a load script, run it
        if loadScript is not None:
            self.scriptEngine.run(loadScript, self)
Exemplo n.º 20
0
   def __init__(self, size, fn=None):
      """
      Initialize a surface and draw the box onto it.

      size - the size of the box.
      fn - the path to a box xml config file.
      """

      #initialize the pygame Surface
      pygame.Surface.__init__(self, size)

      #if no filename given, use the game default box
      if fn == None:
         fn = os.path.join(settings.path, "data", globs.DIALOG)

      #parse the box xml file
      root = data.getTreeRoot(fn)
      tilesetPath = os.path.join(settings.path, "data", data.getAttr(root, "file", data.D_STRING))
      tileset = data.getImage(tilesetPath)
      transparency = data.getAttr(root, "transparency", data.D_INT3LIST)
      tileset.set_colorkey(transparency)
      tileSize = data.getAttr(root, "tilesize", data.D_INT2LIST)
      data.check((tileSize[0]+1)*3==tileset.get_width()+1, tilesetPath)
      data.check((tileSize[1]+1)*3==tileset.get_height()+1, tilesetPath)

      #fill transparent
      self.fill((255,0,255))
      self.set_colorkey((255,0,255))

      #cut each of the nine tiles out from the tileset
      tileNW = tileset.subsurface((0,0), tileSize)
      tileN = tileset.subsurface((tileSize[0]+1,0), tileSize)
      tileNE = tileset.subsurface(((tileSize[0]+1)*2,0), tileSize)
      tileW = tileset.subsurface((0,tileSize[1]+1), tileSize)
      tileC = tileset.subsurface((tileSize[0]+1,tileSize[1]+1), tileSize)
      tileE = tileset.subsurface(((tileSize[0]+1)*2,tileSize[1]+1), tileSize)
      tileSW = tileset.subsurface((0,(tileSize[1]+1)*2), tileSize)
      tileS = tileset.subsurface((tileSize[0]+1,(tileSize[1]+1)*2), tileSize)
      tileSE = tileset.subsurface(((tileSize[0]+1)*2,(tileSize[1]+1)*2), tileSize)

      #calculate how much of the box is not covered by edge tiles - all this middle must be covered by the centre tile
      #work out how many tiles it will take to cover that, and where to start drawing from
      middleSize = size[0]-(2*tileSize[0]), size[1]-(2*tileSize[1])
      dimensions = (middleSize[0]/tileSize[0])+1, (middleSize[1]/tileSize[1])+1
      origin = (size[0]-(dimensions[0]*tileSize[0]))/2, (size[1]-(dimensions[1]*tileSize[1]))/2

      #iterate over the required dimensions, drawing in the centre tiles
      #as we go down the first column only, draw in the left and right side tiles on the edge of the box
      #after we finish each column, draw the top and bottom tiles on the edge
      for x in range(0, dimensions[0]):
         for y in range(0, dimensions[1]):
            self.blit(tileC, (origin[0]+(x*tileSize[0]), origin[1]+(y*tileSize[1])))
            if x == 0:
               self.blit(tileW, (0, origin[1]+(y*tileSize[1])))
               self.blit(tileE, (size[0]-tileSize[0], origin[1]+(y*tileSize[1])))
         self.blit(tileN, (origin[0]+(x*tileSize[0]), 0))
         self.blit(tileS, (origin[0]+(x*tileSize[0]), size[1]-tileSize[1]))

      #draw the corner tiles in the corners
      self.blit(tileNW, (0, 0))
      self.blit(tileNE, (size[0]-tileSize[0], 0))
      self.blit(tileSW, (0, size[1]-tileSize[1]))
      self.blit(tileSE, (size[0]-tileSize[0], size[1]-tileSize[1]))
Exemplo n.º 21
0
   def __init__(self, species, level):
      self.species = species

      fn = os.path.join(settings.path, "data", globs.POKEMON)
      root = data.getTreeRoot(fn, "Ditto main")

      for sp in data.getChildren(root, "species"):
         if data.getAttr(sp, "id", data.D_STRING) == self.species:
            self.speciesNode = sp
            break                           
      
      self.nickname = None
      
      self.level = level
      self.exp = 0

      typeNode = data.getChild(self.speciesNode, "type")
      self.type1 = data.getAttr(typeNode, "primary", data.D_STRING)
      self.type2 = data.getOptionalAttr(typeNode, "secondary", data.D_STRING, None)

      self.PID = random.randint(0, 4294967295)
      self.trainer = None
      self.trainerID = None

      self.ability = None

      self.gender = G_MALE
      self.nature = 0
      self.shiny = False

      self.form = 0
      self.happiness = 0
      self.pokerus = False
      
      self.EVs = {ST_HP: 0,
                  ST_ATTACK: 0,
                  ST_DEFENSE: 0,
                  ST_SPEED: 0,
                  ST_SPATTACK: 0,
                  ST_SPDEFENSE: 0}
      
      self.IVs = {ST_HP: random.randint(0,31),
                  ST_ATTACK: random.randint(0,31),
                  ST_DEFENSE: random.randint(0,31),
                  ST_SPEED: random.randint(0,31),
                  ST_SPATTACK: random.randint(0,31),
                  ST_SPDEFENSE: random.randint(0,31)}

      self.stats = {}
      self.calcStats()

      self.currentHP = self.stats[ST_HP]
      self.status = None

      self.moves = [None, None, None, None]
      movesNode = data.getChild(self.speciesNode, "attacks")
      moveNodes = sorted(data.getChildren(movesNode, "move"), key=lambda n: int(n.attrib["level"]))
      i = 0
      for node in moveNodes[-4:]:
         self.moves[i] = moves.Move(data.getAttr(node, "id", data.D_STRING))
         i += 1
      
      self.ballCaughtIn = 0

      self.heldItem = None
Exemplo n.º 22
0
   def __init__(self, fn):
      """
      Open the map data file, set border tiles and connections, and add NPCs and other events.

      fn - the filename of the map XML file.
      """

      #for the scripting engine
      script_engine.ScriptableObject.__init__(self)

      #store variables we'll need later
      self.fn = fn

      #get a script engine (singleton)
      self.scriptEngine = script_engine.ScriptEngine()

      #parse the XML file
      root = data.getTreeRoot(fn)
      self.music = os.path.join(settings.path, "data", data.getAttr(root, "music", data.D_STRING))

      #open the actual map data file to create the map tile data
      mapPath = os.path.join(settings.path, "data", data.getAttr(root, "file", data.D_STRING))
      self.openMap(mapPath)

      #create the tileset
      tilesetPath = os.path.join(settings.path, "data", data.getAttr(root, "tileset", data.D_STRING))
      self.tileset = tileset.Tileset(tilesetPath)

      #set the border tiles
      self.borderTiles = {}
      borderNode = data.getChild(root, "border")

      #set each border node with the correct tile indexes, subtracting 1 because the tileset starts at 1 not 0
      self.borderTiles[BD_NW] = data.getAttr(borderNode, "nw", data.D_INT)-1
      self.borderTiles[BD_NE] = data.getAttr(borderNode, "ne", data.D_INT)-1
      self.borderTiles[BD_SW] = data.getAttr(borderNode, "sw", data.D_INT)-1
      self.borderTiles[BD_SE] = data.getAttr(borderNode, "se", data.D_INT)-1
      
      #create any connections from the map
      #connected maps will not be loaded until the map becomes the main game map
      #connections are stored as {direction: (filename, offset)}
      self.connections = {}
      self.connectedMaps = {}
      for c in data.getChildren(root, "connection"):
         side = data.getAttr(c, "side", data.D_STRING)
         fp = os.path.join(settings.path, "data", data.getAttr(c, "map", data.D_STRING))
         offset = data.getAttr(c, "offset", data.D_INT)
         
         if side == "left":
            self.connections[sprite.DIR_LEFT] = (fp, offset)
         elif side == "right":
            self.connections[sprite.DIR_RIGHT] = (fp, offset)
         elif side == "up":
            self.connections[sprite.DIR_UP] = (fp, offset)
         elif side == "down":
            self.connections[sprite.DIR_DOWN] = (fp, offset)

      #create any NPCs, adding them to the sprite dictionary
      self.sprites = {}
      for n in data.getChildren(root, "npc"):
         spr = npc.NPC(n, self)
         self.sprites[spr.id] = spr

      #create a dictionary to hold positions reserved by moving sprites
      self.reservedPositions = {}

      #create script and warp events, adding them to the events dictionary
      #if a load script is defined, create it
      self.events = {}
      loadScript = None
      for s in data.getChildren(root, "script"):
         trigger = data.getAttr(s, "trigger", data.D_STRING)
         if trigger == "load":
            loadScript = script_engine.Script(s)   
         else:
            position = tuple(data.getAttr(s, "position", data.D_INT2LIST)) 
            self.events[position] = events.ScriptEvent(s, self)
            
      for w in root.findall("warp"):
         position = tuple(data.getAttr(w, "position", data.D_INT2LIST))
         self.events[position] = events.Warp(w, self)

      #if there is a load script, run it
      if loadScript is not None:
         self.scriptEngine.run(loadScript, self)