def createresult(self, _text, _type): """ Create black board with specified text. :param _text: text to display :param _type: GameState in which board should be displayed. :return: nothing """ result = Entity() result.addartifact(SpriteArtifact( TextSprite(_text, Modifiers.CENTER_H | Modifiers.CENTER_V), 0, 0, _type)) self.systems[DrawSystem.NAME].register(result) self.elements.append(result)
def createhighscores(self): """ Create labels for highscore entering, and placeholder for entering player name. :return: nothing """ new_highscore = Entity() new_highscore.addartifact(SpriteArtifact( TextSprite("New highscore!", Modifiers.CENTER_H), 100, 100, GameState.NEW_HIGHSCORE)) self.systems[DrawSystem.NAME].register(new_highscore) self.elements.append(new_highscore) enter_your_name = Entity() enter_your_name.addartifact(SpriteArtifact( TextSprite("Enter your name:", Modifiers.CENTER_H), 100, 200, GameState.NEW_HIGHSCORE)) self.systems[DrawSystem.NAME].register(enter_your_name) self.elements.append(enter_your_name) self.player_name = Entity() self.player_name.addartifact(SpriteArtifact( TextSprite('_'*30, Modifiers.CENTER_H), 100, 300, GameState.NEW_HIGHSCORE)) self.systems[DrawSystem.NAME].register(self.player_name) self.elements.append(self.player_name)
def createhalloffame(self): """ Create labels and placeholders for loading highscore values. :return: nothing """ potential_size_of_one_line = (self.screen_height - 2*self.margin) \ / (HighscoresManager.topscorescount) + 1 hall_of_fame = Entity() hall_of_fame.addartifact(SpriteArtifact( TextSprite("HALL OF FAME!", Modifiers.CENTER_H), 0 , self.margin, GameState.SHOW_HIGHSCORES)) self.systems[DrawSystem.NAME].register(hall_of_fame) self.elements.append(hall_of_fame) self.highscorevalues.clear() self.highscorenames.clear() for i in range(HighscoresManager.topscorescount): highscore_player = Entity() highscore_player.addartifact(SpriteArtifact( TextSprite("x"), self.margin, self.margin + (i+1) * potential_size_of_one_line, GameState.SHOW_HIGHSCORES)) self.systems[DrawSystem.NAME].register(highscore_player) self.elements.append(highscore_player) self.highscorenames.append(highscore_player) for i in range(HighscoresManager.topscorescount): highscore_value = Entity() highscore_value.addartifact(SpriteArtifact( TextSprite("1"), 900, self.margin + (i+1) * potential_size_of_one_line, GameState.SHOW_HIGHSCORES)) self.systems[DrawSystem.NAME].register(highscore_value) self.elements.append(highscore_value) self.highscorevalues.append(highscore_value)
def test_menu_up(self): system = MenuSystem() item = Entity() item.addartifact(SpriteArtifact(TextSprite("Sdasd"), 0, 0, GameState.MENU)) item.addartifact(MenuArtifact()) system.register(item) system.register(item) system.register(item) system.current_game_state = GameState.MENU self.assertEqual(system.current_index, 0) system.input(pygame.event.Event(pygame.KEYDOWN, key=pygame.K_UP)) self.assertEqual(system.current_index, 2) system.input(pygame.event.Event(pygame.KEYDOWN, key=pygame.K_UP)) self.assertEqual(system.current_index, 1) system.remove(item) system.remove(item) system.remove(item)
def createmenuelement(self, _elementx, _elementy, _text, _type, _parent): """ Create single menu element. :param _elementx: X position of text :param _elementy: Y position of text :param _text: Text of element :param _type: Type of menu event :param _parent: Parent menu element. :return: menu entity """ menu = Entity() menu.addartifact(SpriteArtifact( TextSprite(_text, Modifiers.CENTER_H), _elementx, _elementy, GameState.MENU)) menu.addartifact(MenuArtifact(_type)) self.systems[DrawSystem.NAME].register(menu) self.systems[MenuSystem.NAME].register(menu, _parent) self.elements.append(menu) return menu
def updatehud(self, _caps, _lifes, _points): """ Update HUD with specified number of caps, lifes, and points. :param _caps: Collected caps :param _lifes: Lifes left :param _points: Collected points :return: """ self.text_caps = TextSprite(_caps) self.image_lifes.scale(self.text_caps.size()[1], self.text_caps.size()[1]) self.text_lifes = TextSprite(_lifes) self.image_points.scale(self.text_caps.size()[1], self.text_caps.size()[1]) self.text_points = TextSprite(_points)
def __init__(self, _modifiers=Modifiers.NONE): """ Constructor for HUD. :param tilesize: desired width and height of tile in pixels. """ super(HUDSprite, self).__init__() self.modifiers = _modifiers self.seperator_width = 10 self.text_caps = TextSprite("") self.image_lifes = SimpleImageSprite("res/img/life.png") self.image_lifes.scale(self.text_caps.size()[1], self.text_caps.size()[1]) self.text_lifes = TextSprite("") self.image_points = SimpleImageSprite("res/img/points.png") self.image_points.scale(self.text_caps.size()[1], self.text_caps.size()[1]) self.text_points = TextSprite("")
class HUDSprite(MySprite): """Sprite of on-screen display of points, caps and life.""" def __init__(self, _modifiers=Modifiers.NONE): """ Constructor for HUD. :param tilesize: desired width and height of tile in pixels. """ super(HUDSprite, self).__init__() self.modifiers = _modifiers self.seperator_width = 10 self.text_caps = TextSprite("") self.image_lifes = SimpleImageSprite("res/img/life.png") self.image_lifes.scale(self.text_caps.size()[1], self.text_caps.size()[1]) self.text_lifes = TextSprite("") self.image_points = SimpleImageSprite("res/img/points.png") self.image_points.scale(self.text_caps.size()[1], self.text_caps.size()[1]) self.text_points = TextSprite("") def draw(self, _screen, _positionx, _positiony): """ Draw HUD (Points, caps status, lifes) - with images. :param _screen: screen surface :param _positionx: X position to draw item :param _positiony: Y position to draw item :return: nothing """ text_caps_width, _ = self.text_caps.size() image_lifes_width, _ = self.image_lifes.size() text_lifes_width, _ = self.text_lifes.size() image_points_width, _ = self.image_points.size() text_points_width, _ = self.text_points.size() shift = 0 if self.modifiers & Modifiers.CENTER_H: width = _screen.get_width() / 2 xpos = width - (text_caps_width + image_lifes_width + text_lifes_width + image_points_width + text_points_width + self.seperator_width * 4) / 2 self.text_caps.draw(_screen, xpos + shift, _positiony) shift += text_caps_width + self.seperator_width self.image_lifes.draw(_screen, xpos + shift, _positiony) shift += image_lifes_width + self.seperator_width self.text_lifes.draw(_screen, xpos + shift, _positiony) shift += text_lifes_width + self.seperator_width self.image_points.draw(_screen, xpos + shift, _positiony) shift += image_points_width + self.seperator_width self.text_points.draw(_screen, xpos + shift, _positiony) else: self.text_caps.draw(_screen, _positionx + shift, _positiony) shift += text_caps_width + self.seperator_width self.image_lifes.draw(_screen, _positionx + shift, _positiony) shift += image_lifes_width + self.seperator_width self.text_lifes.draw(_screen, _positionx + shift, _positiony) shift += text_lifes_width + self.seperator_width self.image_points.draw(_screen, _positionx + shift, _positiony) shift += image_points_width + self.seperator_width self.text_points.draw(_screen, _positionx + shift, _positiony) def update(self, _delta): """ Update sprite. :param _delta: :return: nothing """ MySprite.update(self, _delta) def updatehud(self, _caps, _lifes, _points): """ Update HUD with specified number of caps, lifes, and points. :param _caps: Collected caps :param _lifes: Lifes left :param _points: Collected points :return: """ self.text_caps = TextSprite(_caps) self.image_lifes.scale(self.text_caps.size()[1], self.text_caps.size()[1]) self.text_lifes = TextSprite(_lifes) self.image_points.scale(self.text_caps.size()[1], self.text_caps.size()[1]) self.text_points = TextSprite(_points)