def __init__(self, filename): from pytmx import tmxloader self.tiledmap = tmxloader.load_pygame(filename, pixelalpha=True) self.wall_list = pygame.sprite.Group() self.width = self.tiledmap.width * self.tiledmap.tilewidth self.height = self.tiledmap.height * self.tiledmap.tileheight self.bg = pygame.Surface((self.width,self.height)) self.fl = pygame.Surface((self.width,self.height)) self.sp = pygame.Surface((self.width,self.height)) #self.fg = pygame.Surface((self.width,self.height)) self.tp = pygame.Surface((self.width,self.height)) #self.sh1 = pygame.Surface((self.width,self.height)) #self.sh2 = pygame.Surface((self.width,self.height)) self.fl.fill(colors.hotpink) self.sp.fill(colors.hotpink) #self.fg.fill(colors.hotpink) self.tp.fill(colors.hotpink) #self.sh1.fill(colors.hotpink) #self.sh2.fill(colors.hotpink) self.fl.set_colorkey(colors.hotpink) self.sp.set_colorkey(colors.hotpink) #self.fg.set_colorkey(colors.hotpink) self.tp.set_colorkey(colors.hotpink)
def __init__(self, filename): self.tiles = tmxloader.load_pygame(filename, pixelalpha=True) tw = self.tiles.tilewidth th = self.tiles.tileheight gt = self.tiles.getTileImage self.surface = surface.Surface((self.tiles.width * tw, self.tiles.height * th)) self.tile_width = tw self.tile_height = th #draw map for y in xrange(0, self.tiles.height): for x in xrange(0, self.tiles.width): tile = gt(x, y, 0) if tile: self.surface.blit(tile, (x*tw, y*th))
def __init__(self, filename): self.tiles = tmxloader.load_pygame(filename, pixelalpha=True) tw = self.tiles.tilewidth th = self.tiles.tileheight gt = self.tiles.getTileImage self.surface = surface.Surface( (self.tiles.width * tw, self.tiles.height * th)) self.tile_width = tw self.tile_height = th #draw map for y in xrange(0, self.tiles.height): for x in xrange(0, self.tiles.width): tile = gt(x, y, 0) if tile: self.surface.blit(tile, (x * tw, y * th))
def __init__(self, game): screen = game.screen (w,h) = (screen.get_width(), screen.get_height()) tmx_data = load_pygame(getFilePath(game.levelName)) map_data = pyscroll.data.TiledMapData(tmx_data) self.map_layer = pyscroll.BufferedRenderer(map_data, (w,h), padding=1, clamp_camera=True) self.group = PyscrollGroup(map_layer=self.map_layer) self.center = [self.map_layer.rect.width/2, self.map_layer.rect.height/2] self.camera_acc = [0, 0, 0] self.camera_vel = [0, 0, 0] self.last_update = 0 self.screen = screen
def __init__(self, filename): self.tiledmap = tmxloader.load_pygame(filename, pixelalpha=True) self.tw = self.tiledmap.tilewidth self.th = self.tiledmap.tileheight self.width = self.tiledmap.width self.height = self.tiledmap.height self.get_tile_image = self.tiledmap.getTileImage l = self.tiledmap.getTileLayerByName('collition') self.collition_layer = self.tiledmap.tilelayers.index(l) self.layers = [] for layer_name in ('below', 'above'): l = self.tiledmap.getTileLayerByName(layer_name) layer = self.tiledmap.tilelayers.index(l) self.layers.append(layer) self.left_edge = 0 self.top_edge = 64 # height of player self.right_edge = self.tiledmap.width * self.tw self.bottom_edge = self.tiledmap.height * self.th self.objects = self.tiledmap.getObjects() self.tile_rect = pygame.Rect(0,0, self.tw, self.th) """ tiledmap.width and tiledmap.height are int numbers like 70, 80, etc. so in the render function i need to know which tiles collides with camera rect, i can looking for all the tiles "for x in range(self.width):", but that have a low perfomance. For that reason i calculate a view rect which tell me where are the tiles, that i need. level.view_x1 = camera.left / level.tw level.view_x2 = camera.right / level.tw level.view_y1 = camera.top / level.tw level.view_y2 = camera.bottom / level.tw """ self.view_x1 = 0 self.view_x2 = 20 self.view_y1 = 0 self.view_y2 = 15
def loadMap(self): tmxData = load_pygame(self._filename) self.size = tmxData.width * tmxData.tilewidth, tmxData.height * tmxData.tileheight self.width = tmxData.width self.height = tmxData.height self.tileSize = tmxData.tilewidth if tmxData.background_color: self._bgColor = tmxData.background_color # iterate over all the visible layers and create a map layer for each. for layer in tmxData.visible_layers: if isinstance(layer, TiledTileLayer): self._layers.append(TileLayer(tmxData, layer)) elif isinstance(layer, TiledObjectGroup): objFactory = ObjectFactory(tmxData) for obj in layer: sprite = objFactory.construct(obj) self._objects.add(sprite)
def __init__(self, filename): self.tiledmap = tmxloader.load_pygame(filename, pixelalpha=True) self.tw = self.tiledmap.tilewidth self.th = self.tiledmap.tileheight self.width = self.tiledmap.width self.height = self.tiledmap.height self.get_tile_image = self.tiledmap.getTileImage l = self.tiledmap.getTileLayerByName('collition') self.collition_layer = self.tiledmap.tilelayers.index(l) self.layers = [] for layer_name in ('below', 'above'): l = self.tiledmap.getTileLayerByName(layer_name) layer = self.tiledmap.tilelayers.index(l) self.layers.append(layer) self.left_edge = 0 self.top_edge = 64 # height of player self.right_edge = self.tiledmap.width * self.tw self.bottom_edge = self.tiledmap.height * self.th self.objects = self.tiledmap.getObjects() self.tile_rect = pygame.Rect(0, 0, self.tw, self.th) """ tiledmap.width and tiledmap.height are int numbers like 70, 80, etc. so in the render function i need to know which tiles collides with camera rect, i can looking for all the tiles "for x in range(self.width):", but that have a low perfomance. For that reason i calculate a view rect which tell me where are the tiles, that i need. level.view_x1 = camera.left / level.tw level.view_x2 = camera.right / level.tw level.view_y1 = camera.top / level.tw level.view_y2 = camera.bottom / level.tw """ self.view_x1 = 0 self.view_x2 = 20 self.view_y1 = 0 self.view_y2 = 15
def __init__(self, filename): print 'loading', filename self.tiled = tmxloader.load_pygame('maps/' + filename + '.tmx') #print dir(self.tiled) self.events = [[0 for i in range(self.tiled.width)] for j in range(self.tiled.height)] self.monsters = [] for obj in self.tiled.getObjects(): obj.x /= constants.TILESIZE obj.y /= constants.TILESIZE self.events[obj.y][obj.x] = obj if obj.type == 'monster': self.monsters.append(Monster(obj.monType, obj.x, obj.y)) if os.path.exists(os.path.abspath(os.curdir + '/maps/' + filename + '.py')): exec('from maps.{0} import {0} as mapObject'.format(filename)) self.mapScript = mapObject() try: self.name = self.tiled.name except: self.name = ''
How tmxloader.py works from pytmx import tmxloader tmxdata = tmxloader.load_pygame("map.tmx") def load_pygame(): # on line 337 of tmxloader.py # load_pygame does: tmxdata = pytmx.TiledMap(filename) # which is a call to class TiledMap, line 151 of pytmx.py _load_images_pygame(tmxdata, None, *args, **kwargs) # which is a call to the function on line 250 of tmxloader return tmxdata # so the images have been loaded # Then # When you want to draw tiles, you simply call "getTileImage": image = tmxdata.get_tile_image(x coord, y coord, layer number) # tmxdata is a TiledMap(filename) object # def get_tile_image line 233 of pytmx screen.blit(image, position)
def __init__(self, filename): from pytmx import tmxloader self.tiledmap = tmxloader.load_pygame(filename, pixelalpha=True)
def __init__(self, filename): tm = load_pygame(filename) self.size = tm.width * tm.tilewidth, tm.height * tm.tileheight print self.size self.tmx_data = tm
def load_map(self, filename): self.tmxdata = tmxloader.load_pygame(filename)
def main(self,screen): clock = pygame.time.Clock() # loading materials from pytmx import tmxloader tmxdata = tmxloader.load_pygame("../materials/map.tmx", pixelalpha=True) # get trigger layer for og in tmxdata.objectgroups: if hasattr(og,'triggers'): triggers = og # create search graph for pathfinding searchGraph = pathfinding.NodeGraph(triggers) #spawn_1 = searchGraph.start_nodes[0] #spawn_2 = searchGraph.start_nodes[1] creeps = [] # create map with tiles areaMap = Map(tmxdata) # set up camera camera = Camera(screen.get_size(),areaMap.getDimensions()) clock.tick() while True: dt = clock.tick()/1000.0 # calculate time in seconds since last frame #print "fps:", clock.get_fps() mousePosition = pygame.mouse.get_pos() # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: return if event.type == pygame.MOUSEBUTTONDOWN: button_states = pygame.mouse.get_pressed() if button_states[0]: creeps.append(Creep(1,(mousePosition[0]+camera.pos[0],mousePosition[1]+camera.pos[1]))) elif button_states[2]: searchGraph.makeImpassable((mousePosition[0]+camera.pos[0],mousePosition[1]+camera.pos[1])) #if pygame.key.get_pressed()[pygame.K_c]: #creeps.append(Creep(1,spawn_1.pos_x,spawn_1.pos_y)) #creeps.append(Creep(1,spawn_2.pos_x,spawn_2.pos_y)) # mouse scrolling camera.scroll(mousePosition) ############################################################################################### # Game logics ############################################################################################### # move all creeps according to their tiles optimal directions for creep in creeps: d = searchGraph.getDirection(creep.pos) creep.move(d,dt) # check for collisions for creep in creeps: v = searchGraph.handleCollision(creep.pos,creep.size) if v != None: creep.shift(v) # delete nodes that reached endnode for creep in creeps: if searchGraph.atEndNode(creep.pos): creeps.remove(creep) #del creep #print direct #print "creep at:", (goblin.pos_x, goblin.pos_y) ############################################################################################### # Graphics ############################################################################################### # set background color screen.fill((0,0,0)) # draw map to screen, using the settings of the camera areaMap.drawMap(screen,camera) # plot nodes of search graph (for debugging purposes) searchGraph.plotNodes(screen,camera) # draw all towers # draw all creatures for creep in creeps: creep.plot(screen,camera) # draw all special effects (arrows, missles, etc.) # background tileset #---------------------------------------- background.drawMap(screen) pygame.display.flip()
def __init__(self, filename): # load it using pytmx from pytmx import tmxloader self.path = filename self.tiledmap = tmxloader.load_pygame(filename, force_colorkey = PINK_TRANSPARENT) self.spritesheet = self.tiledmap.tilesets[0].source # some useful variables self.map_size = (self.tiledmap.width * self.tiledmap.tilewidth, self.tiledmap.height * self.tiledmap.tileheight) s.tile_width = self.tiledmap.tilewidth s.tile_height = self.tiledmap.tileheight #used to keep in touch with the objects, placeholder self.item_sprites = pygame.sprite.Group() # collision tiles coordinates and sprites self.col_tiles_coords = [] self.one_way_col = [] self.collision_group = pygame.sprite.Group() # single list for all the layers self.layers = [] #used to store tiles representing actions: doors, text signs, climbing tiles... self.actionsprites = pygame.sprite.Group() #------------------------ # Mobs and players. # Sotres all the mobs and the player in the same group, used to # just run mobs.update self.mobs = pygame.sprite.Group() self.hostiles = pygame.sprite.Group() # stores the spawn position of the player, is set by a "Player" # type of object in a object layer called "Player" in the Tiled # map self.player_spawns = {} # which one of the moblayers is the one with the player self.player_layer = None # cameras found in sprites to add to the renderer self.cameras = [] self.debugging = False # get all the layers! all_tiled_layers = self.tiledmap.all_layers for layer in range(len(all_tiled_layers)): tiledlayer = all_tiled_layers[layer] if tiledlayer in self.tiledmap.tilelayers: tile_layer_index = self.tiledmap.tilelayers.index(tiledlayer) if hasattr(tiledlayer, 'Collision'): new_layer = NewCollisionLayer(self.tiledmap, tile_layer_index) else: new_layer = TileLayer(self.tiledmap, tile_layer_index) elif tiledlayer in self.tiledmap.imagelayers: index = self.tiledmap.imagelayers.index(tiledlayer) new_layer = ImageLayer(self.tiledmap, index) elif tiledlayer in self.tiledmap.objectgroups: index = self.tiledmap.objectgroups.index(tiledlayer) new_layer = ObjectLayer(self.tiledmap, index) self.layers.append(new_layer) # final loop to collect all the layer info for layer in self.layers: if isinstance(layer, ObjectLayer): self.get_info_object_layer(layer) elif isinstance(layer, NewCollisionLayer): #~ self.collision_group.add(layer.collision) self.collision_group.add(layer.static) if len(self.collision_group) > 300: depth = 8 else: depth = 6 if self.collision_group.sprites(): self.collision_tree = QuadTree(self.collision_group, depth)
def __init__(self, game, world, mapName): self.mapName = mapName self.world = world self.drawable = [] self.layers = [] self.tilemap = tmxloader.load_pygame("maps/%s/%s/map.tmx" % (self.world, self.mapName), pixelalpha = True) self.width = self.tilemap.width * self.tilemap.tilewidth self.height = self.tilemap.height * self.tilemap.tileheight self.drawShadow = True self.gravity = 750 self.gravityAccel = 25 self.bgColor = (82, 246, 255) self.background = None self.pPosition = vector.Vec2d(0, 0) self.pLayer = 0 self.layerCount = -1 self.hasKeyHoles = False if hasattr(self.tilemap, "shadow"): self.drawShadow = bool(int(self.tilemap.shadow)) if hasattr(self.tilemap, "gravity"): self.gravity = int(self.tilemap.gravity) if hasattr(self.tilemap, "gravityAccel"): self.gravityAccel = int(self.tilemap.gravityAccel) if hasattr(self.tilemap, "rgb"): self.bgColor = tuple([map(int, self.tilemap.rgb.split())]) if hasattr(self.tilemap, "bg"): tmp = self.tilemap.bg.split(":") if tmp[0] == "d": self.background = pygame.image.load("assets/sprites/backgrounds/%s.png" % (tmp[1])).convert_alpha() elif tmp[0] == "w": self.background = pygame.image.load("maps/%s/%s.png" % (self.world, tmp[1])).convert_alpha() elif tmp[0] == "m": self.background = pygame.image.load("maps/%s/%s/%s.png" % (self.world, self.mapName, tmp[1])).convert_alpha() self.bgSize = self.background.get_size() self.bgOffset = (0, 0) for rawLayer in self.tilemap.getTileLayerOrder(): self.layerCount += 1 if rawLayer.visible: layer.Layer(self, rawLayer) self.layerCount += 1 MapText.group = [] for i in self.layers: MapText.group.append([]) for obj in self.tilemap.getObjects(): if obj.name == "spawn": self.pPosition.x = obj.x self.pPosition.y = obj.y if hasattr(obj, "layer"): self.pLayer = int(obj.layer) elif obj.name == "enemy": eLayer = 0 enemyType = "enemyBlue" if hasattr(obj, "layer"): eLayer = int(obj.layer) if hasattr(obj, "color"): enemyType = "enemy" + obj.color.capitalize() enemy.Enemy(game, self, enemyType, vector.Vec2d(obj.x, obj.y), eLayer) elif obj.name == "item": iLayer = 0 itemType = "keyYellow" if hasattr(obj, "layer"): iLayer = int(obj.layer) if hasattr(obj, "color"): itemType = "key" + obj.color.capitalize() item.Item(game, self, itemType, vector.Vec2d(obj.x, obj.y), iLayer) elif obj.name == "text": text = "" tLayer = 0 if hasattr(obj, "text"): text = obj.text if hasattr(obj, "layer"): tLayer = int(obj.layer) MapText(game, text, tLayer, vector.Vec2d(obj.x, obj.y), obj.width) elif obj.name == "title": tLayer = 0 if hasattr(obj, "layer"): tLayer = int(obj.layer) MapText(game, self.world + " | " + self.mapName, tLayer, vector.Vec2d(obj.x, obj.y), obj.width)
def __init__(self, filename): self.tiledmap = tmxloader.load_pygame(filename, pixelalpha=True) self.boxcollider = [] self.alphacollider = []