def to_p8_file(self, outstr, lua_writer_cls=None, lua_writer_args=None,
                   filename=None):
        outstr.write(HEADER_TITLE_STR)

        # Even though we can get the original cart version, we
        # hard-code version 5 for output because we only know how to
        # write v5 .p8 files. There are minor changes from previous
        # versions of .p8 that don't apply to .p8.png (such as the gff
        # section).
        outstr.write(HEADER_VERSION_PAT.format(5))

        # Sanity-check the Lua written by the writer.
        transformed_lua = Lua.from_lines(
            self.lua.to_lines(writer_cls=lua_writer_cls,
                              writer_args=lua_writer_args),
            version=(self.version or 0))
        if transformed_lua.get_char_count() > PICO8_LUA_CHAR_LIMIT:
            if filename is not None:
                util.error('{}: '.format(filename))
            util.error('warning: character count {} exceeds the Pico-8 '
                       'limit of {}'.format(
                transformed_lua.get_char_count(),
                PICO8_LUA_CHAR_LIMIT))
        if transformed_lua.get_token_count() > PICO8_LUA_TOKEN_LIMIT:
            if filename is not None:
                util.error('{}: '.format(filename))
            util.error('warning: token count {} exceeds the Pico-8 '
                       'limit of {}'.format(
                transformed_lua.get_char_count(),
                PICO8_LUA_CHAR_LIMIT))

        outstr.write(SECTION_DELIM_PAT.format('lua'))
        ended_in_newline = None
        for l in self.lua.to_lines(writer_cls=lua_writer_cls,
                                   writer_args=lua_writer_args):
            outstr.write(l)
            ended_in_newline = l.endswith('\n')
        if not ended_in_newline:
            outstr.write('\n')

        outstr.write(SECTION_DELIM_PAT.format('gfx'))
        for l in self.gfx.to_lines():
            outstr.write(l)

        outstr.write(SECTION_DELIM_PAT.format('gff'))
        for l in self.gff.to_lines():
            outstr.write(l)

        outstr.write(SECTION_DELIM_PAT.format('map'))
        for l in self.map.to_lines():
            outstr.write(l)

        outstr.write(SECTION_DELIM_PAT.format('sfx'))
        for l in self.sfx.to_lines():
            outstr.write(l)

        outstr.write(SECTION_DELIM_PAT.format('music'))
        for l in self.music.to_lines():
            outstr.write(l)

        outstr.write('\n')
Write the game data as a .p8 file.
dataset/ETHPy150Open dansanderson/picotool/pico8/game/game.py/Game.to_p8_file