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, 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 __init__(self): w,h = director.director.get_window_size() super( HUDLayer, self).__init__() # transparent layer self.add( layer.ColorLayer(32,32,32,32, width=192, height=h),z=-1 ) self.position = (w - 192, 0) self.counter = text.Label('organisms', font_size=12, color=colors.rgba(colors.base03), anchor_x='left', anchor_y='bottom') self.counter.position=(0, h-50) self.add( self.counter)
def __init__(self): super().__init__() events.settingsevents.push_handlers(self) self.infoBox = Sprite("infoBox.png") self.bgDimmer = layer.ColorLayer(0, 0, 0, 20) self.exitButton = smallButton("X", events.settingsevents.aboutPopupHide) self.bgDimmer.opacity = 100 self.infoBox.x = reswidth / 2 self.infoBox.y = resheight / 2 self.bgDimmer.width = reswidth self.bgDimmer.height = resheight self.exitButton.x = self.infoBox.x + ((self.infoBox.width / 2) * 0.91) self.exitButton.y = self.infoBox.y + ((self.infoBox.height / 2) * 0.85) self.active = False self.title = Label("title", anchor_x="center", anchor_y="center", font_size=39, color=(0, 0, 0, 255)) self.title.x = self.infoBox.x self.title.y = self.infoBox.y + ((self.infoBox.height / 2) * 0.8) self.desc = Label("desc", anchor_x="center", anchor_y="top", font_size=17, multiline=True, width=(self.infoBox.width * 0.87), height=(self.infoBox.height * 0.4), color=(0, 0, 0, 255), align="center") self.desc.x = self.infoBox.x self.desc.y = self.infoBox.y + ((self.infoBox.height / 2) * 0.6) self.add(self.infoBox, z=5) self.add(self.bgDimmer, z=4) self.add(self.exitButton, z=5) self.add(self.title, z=5) self.add(self.desc, z=5) self.bgDimmer.do(FadeOut(0.00001)) self.infoBox.do(FadeOut(0.00001)) self.exitButton.do(FadeOut(0.00001)) self.title.do(FadeOut(0.00001)) self.desc.do(FadeOut(0.00001))
def main(): global keyboard, walls, scroller from cocos.director import director director.init(width=800, height=600, autoscale=False) print(description) car_layer = layer.ScrollableLayer() car = cocos.sprite.Sprite('car.png') car_layer.add(car) car.position = (200, 100) car.max_forward_speed = 200 car.max_reverse_speed = -100 worker_action = car.do(DriveCar()) # add the map and the player sprite layer to a scrolling manager scroller = layer.ScrollingManager() walls = tiles.load('tmx_collision.tmx')['walls'] assert isinstance(walls, tiles.TmxObjectLayer) worker_action.walls = walls scroller.add(walls, z=0) scroller.add(car_layer, z=1) player_start = walls.find_cells(player_start=True)[0] # extract the player_start, which is not a wall walls.objects.remove(player_start) # give car access to the walls so it can change colors car.walls = 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, tilemap, scroller from cocos.director import director director.init(width=800, height=600, do_not_scale=True) 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 tilemap and the player sprite layer to a scrolling manager scroller = layer.ScrollingManager() tilemap = tiles.load('platformer-map.xml')['level0'] scroller.add(tilemap, z=0) scroller.add(player_layer, z=1) # set the player start using the player_start token from the tilemap start = tilemap.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 # 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(): """ Every program needs a main function. This is it. """ # Instantiate and Initialize main director maindirector = director.director maindirector.init(resizable=True) # Instantiate main scene mainscene = scene.Scene() # Instantiate main menu layer - main menu at start of game mainmenulayer = menu.MainMenu() # Instantiate gameview, a CocosNode containing layers for actual gameplay maingameview = gameview.GameView() # Add our layers in a multiplex (only 1 layer per multiplex can be visible at a time) # The default visible layer is the first in the multiplex (mainmenu) # This multiplex layer is indexed z = 1 mainscene.add(layer.base_layers.MultiplexLayer(mainmenulayer, maingameview), z=1) # Instantiate background layer - just a colored layer # Note: The * (splat operator) unpacks the tuple returned by rgba() backgroundlayer = layer.ColorLayer(*colors.rgba(colors.base3)) # Add background, always visible, indexed at z = 0 mainscene.add(backgroundlayer, z=0) # DEBUG - shows what key is pressed and mouse coords # scene.add( mousedisplay.MouseDisplay(), z = 2) # scene.add( keydisplay.KeyDisplay(), z = 3) # Tell director to run our scene (start game!) maindirector.run(mainscene)
def __init__(self, *children): super().__init__(*children) self.scrollManager = layer.ScrollingManager() self.scrollBar = ScrollBar(self.scrollManager) self.scrollLayer = ScrollLayer(reswidth / 2, resheight, reswidth, resheight, self.scrollBar) self.scrollLayer.x = 0 self.scrollLayer.y = 0 self.scrollBar.x = reswidth - (self.scrollBar.width / 2) self.scrollBar.y = (resheight - (resheight * 0.02)) - (self.scrollBar.img.height / 2) self.title = text.Label("Controls", font_name=resources.font[1], font_size=50, anchor_y="top", anchor_x="center") self.title.x = reswidth / 2 self.title.y = resheight * 0.85 self.add(self.title) blackLayer = layer.ColorLayer(0, 0, 0, 255) blackLayer.width = reswidth blackLayer.height = int(resheight * 0.35) blackLayer.x = 0 blackLayer.y = int(resheight - (blackLayer.height / 2)) self.add(blackLayer, z=-1) backButton = elements.mediumButton( "BACK", events.mainmenuevents.onSettingsButtonClick) backButton.x = reswidth * 0.065 backButton.y = resheight * 0.89 backButton.show(0.0001) self.add(backButton) controlElements = [] i = 1 for control, value in cfg.configuration["Controls"].items(): lbl = text.Label(str(control).capitalize(), font_size=25, anchor_x="left", anchor_y="center", color=(255, 255, 255, 255)) lbl.x = reswidth * 0.1 lbl.y = resheight * (0.6 - (0.15 * (i - 1))) txtBx = ControlTextBox(control) txtBx.x = reswidth * 0.9 txtBx.y = resheight * (0.6 - (0.15 * (i - 1))) self.scrollLayer.add(lbl) self.scrollLayer.add(txtBx) controlElements.append([lbl, txtBx]) i += 1 self.scrollManager.add(self.scrollLayer) self.scrollLayer.calculate() self.scrollManager.set_focus(reswidth / 2, resheight / 2) self.add(self.scrollManager, z=-2) self.add(self.scrollBar) self.add(messagePopup, z=1)
def main(): global keyboard, tilemap, scroller, player_direction, player_right, player_left, my_start_x, ground_list, start_point from cocos.director import director director.init(width=BG_WD, height=BG_HT, autoscale=False) print(description) # create a layer to put the player in player_layer = layer.ScrollableLayer(1) # 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('block_man.png') player.x = -2850 player.y = 120 dragon_keep_bg = cocos.sprite.Sprite('DragonKeepPNG.png') player_layer.add(dragon_keep_bg, z=1) player_layer.add(player, z=2) #test tmx object collision - no really good examples #points = [(3232, 2400), (4232, 2400), (4232, 2300), (3232, 2300)] #t = cocos.tiles.TmxObject('polygon', 'dragon', 3232, 2400, 1000, 10, None, None, None, 1, points) #lay = cocos.tiles.TmxObjectLayer('layer', (255, 0, 0, 255), [t], 1, 1, (3232, 2400)) #i = 0 #pdb.set_trace() #while (i < 10): # player.x += 10 # player.y -= 10 # rect = player.get_rect() # if (lay.collide(rect, 'layer')): # print("True!") # i+=1 #tbd player.do(PlatformerController()) scroller = layer.ScrollingManager() scroller.add(player_layer, z=2) # construct the scene with a background layer color and the scrolling layers platformer_scene = cocos.scene.Scene() platformer_scene.add(layer.ColorLayer(0, 0, 0, 255), z=0) platformer_scene.add(scroller, z=1) # track keyboard presses keyboard = key.KeyStateHandler() director.window.push_handlers(keyboard) my_start_x = 0 ground_list = [] start_point = [] # allow display info about cells / tiles def on_key_press(key, modifier): #pdb.set_trace() global player_direction if key == pyglet.window.key.LEFT: player.x -= 100 if key == pyglet.window.key.RIGHT: player.x += 100 if key == pyglet.window.key.UP: player.y += 100 if key == pyglet.window.key.DOWN: player.y -= 100 if key == pyglet.window.key.E: print(ground_list) scroller.set_focus(player.position[0], player.position[1]) print(player.position) print("x{}, y{}".format(player.x, player.y)) def on_mouse_release(x, y, buttons, modifiers): global my_start_x, start_point, ground_list t = scroller.screen_to_world(x, y) print("mouse x{}, y{}".format(x, y)) print("mouse x{}, y{}".format(t[0], t[1])) if (my_start_x == 0): print("start_x = 0") my_start_x = 1 start_point = t else: print("start_x = 1") my_start_x = 0 ground_list.append([start_point, t]) director.window.push_handlers(on_key_press) director.window.push_handlers(on_mouse_release) scroller.set_focus(player.position[0], player.position[1]) # run the scene director.run(platformer_scene)
def loadSceenShowing(self): self.levelData = tiles.load(self.level.datapath) self.tilemap_decorations = self.levelData["decorations"] self.tilemap_walls = self.levelData["walls"] self.layers = [self.tilemap_decorations, self.tilemap_walls] self.layers[0].visible = False self.layers[1].visible = True setattr(self.layers[0], "name", "Decorations Layer") setattr(self.layers[1], "name", "Walls Layer") if isinstance(self.level.background, str): self.bgLayer = layer.ScrollableLayer() self.bgLayer.parallax = 0.5 bgImage = cocos.sprite.Sprite(self.level.background) # TODO: Add code to scale image to viewport, then tile it self.bgLayer.add(bgImage) self.scrollerViewport = rect.Rect(0, int(resheight * 0.12), int(reswidth), int(resheight * 0.76)) self.scroller = self.LevelEditorScrollManager(self.scrollerViewport, True) self.scroller.autoscale = False #self.scroller.on_cocos_resize = on_cocos_resize self.scroller.scale = 0.8 self.scroller.viewport = cocos.rect.Rect(0, int(resheight * 0.12), int(reswidth), int(resheight * 0.76)) #self.scroller.x = 0 #self.scroller.y = 0 self.scroller.add(self.tilemap_decorations, z=-1) self.scroller.add(self.tilemap_walls, z=0) self.gridLayer = LevelGridLayer(walls=self.tilemap_walls, decorations=self.tilemap_decorations, scroller=self.scroller, level=self.level) self.scroller.add(self.gridLayer, z=1) self.add(self.bgLayer, z=-5) self.add(self.scroller) self.headerLayer = layer.ColorLayer(0, 0, 0, 175, width=int(reswidth), height=int(resheight * 0.12)) self.headerLayer.x = 0 self.headerLayer.y = resheight - self.headerLayer.height self.title = text.Label("Level Editor", font_name=resources.font[1], font_size=50, anchor_y="center", anchor_x="center") self.title.x = int(self.headerLayer.width / 2) self.title.y = int(self.headerLayer.height / 2) self.backButton = elements.mediumButton( "BACK", events.leveleditorevents.leveleditorGoBack) self.backButton.x = int(self.headerLayer.width * 0.065) self.backButton.y = int(self.headerLayer.height / 2) self.saveButton = elements.mediumButton( "SAVE", events.leveleditorevents.levelSave) self.saveButton.x = int(self.headerLayer.width * 0.947) self.saveButton.y = int(self.headerLayer.height / 2) self.discardButton = elements.mediumButton( "DISCARD", events.leveleditorevents.levelDiscard) self.discardButton.lbl.element.font_size = 13 self.discardButton.x = int(self.headerLayer.width * 0.857) self.discardButton.y = int(self.headerLayer.height / 2) #self.editButton self.add(self.headerLayer, z=2) self.headerLayer.add(self.title) self.headerLayer.add(self.saveButton) self.headerLayer.add(self.backButton) self.headerLayer.add(self.discardButton) self.backButton.px = self.saveButton.px = self.discardButton.px = self.headerLayer.x self.backButton.py = self.saveButton.py = self.discardButton.py = self.headerLayer.y self.backButton.show(0.1) self.footerLayer = layer.ColorLayer(0, 0, 0, 125, width=int(reswidth), height=int(resheight * 0.12)) self.footerLayer.x = 0 self.footerLayer.y = 0 self.add(self.footerLayer, z=2) self.upButton = elements.smallButton("\u2b9d", self.moveUp) self.rightButton = elements.smallButton("\u2b9e", self.moveRight) self.leftButton = elements.smallButton("\u2b9c", self.moveLeft) self.downButton = elements.smallButton("\u2b9f", self.moveDown) self.upButton.x = self.footerLayer.width * 0.92 self.upButton.y = self.footerLayer.height * 0.75 self.rightButton.x = self.footerLayer.width * 0.955 self.rightButton.y = self.footerLayer.height * 0.5 self.leftButton.x = self.footerLayer.width * 0.885 self.leftButton.y = self.footerLayer.height * 0.5 self.downButton.x = self.footerLayer.width * 0.92 self.downButton.y = self.footerLayer.height * 0.25 self.itemRows = ItemPackRows() self.activeLayerSelection = ActiveLayerSelection() self.activeLayerSelection.changeLabel.element.text = self.layers[ 1].name self.footerLayer.add(self.upButton) self.footerLayer.add(self.rightButton) self.footerLayer.add(self.leftButton) self.footerLayer.add(self.downButton) self.footerLayer.add(self.itemRows) self.footerLayer.add(self.activeLayerSelection) self.upButton.show(0.1) self.rightButton.show(0.1) self.leftButton.show(0.1) self.downButton.show(0.1) self.scroller.set_focus(int(self.tilemap_walls.view_w / 2), int(self.tilemap_walls.view_h / 2)) self.scrollerFocusLimits = { "up": 0, "down": int(self.tilemap_walls.view_h / 2), "left": int(self.tilemap_walls.view_w / 2), "right": 0 } self.intro.do(cocos.actions.Delay(3) + cocos.actions.FadeOut(1)) self.intro.title.do(cocos.actions.Delay(3) + cocos.actions.FadeOut(1)) self.intro.desc.do(cocos.actions.Delay(3) + cocos.actions.FadeOut(1))