def __init__(self, title, lines): super(BulletListLayer, self).__init__() x, y = director.get_window_size() self.title = text.Label( title, (x/2, y-50), font_name = 'Gill Sans', font_size = 64, anchor_x='center', anchor_y='center' ) self.add( self.title ) start_y = (y/12)*8 font_size = 52 / (len(lines)/2.2+1) font_size = min(font_size, 52) line_font = font.load('Gill Sans', font_size) tot_height = 0 max_width = 0 rendered_lines = [] step = 300/ max(len(lines),1) i = 0 for line in lines: line_text = text.Label( line, (x/2, y-150-step*i), font_name = 'Gill Sans', font_size = font_size, anchor_x='center', anchor_y='center' ) i += 1 self.add( line_text )
def on_enter(self): super().on_enter() score = highscore.get_score() if self.table: self._remove_old() self.table = [] self.font_top = {} self.font_top['font_size'] = self.fontsize self.font_top['bold'] = True self.font_top['font_name'] = _font_ self.font_label = {} self.font_label['font_size'] = self.fontsize self.font_label['bold'] = False self.font_label['font_name'] = _font_ Head_Pos = text.Label('', anchor_x='right', anchor_y='top', **self.font_top) Head_Name = text.Label('Name', anchor_x='left', anchor_y='top', **self.font_top) Head_Wave = text.Label('Wave', anchor_x='right', anchor_y='top', **self.font_top) self.table.append((Head_Pos, Head_Name, Head_Wave)) self.table.append((text.Label(''), text.Label(''), text.Label(''))) for i, entry in enumerate(score): pos = text.Label('%i. ' % (i + 1), anchor_x='right', anchor_y='top', **self.font_label) try: name = text.Label(entry[1].strip(), anchor_x='left', anchor_y='top', **self.font_label) except IndexError: print("highscore file broken") name = text.Label("Error", anchor_x='left', anchor_y='top', **self.font_label) wave = text.Label(entry[0], anchor_x='right', anchor_y='top', **self.font_label) self.table.append((pos, name, wave)) self._process_table()
def __init__(self, title, subtitle): super(TitleSubTitleLayer, self).__init__() x, y = director.get_window_size() self.title = text.Label( title, (x/2, y/2+50), font_name = 'Gill Sans', font_size = 64, anchor_x='center', anchor_y='center' ) self.add( self.title ) self.subtitle = text.Label( subtitle, (x/2, y/2-30), font_name = 'Gill Sans', font_size = 44, anchor_x='center', anchor_y='center' ) self.add( self.subtitle )
def __init__(self, name, desc, r, g, b, a, width=None, height=None): super().__init__(r, g, b, a, width=width, height=height) self.title = text.Label(name, font_size=40, anchor_x="center", anchor_y="center") self.desc = text.Label(desc, font_size=30, anchor_x="center", anchor_y="center") self.title.x = self.width / 2 self.title.y = self.height * 0.55 self.desc.x = self.width / 2 self.desc.y = self.height * 0.4 self.add(self.title, z=3) self.add(self.desc, z=3)
def __init__(self,score): super(GamePauseLayer, self).__init__(10,10,10,100) score_lable = text.Label(u'得分:%s'%score,font_size=20,font_name='Microsoft Yahei',color=(255,255,255,255),anchor_x="center",anchor_y="center") score_lable.position = config.WINDOS_WIDTH // 2, config.WINDOS_HEIGHT - 200 self.add(score_lable) pause_menu = GamePauseMeum() self.add(pause_menu)
def __init__(self): super(KeyDisplay, self).__init__() self.text = text.Label('', x=100, y=280) # To keep track of which keys are pressed: self.keys_pressed = set() self.update_text() self.add(self.text)
def __init__(self): super(MouseDisplay, self).__init__() self.posx = 100 self.posy = 240 self.text = text.Label('No mouse event yet', font_size=18, x=self.posx, y=self.posy) self.add(self.text)
def show_hp_string(self): self.hp_string = text.Label( '', font_name=FONT['body'], font_size=16, anchor_x='center', anchor_y='center', position=(80, 40), color=(225, 225, 225, 225), ) self.hp_string.element.text = "Health: 100%"
def show_bullets_string(self): self.bullets_string = text.Label( '', font_name=FONT['body'], font_size=16, anchor_x='center', anchor_y='center', position=(WIDTH - 16 * 5, 40), color=(225, 225, 225, 225), ) self.bullets_string.element.text = "Bullets: %03d" % len( self.spaceship.bullets)
def __init__(self): super(GameOverMenuLayer, self).__init__() self.label = text.Label("得分:" + str(Hero.score), font_name='Microsoft YaHei', font_size=20, anchor_x='center', anchor_y='center', color=(120, 124, 125, 255)) self.label.position = config.WIN_WIDTH // 2, config.WIN_HEIGHT // 2 + 140 self.add(self.label) self.game_menu = GameOverMenu() self.add(self.game_menu)
def __init__(self): super().__init__() changeArrow = elements.smallButton( "\u21f5", events.leveleditorevents.activeLayerChange) changeArrow.x = reswidth * 0.5 changeArrow.y = resheight * 0.06 titleLabel = text.Label("Active Layer:", anchor_x="center", anchor_y="center", font_size=14) titleLabel.x = reswidth * 0.4 titleLabel.y = resheight * 0.09 self.changeLabel = text.Label("Change Label", anchor_x="center", anchor_y="center", font_size=14) self.changeLabel.x = reswidth * 0.4 self.changeLabel.y = resheight * 0.03 self.add(changeArrow) self.add(titleLabel) self.add(self.changeLabel) changeArrow.show(0.01)
def __init__(self): super(HelloWorld, self).__init__() label = text.Label( 'Hello World', font_name='Microsoft YaHei UI', font_size=32, anchor_x='center', anchor_y='center', ) label.position = 320, 240 self.add(label)
def __init__(self): super(HelloWorld, self).__init__() # 创建标签 self.label = text.Label('Hello, World!', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center') # 获得窗口的宽度和高度 width, height = director.get_window_size() # 设置标签的位置 self.label.position = width // 2, height // 2 # 添加标签到HelloWorld层 self.add(self.label)
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, itempack, label, item_blocks=[]): super().__init__() self.packLbl = text.Label(label, font_size=14, anchor_x="center", anchor_y="center") self.packLbl.x = reswidth * 0.15 self.packLbl.y = resheight * 0.095 self.blocks = [] self._visible = False if item_blocks[0] == "empty": itemBlock = sprite.Sprite("emptyBlock.png", scale=1.2) itemBlock.x = reswidth * 0.05 itemBlock.y = resheight * 0.045 itemBlock.opacity = 0 itemSelectionBlock = self.ItemHoverBox( "leveleditorItemHovered.png", None, "empty", reswidth * 0.05, resheight * 0.045, scale=1.2) self.add(itemBlock, z=1) self.add(itemSelectionBlock, z=2) self.blocks.append([itemBlock, itemSelectionBlock]) else: for itemId in range(len(item_blocks)): itemBlock = sprite.Sprite(items.itempacks[ itempack.idx].item_data[item_blocks[itemId][:-4]].image, scale=1.2) itemBlock.x = (reswidth * 0.05) + (reswidth * (0.04 * itemId)) itemBlock.y = resheight * 0.045 itemBlock.opacity = 0 itemSelectionBlock = self.ItemHoverBox( "leveleditorItemHovered.png", itempack, item_blocks[itemId], (reswidth * 0.05) + (reswidth * (0.04 * itemId)), resheight * 0.045, scale=1.2) self.add(itemBlock, z=1) self.add(itemSelectionBlock, z=2) self.blocks.append([itemBlock, itemSelectionBlock]) self.add(self.packLbl)
def __init__(self, score): super(GameOverLayer, self).__init__() self.bg = sprite.Sprite('img/bg.png') self.bg.position = config.WINDOS_WIDTH // 2, config.WINDOS_HEIGHT // 2 self.add(self.bg, z=0) feiji = sprite.Sprite('img/hero/hero1.png') feiji.position = config.WINDOS_WIDTH // 2, config.WINDOS_HEIGHT - 200 feiji.scale = 0.5 self.add(feiji) score_label = text.Label(u'得分:%d' % score, font_size=20, font_name="Microsoft Yahei", color=(10, 10, 10, 255), anchor_x="center", anchor_y="center") score_label.position = config.WINDOS_WIDTH // 2, config.WINDOS_HEIGHT - 300 self.add(score_label)
def __init__(self): super(HelloWorld, self).__init__() label = text.Label( 'Hello World', font_name='Microsoft YaHei UI', font_size=32, # [NOTE] anchor means the position is where of this label. anchor_x='center', anchor_y='center', ) # [NOTE] Position and font_size seem to be relative. # It will change when the screen is resizing. label.position = 320, 240 # [LEARN] Label is a subclass of CocosNode, so it can be added as a child. # All CocosNode objects know how to render itself, perform actions and transformations. self.add(label)
def __init__(self, width=None, height=None): super().__init__(100, 100, 100, 100, width=width, height=height) self.width = reswidth self.height = int(resheight * 0.07) self.x = 0 self.y = resheight self.showAction = actions.AccelDeccel( actions.MoveTo((self.x, resheight - self.height), 0.5)) self.hideAction = actions.AccelDeccel( actions.MoveTo((self.x, resheight), 0.5)) self.lbl = text.Label("", anchor_x="center", anchor_y="center", font_size=18) self.lbl.x = self.width / 2 self.lbl.y = self.height / 2 self.active = False self.add(self.lbl)
def on_enter(self): super().on_enter() w, h = director.get_window_size() about_text = text.Label('PyFense ist ein Tower Defense Spiel ' + 'welches im Rahmen des Python ' + 'Projektpraktikums an der TU ' + 'München von fünf Studenten programmiert ' + 'wurde.\nMitglieder: Daniel, Jakob, ' + 'Matthias, Nimar, Robin' + '\nMusic from:\n' + 'www.freesound.org/people/djgriffin/', font_name=_font_, font_size=20, anchor_x='center', anchor_y='center') about_text.element.width = w * 0.3 about_text.element.multiline = True about_text.element.wrap_lines = True about_text.position = w / 2., h / 2. self.add(about_text)
def __init__(self): # Blueish color super(HelloActions, self).__init__( r=64, g=64, b=224, a=255, ) label = text.Label( 'Hello World', font_name='Microsoft YaHei UI', font_size=32, anchor_x='center', anchor_y='center', ) label.position = 320, 240 self.add(label) # Add a cocos Sprite. sprite_ = sprite.Sprite('HSCard.png') sprite_.position = 320, 240 # [NOTE] The sprite will 3 times bigger. sprite_.scale = 0.58 # [NOTE] z is the precedence of the sprite. This sprite will on the top of the label. self.add(sprite_, z=1) # [LEARN] We create a ScaleBy action. It will scale 3 times the object in 2 seconds: scale = actions.ScaleBy(3, duration=2) # [LEARN] '+' is sequence action here. # 'Reverse' reverse the action. label.do(actions.Repeat(scale + actions.Reverse(scale))) sprite_.do(actions.Repeat(actions.Reverse(scale) + scale))
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 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))
def __init__(self, card: Card, position=(0, 0), scale=1, hidden=False): card_image_name = '{}-{}.png'.format(Klass.Idx2Str[card.klass], card.type) super().__init__(image=card_image_name, position=position, scale=scale) self.card = card aabb = self.get_AABB() self.hidden = hidden self.front = [] self.back = [] back_sprite = Sprite( 'Card_back-Classic.png', position=_pos(0., 0., base=self.point_to_local(aabb.center)), scale=1.0, ) self.add(back_sprite) self.back.append(back_sprite) name_label = text.Label( text=card.name, position=(0, 0), font_name='SimHei', font_size=60 * self.scale, anchor_x='center', anchor_y='center', bold=True, ) desc_label = text.HTMLLabel( text=_render_desc(card.description, color='black'), position=_pos(0.0, 0.55, base=self.point_to_local(aabb.bottomleft)), anchor_x='center', anchor_y='center', width=self.width * 1.5, multiline=True, ) # See notes in `_render_desc`. desc_label.element.font_name = 'SimHei' desc_label.element.font_size = 50 * self.scale self.add(name_label) self.add(desc_label) self.front += [name_label, desc_label] # [NOTE] Only `get_AABB` method get the correct position of the child sprite. if card.type == Type.Permanent: mana_sprite = Sprite( 'Mana.png', position=_pos(0.85, 0.76, base=self.point_to_local(aabb.topleft)), scale=0.9, ) else: mana_sprite = Sprite( 'Mana-{}.png'.format(card.cost), position=_pos(0.85, 0.76, base=self.point_to_local(aabb.topleft)), scale=0.9, ) self.add(mana_sprite) self.front.append(mana_sprite) if card.rarity not in (Rarity.Basic, Rarity.Derivative): pass if card.type == Type.Minion: assert isinstance(card, Minion) name_label.position = _pos(0.0, 0.1, base=self.point_to_local( aabb.midbottom)) attack_sprite = Sprite( 'Atk-{}.png'.format(card.attack), position=_pos(0.85, 0.86, base=self.point_to_local(aabb.bottomleft)), scale=1.08, ) health_sprite = Sprite( 'Health-{}.png'.format(card.health), position=_pos(0.85, 0.86, base=self.point_to_local(aabb.bottomright)), scale=1.08, ) self.add(attack_sprite) self.add(health_sprite) self.front += [attack_sprite, health_sprite] elif card.type == Type.Spell: assert isinstance(card, Spell) mana_sprite.position = _pos(0.85, 0.86, base=self.point_to_local(aabb.topleft)) elif card.type == Type.Weapon: assert isinstance(card, Weapon) mana_sprite.position = _pos(0.85, 0.78, base=self.point_to_local(aabb.topleft)) desc_label.element.color = Colors['white'] attack_sprite = Sprite( 'WeaponAtk-{}.png'.format(card.attack), position=_pos(0.85, 0.86, base=self.point_to_local(aabb.bottomleft)), scale=1.08, ) health_sprite = Sprite( 'WeaponHealth-{}.png'.format(card.health), position=_pos(0.85, 0.86, base=self.point_to_local(aabb.bottomright)), scale=1.08, ) self.add(attack_sprite) self.add(health_sprite) self.front += [attack_sprite, health_sprite] elif card.type == Type.HeroCard: assert isinstance(card, HeroCard) armor_sprite = Sprite( 'HeroArmor-{}.png'.format(card.armor), position=_pos(0.85, 0.86, base=self.point_to_local(aabb.bottomright)), scale=1.08, ) self.add(armor_sprite) self.front.append(armor_sprite) # z-order and move actions for animation to restore original state. self._orig_z_order = None self._move_actions = None self._update_hidden()
def __init__(self, game: Game, **kwargs): super(HSGameBoard, self).__init__() # Some positions. right_b = 0.88 right_c = (1 + right_b) / 2 hero_b = 0.66 # Lines. self.add( draw.Line(_pos(right_b, .0), _pos(right_b, 1.), Colors['white'], 2)) self.add(draw.Line(_pos(.0, .5), _pos(1., .5), Colors['white'], 2)) self.add( draw.Line(_pos(.0, .23), _pos(hero_b, .23), Colors['white'], 2)) self.add( draw.Line(_pos(.0, .77), _pos(hero_b, .77), Colors['white'], 2)) self.add( draw.Line(_pos(hero_b, .0), _pos(hero_b, 1.), Colors['white'], 2)) self.cards = [] # Variables for selection. self.active_card = None if game is None: warning('The game is not running, just show the board') return # Players. P0 is current player (show in bottom), P1 is current player (show in top, hide something) player_ids = game.current_player, 1 - game.current_player players = [game.players[player_id] for player_id in player_ids] # Decks. deck_sizes = [len(p.get_zone(Zone.Deck)) for p in players] for ds, y in zip(deck_sizes, [0.15, 0.85]): self.add( text.Label( '牌库:{}'.format(ds), _pos(right_c, y), font_name='SimHei', font_size=16, anchor_x='center', anchor_y='center', bold=True, )) # Manas. manas = [[p.displayed_mana(), p.max_mana, p.overload, p.overload_next] for p in players] for (mana, max_mana, overload, overload_next), y in zip(manas, [0.3, 0.7]): self.add( text.Label( '{}/{}{}{}'.format( mana, max_mana, '' if overload == 0 else '\n(Overload {})'.format(overload), '' if overload_next == 0 else '\n(Overload next {})'.format(overload_next), ), _pos(right_c, y), font_name='SimHei', font_size=16, anchor_x='center', anchor_y='center', color=Colors['blue'], bold=True, multiline=True, width=(1 - right_b) * Width, align='center', )) # Other components of right board. for pi, y in zip(player_ids, [0.42, 0.58]): self.add( text.Label( 'Player {}'.format(pi), _pos(right_c, y), font_name='SimHei', font_size=16, anchor_x='center', anchor_y='center', bold=True, )) # Hands. hands = [p.get_zone(Zone.Hand) for p in players] for pi, (hand, y) in enumerate(zip(hands, [0.115, 0.885])): for i, card in enumerate(hand): # [NOTE]: position need fix here. hand_card = CardSprite(card, _pos(hero_b / (len(hand) + 1) * (i + 1), y), scale=0.35, hidden=(pi == 1)) self.add(hand_card) self.cards.append(hand_card)
def on_enter(self): super().on_enter() w, h = director.get_window_size() nr_towers = len(resources.tower) help_text = text.Label('Press Q to quit the running level or Esc ' 'to enter the Pause Menu', font_name=_font_, font_size=20, anchor_x='center', anchor_y='center') help_text.element.width = w * 0.3 help_text.element.multiline = True help_text.element.wrap_lines = True help_text.position = w / 2., h / 2. + 300 self.add(help_text) # tower information self.damage_pic = resources.picto_damage self.rate_pic = resources.picto_rate pic_width = resources.tower[1][1]["image"].width self.menuMin_x = (w / 2. - pic_width * (4 / 3) - 55) self.menuMin_y = 700 towername_font = { 'bold': True, 'anchor_x': "right", 'anchor_y': 'center', 'font_size': 18, 'color': (193, 249, 255, 255) } caption_font = { 'bold': True, 'anchor_x': "left", 'anchor_y': 'center', 'font_size': 15, } towername_label = [] towername_label.append(text.Label("Rapidfire Tower", **towername_font)) towername_label.append(text.Label("Range Tower", **towername_font)) towername_label.append(text.Label("Plasma Tower", **towername_font)) towername_label.append(text.Label("Poison Tower", **towername_font)) towername_label.append(text.Label("Slow Tower", **towername_font)) for j, m in enumerate(towername_label): m.position = (self.menuMin_x - 80, self.menuMin_y - j * (pic_width + 15)) try: for j in range(nr_towers): self.add(towername_label[j]) except IndexError: print("please add towername_label for all towers. " + "otherwise no labels will be printed") price_label = text.Label("$ Price", color=(255, 0, 0, 255), **caption_font) dam_pic = cocos.sprite.Sprite(self.damage_pic) dam_label = text.Label("Damage per hit", color=(255, 70, 0, 255), **caption_font) rate_pic = cocos.sprite.Sprite(self.rate_pic) rate_label = text.Label("Firerate", color=(0, 124, 244, 255), **caption_font) price_label.position = (self.menuMin_x - 30, self.menuMin_y - (nr_towers * (pic_width + 15))) dam_pic.position = (self.menuMin_x + 103, self.menuMin_y - (nr_towers * (pic_width + 15))) dam_label.position = (self.menuMin_x + 135, self.menuMin_y - (nr_towers * (pic_width + 15))) rate_pic.position = (self.menuMin_x + 370, self.menuMin_y - (nr_towers * (pic_width + 15))) rate_label.position = (self.menuMin_x + 402, self.menuMin_y - (nr_towers * (pic_width + 15))) self.add(price_label) self.add(dam_pic) self.add(dam_label) self.add(rate_pic) self.add(rate_label) for l in range(1, 4): # loop over upgrade levels self.towerDamagePic = [] self.towerFireratePic = [] self.towerThumbnails = [] # loop over all tower thumbnails try: for i in range(nr_towers): self.towerThumbnails.append(cocos.sprite.Sprite( resources.tower[i][l]["image"])) except KeyError: print("check your tower naming, first tower should start " + "with 0 and no number should be left out.") nr_towers = 0 text_font = { 'bold': True, 'anchor_x': "left", 'anchor_y': 'center', 'font_size': 15, 'color': (255, 70, 0, 255) } damage_label = [] for i in range(nr_towers): damage_label.append(text.Label(" ", **text_font)) self.towerDamageTexts = [n for n in damage_label] text_font['color'] = (0, 124, 244, 255) firerate_label = [] for i in range(nr_towers): firerate_label.append(text.Label(" ", **text_font)) self.towerFirerateTexts = [n for n in firerate_label] text_font['color'] = (255, 0, 0, 255) cost_label = [] for i in range(nr_towers): cost_label.append(text.Label(" ", **text_font)) self.towerCostTexts = [n for n in cost_label] for picture in range(nr_towers): self.towerThumbnails[picture].position = ( self.menuMin_x + (l - 1) * (self.towerThumbnails[picture].width + 100), -picture * (self.towerThumbnails[picture].width + 15) + self.menuMin_y) self.towerDamagePic.append( cocos.sprite.Sprite(self.damage_pic)) self.towerDamagePic[picture].position = ( self.menuMin_x + (l - 1) * (self.towerThumbnails[picture].width + 100) + self.towerThumbnails[picture].width / 2. + 15, -picture * (self.towerThumbnails[picture].width + 15) + self.menuMin_y) self.towerDamageTexts[picture].element.text = ( str(resources.tower[picture][l]["damage"])) self.towerDamageTexts[picture].position = ( self.menuMin_x + (l - 1) * (self.towerThumbnails[picture].width + 100) + self.towerThumbnails[picture].width / 2. + 35, -picture * (self.towerThumbnails[picture].width + 15) + self.menuMin_y) self.towerFireratePic.append( cocos.sprite.Sprite(self.rate_pic)) self.towerFireratePic[picture].position = ( self.menuMin_x + (l - 1) * (self.towerThumbnails[picture].width + 100) + self.towerThumbnails[picture].width / 2. + 15, -picture * (self.towerThumbnails[picture].width + 15) + self.menuMin_y - 25) self.towerFirerateTexts[picture].element.text = ( str(resources.tower[picture][l]["firerate"])) self.towerFirerateTexts[picture].position = ( self.menuMin_x + (l - 1) * (self.towerThumbnails[picture].width + 100) + self.towerThumbnails[picture].width / 2. + 35, -picture * (self.towerThumbnails[picture].width + 15) + self.menuMin_y - 25) self.towerCostTexts[picture].element.text = ( '$ ' + str(resources.tower[picture][l]["cost"])) self.towerCostTexts[picture].position = ( self.menuMin_x + (l - 1) * (self.towerThumbnails[picture].width + 100) + self.towerThumbnails[picture].width / 2. + 15, -picture * (self.towerThumbnails[picture].width + 15) + self.menuMin_y + 25) self.add(self.towerThumbnails[picture]) self.add(self.towerDamageTexts[picture]) self.add(self.towerFirerateTexts[picture]) self.add(self.towerDamagePic[picture]) self.add(self.towerFireratePic[picture]) self.add(self.towerCostTexts[picture])
def hs_style_label(text_='', position=(0, 0), **kwargs): kw_with_default = DefaultLabelStyle.copy() kw_with_default.update(kwargs) return text.Label(text_, position, **kw_with_default)