Question

I'm working on a custom tiled map loader. Seems to work fine, I don't get any errors, but the screen only shows up 1 tile of each type.

this is the file structure:

/main.py 
/other/render2.py 
/other/render.py

here's the render2.py file:

import pyglet, json
from pyglet.window import key
from pyglet.gl import *
from ConfigParser import SafeConfigParser
from cocos.layer import *
from cocos.batch import *
from cocos.sprite import Sprite

class renderer( Layer ):
    #init function
    def __init__(self):
        super( renderer, self ).__init__()

    #call function, returns the map as a list of sprites, and coordinates
    def __call__(self, mapname):

        #runs the map file parser
        parser = SafeConfigParser()

        #reads the map file
        try:
            world = parser.read('maps/'+mapname+'.txt')
            print world
        except IOError:
            return

        #These variables the config from the map file
        tileSize = int(parser.get('config', 'tilesize'))
        layers = int(parser.get('config', 'layers'))

        mapList = []

        #the super mega advanced operation to render the mapList
        for i in range(0,layers):
            layer = json.loads(parser.get('layer'+str(i), 'map'))
            tileType = parser.get('layer'+str(i), 'tiletype')
            nTiles = int(parser.get('layer'+str(i), 'tiles'))
            tileSet  = []

            #this over here loads all 16 tiles of one type into tileSet
            for n in range(0, nTiles):
                tileSet.append(Sprite("image/tiles/"+tileType+"/"+str(n)+".png", scale = 1, anchor = (0,0)))

            for x in range(0, len(layer)):
                for y in range(0, len(layer[x])):
                    X = (x*tileSize)
                    Y = (y*tileSize)
                    total = [tileSet[layer[x][y]], i, X, Y]
                    print layer[x][y], tileSet[layer[x][y]]
                    mapList.append(total)
        return mapList

This is an example of what this returns :

 [<cocos.sprite.Sprite object at 0x060910B0>, 0, 0,0 ]
 [<cocos.sprite.Sprite object at 0x060910B0> , 0, 64,64 ]

It returns a huge list with a lot of sublists like these in it.

when I call it from the main.py file, it only draws the last tile of each kind. here's the main.py file:

import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

import pyglet
import threading,time
from pyglet import clock
from pyglet.gl import *
from cocos.director import *
from cocos.menu import *
from cocos.scene import *
from cocos.layer import *
from cocos.actions import *
from cocos.batch import *
from cocos.sprite import Sprite
from other.render2 import renderer
import random; rr = random.randrange
class Background(ScrollableLayer):
    def __init__(self):
        super(Background, self).__init__()
        world = renderer()
        bg = world('sampleidea')


        batch = BatchNode()
        for i in range(0, len(bg)):
            l= bg[i][1]
            x= bg[i][2]
            y= bg[i][3]

            spr = bg[i][0]
            spr.position =(x,y)
            batch.add(spr, z = l)

        self.add(batch)
class Menu(Layer):
    def __init__(self):
        super(Menu, self).__init__()
        title = Sprite('image/title.png' )
        title.position = (400,520)
        self.add( title )


def start():
    director.set_depth_test()
    background = Background()
    menu = Menu()
    scene = Scene(background, menu)
    return scene

def init():
    director.init( do_not_scale=True, resizable=True, width=1280, height=720)
def run(scene):
    director.run( scene )

if __name__ == "__main__":
    init()
    s = start()
    run(s)

What am I doing wrong? I have an older render.py, which does work, but I remade it since it loaded each sprite file for each tile. That took way to long to load on big maps.

This is the old render.py I've been using before. It's quite different since it used different map files too.

import pyglet, json
from pyglet.window import key
from pyglet.gl import *
from ConfigParser import SafeConfigParser
from cocos.layer import *
from cocos.batch import *
from cocos.sprite import Sprite

class renderer( Layer ):
    def __init__(self):
        super( renderer, self ).__init__()
    def __call__(self, mapname):
        parser = SafeConfigParser()
        try:
            world = parser.read('maps/'+mapname+'.txt')
            print world
        except IOError:
            print("No world file!")
            return

        tilesize = json.loads(parser.get('data', 'tilesize'))
        world = json.loads(parser.get('data', 'world'))

        maplist = []
        for l in range(len(world)):

            for x in range(len(world[l])):

                for y in range(len(world[l][x])):
                    if world[l][x][y] != None:
                        foldername = str(world[l][x][y][0])
                        imagename = str(world[l][x][y][1])


                        spr = Sprite("image/tiles/"+foldername+"/"+imagename+".png", scale = 1, anchor = (0,0))

                        X = (x*tilesize)
                        Y = (y*tilesize)

                        total = [spr, l, X, Y]

                        maplist.append(total)

        return maplist

Is it possible to make the new "render" to work?

Was it helpful?

Solution

The problem is that my new optimized "renderer" creates a bunch of cocos.sprite.Sprite objects, instead of just loading Image files as i thought it would. The code in my question only repositioned the same sprite object over and over again this way. To solve this, the way to do it is by opening the image with pyglet.image.load(), and creating sprite objects with that. example:

f = pyglet.image.load('sprite.png')
batch = CocosNode()
batch.position = 50, 100
add(batch)
for i in range(0, 200):
    test = Sprite(f)        
    test.position = i*10,i*10
    batch.add( test )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top