def __init__(self, *args, **kwargs): super(MyWitch, self).__init__(*args, **kwargs) self.fullmap = cocos.tiles.load('maps/platformer-map.xml') # add the walls (labyrinth) self.walls = self.fullmap['walls'] self.add(self.walls, z=0) # add the items (bushes cauldron) self.decoration = self.fullmap['decoration'] self.add(self.decoration, z=1) # add the player player_layer = layer.ScrollableLayer() self.player = cocos.sprite.Sprite('img/witch-standing.png') self.player.do(MyWitchController()) player_layer.add(self.player) self.add(player_layer, z=2) # set the player start position using the player_start token from the map start = self.decoration.find_cells(player_start=True)[0] r = self.player.get_rect() r.midbottom = start.midbottom self.player.position = r.center # give a collision handler to the player mapcollider = cocos.mapcolliders.RectMapCollider( velocity_on_bump='slide') self.player.collision_handler = mapcolliders.make_collision_handler( mapcollider, self.walls)
def main(): global keyboard, tilemap, scroller from cocos.director import director director.init(width=800, height=600, autoscale=False) print(description) # create a layer to put the player in player_layer = layer.ScrollableLayer() # NOTE: the anchor for this sprite is in the CENTER (the cocos default) # which means all positioning must be done using the center of its rect player = cocos.sprite.Sprite('witch-standing.png') player_layer.add(player) player.do(PlatformerController()) # add the tilemaps and the player sprite layer to a scrolling manager scroller = layer.ScrollingManager() fullmap = tiles.load('platformer-map.xml') tilemap_walls = fullmap['walls'] scroller.add(tilemap_walls, z=0) tilemap_decoration = fullmap['decoration'] scroller.add(tilemap_decoration, z=1) scroller.add(player_layer, z=2) # set the player start using the player_start token from the map start = tilemap_decoration.find_cells(player_start=True)[0] r = player.get_rect() # align the mid bottom of the player with the mid bottom of the start cell r.midbottom = start.midbottom # player image anchor (position) is in the center of the sprite player.position = r.center # give a collision handler to the player mapcollider = mapcolliders.RectMapCollider(velocity_on_bump='slide') player.collision_handler = mapcolliders.make_collision_handler( mapcollider, tilemap_walls) # construct the scene with a background layer color and the scrolling layers platformer_scene = cocos.scene.Scene() platformer_scene.add(layer.ColorLayer(100, 120, 150, 255), z=0) platformer_scene.add(scroller, z=1) # track keyboard presses keyboard = key.KeyStateHandler() director.window.push_handlers(keyboard) # allow display info about cells / tiles def on_key_press(key, modifier): if key == pyglet.window.key.D: tilemap_walls.set_debug(True) director.window.push_handlers(on_key_press) # run the scene director.run(platformer_scene)
def main(): global keyboard, tilemap, scroller from cocos.director import director director.init(width=800, height=600, autoscale=False) print(description) # create a layer to put the player in player_layer = layer.ScrollableLayer() # NOTE: the anchor for this sprite is in the CENTER (the cocos default) # which means all positioning must be done using the center of its rect player = cocos.sprite.Sprite('witch-standing.png') player_layer.add(player) player.do(PlatformerController()) # add the tilemaps and the player sprite layer to a scrolling manager scroller = layer.ScrollingManager() fullmap = tiles.load('platformer-map.xml') tilemap_walls = fullmap['walls'] scroller.add(tilemap_walls, z=0) tilemap_decoration = fullmap['decoration'] scroller.add(tilemap_decoration, z=1) scroller.add(player_layer, z=2) # set the player start using the player_start token from the map start = tilemap_decoration.find_cells(player_start=True)[0] r = player.get_rect() # align the mid bottom of the player with the mid bottom of the start cell r.midbottom = start.midbottom # player image anchor (position) is in the center of the sprite player.position = r.center # give a collision handler to the player mapcollider = mapcolliders.RectMapCollider(velocity_on_bump='slide') player.collision_handler = mapcolliders.make_collision_handler( mapcollider, tilemap_walls) # construct the scene with a background layer color and the scrolling layers platformer_scene = cocos.scene.Scene() platformer_scene.add(layer.ColorLayer(100, 120, 150, 255), z=0) platformer_scene.add(scroller, z=1) # track keyboard presses keyboard = key.KeyStateHandler() director.window.push_handlers(keyboard) # allow display info about cells / tiles def on_key_press(key, modifier): if key == pyglet.window.key.D: tilemap.set_debug(True) director.window.push_handlers(on_key_press) # run the scene director.run(platformer_scene)
def main(): global keyboard, walls, scroller from cocos.director import director director.init(width=800, height=600, autoscale=False) print(description) # create the scrolling manager that will hold all game entities scroller = layer.ScrollingManager() # load the map layer of interest and add to the level walls = tiles.load('tmx_collision.tmx')['walls'] assert isinstance(walls, tiles.TmxObjectLayer) scroller.add(walls, z=0) # create a layer to put the player in; it will care for player scroll player_layer = layer.ScrollableLayer() # NOTE: the anchor for this sprite is in the CENTER (the cocos default) # which means all positioning must be done using the center of its rect player = cocos.sprite.Sprite('witch-standing.png') player_layer.add(player) player.do(PlatformerController2()) scroller.add(player_layer, z=1) # set the player start using the object with the 'player_start' property player_start = walls.find_cells(player_start=True)[0] # for convenience the player start was give the same dimensions as player, # so put player.center over player start center player.position = player_start.center # set focus so the player is in view scroller.set_focus(*player.position) # extract the player_start, which is not a wall walls.objects.remove(player_start) # give a collision handler to the player mapcollider = mapcolliders.TmxObjectMapCollider() mapcollider.on_bump_handler = mapcollider.on_bump_slide player.collision_handler = mapcolliders.make_collision_handler( mapcollider, walls) # construct the scene with a background layer color and the scrolling layers platformer_scene = cocos.scene.Scene() platformer_scene.add(layer.ColorLayer(100, 120, 150, 255), z=0) platformer_scene.add(scroller, z=1) # track keyboard presses keyboard = key.KeyStateHandler() director.window.push_handlers(keyboard) # run the scene director.run(platformer_scene)
def main(): global keyboard, walls, scroller from cocos.director import director director.init(width=800, height=600, autoscale=False) print(description) # create the scrolling manager that will hold all game entities scroller = layer.ScrollingManager() # load the map layer of interest and add to the level walls = tiles.load('tmx_collision.tmx')['walls'] assert isinstance(walls, tiles.TmxObjectLayer) scroller.add(walls, z=0) # create a layer to put the player in; it will care for player scroll player_layer = layer.ScrollableLayer() # NOTE: the anchor for this sprite is in the CENTER (the cocos default) # which means all positioning must be done using the center of its rect player = cocos.sprite.Sprite('witch-standing.png') player_layer.add(player) player.do(PlatformerController2()) scroller.add(player_layer, z=1) # set the player start using the object with the 'player_start' property player_start = walls.find_cells(player_start=True)[0] # for convenience the player start was give the same dimensions as player, # so put player.center over player start center player.position = player_start.center # set focus so the player is in view scroller.set_focus(*player.position) # extract the player_start, which is not a wall walls.objects.remove(player_start) # give a collision handler to the player mapcollider = mapcolliders.TmxObjectMapCollider() mapcollider.on_bump_handler = mapcollider.on_bump_slide player.collision_handler = mapcolliders.make_collision_handler(mapcollider, walls) # construct the scene with a background layer color and the scrolling layers platformer_scene = cocos.scene.Scene() platformer_scene.add(layer.ColorLayer(100, 120, 150, 255), z=0) platformer_scene.add(scroller, z=1) # track keyboard presses keyboard = key.KeyStateHandler() director.window.push_handlers(keyboard) # run the scene director.run(platformer_scene)
def main(): global keyboard, walls, scroller from cocos.director import director director.init(width=800, height=600, autoscale=False) print(description) # add the tilemap and the player sprite layer to a scrolling manager scroller = layer.ScrollingManager() walls = tiles.load('tmx_collision.tmx')['walls'] assert isinstance(walls, tiles.TmxObjectLayer) scroller.add(walls, z=0) # make the function to handle collision between actors and walls mapcollider = TmxObjectMapCollider() mapcollider.on_bump_handler = mapcollider.on_bump_bounce fn_collision_handler = mapcolliders.make_collision_handler( mapcollider, walls) # make the function to set visual focus at position fn_set_focus = scroller.set_focus # create a layer to put the player in actors_layer = layer.ScrollableLayer() ball = Ball((300, 300), (600, 600), fn_collision_handler, fn_set_focus, (255, 0, 255)) actors_layer.add(ball) scroller.add(actors_layer, z=1) # set the player start using the object with the 'player_start' property player_start = walls.find_cells(player_start=True)[0] ball.position = player_start.center # set focus so the player is in view scroller.set_focus(*ball.position) # extract the player_start, which is not a wall walls.objects.remove(player_start) # construct the scene with a background layer color and the scrolling layers platformer_scene = cocos.scene.Scene() platformer_scene.add(layer.ColorLayer(100, 120, 150, 255), z=0) platformer_scene.add(scroller, z=1) # track keyboard presses keyboard = key.KeyStateHandler() director.window.push_handlers(keyboard) # run the scene director.run(platformer_scene)
def main(): global keyboard, walls, scroller from cocos.director import director director.init(width=800, height=600, autoscale=False) print(description) # add the tilemap and the player sprite layer to a scrolling manager scroller = layer.ScrollingManager() walls = tiles.load('tmx_collision.tmx')['walls'] assert isinstance(walls, tiles.TmxObjectLayer) scroller.add(walls, z=0) # make the function to handle collision between actors and walls mapcollider = TmxObjectMapCollider() mapcollider.on_bump_handler = mapcollider.on_bump_bounce fn_collision_handler = mapcolliders.make_collision_handler(mapcollider, walls) # make the function to set visual focus at position fn_set_focus = scroller.set_focus # create a layer to put the player in actors_layer = layer.ScrollableLayer() ball = Ball((300, 300), (600, 600), fn_collision_handler, fn_set_focus, (255, 0, 255)) actors_layer.add(ball) scroller.add(actors_layer, z=1) # set the player start using the object with the 'player_start' property player_start = walls.find_cells(player_start=True)[0] ball.position = player_start.center # set focus so the player is in view scroller.set_focus(*ball.position) # extract the player_start, which is not a wall walls.objects.remove(player_start) # construct the scene with a background layer color and the scrolling layers platformer_scene = cocos.scene.Scene() platformer_scene.add(layer.ColorLayer(100, 120, 150, 255), z=0) platformer_scene.add(scroller, z=1) # track keyboard presses keyboard = key.KeyStateHandler() director.window.push_handlers(keyboard) # run the scene director.run(platformer_scene)
def createMPCH(collision_layers): # creates mapcolliders and collision handlers mps = [] # map colliders chs = [] # collision handlers for the user for index, layer in enumerate(collision_layers): mp = mapcolliders.TmxObjectMapCollider() mp.id = index mp.on_bump_handler = mp.on_bump_bounce mps.append(mp) ch = mapcolliders.make_collision_handler(mp, collision_layers[index]) ch.id = index chs.append(ch) return (mps, chs)
def main(): global walls, scroller, platformer_scene from cocos.director import director director.init(width=800, height=600, autoscale=False) print(description) # add the tilemap and the player sprite layer to a scrolling manager scroller = layer.ScrollingManager() walls = tiles.load('tmx_collision.tmx')['walls'] assert isinstance(walls, tiles.TmxObjectLayer) scroller.add(walls, z=0) # make the function to handle collision between actors and walls mapcollider = TmxObjectMapCollider() mapcollider.on_bump_handler = mapcollider.on_bump_bounce fn_collision_handler = mapcolliders.make_collision_handler(mapcollider, walls) # make the function to set visual focus at position fn_set_focus = scroller.set_focus scroller.add(Actors(fn_collision_handler), z=1) # set the player start using the object with the 'player_start' property player_start = walls.find_cells(player_start=True)[0] # extract the player_start, which is not a wall walls.objects.remove(player_start) # construct the scene with a background layer color and the scrolling layers platformer_scene = cocos.scene.Scene() platformer_scene.add(layer.ColorLayer(100, 120, 150, 255), z=0) platformer_scene.add(scroller, z=1) # set focus scroller.set_focus(300, 300) # handle zoom and pan commands director.window.push_handlers(on_key_press) # run the scene director.run(platformer_scene)
def loadLvl(level, gamemode): global scroller global player_layer global player global fullmap global gamemoderun global start global r global mapcollider global tilemap_decorations, tilemap_walls global bgLayer try: fullmap = tiles.load(level.datapath) tilemap_walls = fullmap["walls"] tilemap_decorations = fullmap["decorations"] scroller.add(bgLayer, z=-2) scroller.add(tilemap_decorations, z=-1) scroller.add(tilemap_walls, z=0) scroller.add(player_layer, z=1) start = tilemap_walls.find_cells(player_start=True)[0] r = player.get_rect() r.midbottom = start.midbottom player.position = r.center mapcollider = mapcolliders.RectMapCollider(velocity_on_bump="slide") player.collision_handler = mapcolliders.make_collision_handler( mapcollider, tilemap_walls) gamemoderun = player.do(gamemode.main()) except Exception as e: # TODO: if cfg.configuration["Debug"]["developer"] == "False": logger.addLog( "An error was caught rendering the level.\n" + str(e), logger.loglevel["error"]) else: raise e